cancel
Showing results for 
Search instead for 
Did you mean: 

Having an issue with using nucleo-l432kc, MCP9700A thermistor, and an HD44780 LCD where once program runs first loop the incorrect temp displays (~2-3 degrees C less than actual temp) then it evens out by the 2nd or 3rd output.

cl.1_99
Associate II

0693W000000UQkyQAG.pngI have a thermistor attached to the nucleo-l432kc board and a C program that obtains the temperature and prints it on a terminal display, an LCD display, and lights up a corresponding colour of an RGB LED. I initially had my LCD set up to 3.3V (because that is what our thermistor is supposed to be powered by), and didn't have any issues with the output of temperature values in terms of printing, LCD display, or the LED that turns on. But now that I have changed the LCD input voltage to 5V to increase the contrast (using a potentiometer from the 5V), there are issues with the output of temperature that I am unsure of how to fix. I've attached a photo of the LCD and nucleo-l432kc schematic setup. Also worth noting, I've tried to change it back to the 3.3V I initially used and it just displays the wrong temp for the entire duration, not just the first loop, still being about 2-3 degrees lower.

NOTE* I've deleted the commented sections that stm32cube ide creates automatically just to make it a bit easier to read.

My code is as follows:

#include "main.h"

#include "stdio.h"

ADC_HandleTypeDef hadc1;

TIM_HandleTypeDef htim1;

UART_HandleTypeDef huart2;

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART2_UART_Init(void);

static void MX_ADC1_Init(void);

static void MX_TIM1_Init(void);

//A function to set the amount of red, green, and blue for any colour

void set_rgb(uint8_t red, uint8_t green, uint8_t blue)

{

htim1.Instance->CCR1 = red;

htim1.Instance->CCR2 = green;

htim1.Instance->CCR3 = blue;

}

int main(void)

{

 HAL_Init();

 SystemClock_Config();

 MX_GPIO_Init();

 MX_USART2_UART_Init();

 MX_ADC1_Init();

 MX_TIM1_Init();

 HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);

 HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);

 HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);

 HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);

 HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);

 HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3);

 uint32_t measureVoltage = 0;

 HD44780_Init();

 float voltPA1 = 0.;

 float tempValue = 0.;

 char voltageValue [100] = {0}; //this is where the temp value will be stored after being

 //converted to a string so it can be displayed on the LCD

 while (1)

 {

  /* USER CODE END WHILE */

 //obtain the analog voltage value

 HAL_ADC_Start(&hadc1);

 HAL_ADC_PollForConversion(&hadc1, 1000);

 measureVoltage = HAL_ADC_GetValue(&hadc1);

 HAL_ADC_Stop(&hadc1);

 //convert the analog voltage value to digital

// voltPA1 = (measureVoltage * 3300.)/4096.; //note that I'm not currently using this value as I found just using the direct ADC value obtained from above gives the correct temp

 //convert the voltage to temperature

 tempValue = (measureVoltage - 587.)/10.; //where 500mv is the thermistor voltage @ 0 deg C and 87mv is the calibration voltage that I measured based on an ADC reading of 807 at a temperature of 22 deg C.

 //convert the temperature from float to string so it can be displayed on LCD

 sprintf(voltageValue, "%+4.1f", tempValue);

 //blue led on

 if (-40.0 <= tempValue && tempValue < 20.5)

 {

 HD44780_PutStr("Temp is "); //display temp on LCD display

 HD44780_PutStr(voltageValue); //display actual temp #, now stored as a string, on LCD

 HD44780_GotoXY(0,1); //move LCD position to the second line of display:

 HD44780_PutStr("degrees C"); //display "degrees C" on the second line of LCD

 set_rgb(0, 0, 0); //turn off all LEDs to avoid mixing incorrect colours

 set_rgb(0, 0, 255); //turn on blue LED (blue set to 255 max, red and green set to 0)

 printf("\rThe temp is %+4.1f °C\n", tempValue); //print temp to terminal screen

 HAL_Delay(3000); //keep light/LCD/terminal o/p on for 3 seconds

 HD44780_ClrScr(); //clear screen before looping through again

 }

 //green led on

 else if (20.5 <= tempValue && tempValue < 37.3)

 {

 HD44780_PutStr("Temp is ");

 HD44780_PutStr(voltageValue);

 HD44780_GotoXY(0,1);

 HD44780_PutStr("degrees C");

 set_rgb(0, 0, 0);

 set_rgb(0, 255, 0); //turn on green LED (green set to 255 max, red and blue set to 0)

 printf("\rThe temp is %+4.1f °C\n", tempValue);

 HAL_Delay(3000);

 HD44780_ClrScr();

 }

 //orange led on

 else if (37.3 <= tempValue && tempValue < 39.5)

 {

 HD44780_PutStr("Temp is ");

 HD44780_PutStr(voltageValue);

 HD44780_GotoXY(0,1);

 HD44780_PutStr("degrees C");

 set_rgb(0, 0, 0);

 set_rgb(255, 50, 0); //turn on orange - a mix of red at 255 + green at 50.

 printf("\rThe temp is %+4.1f °C\n", tempValue);

 HAL_Delay(3000);

 HD44780_ClrScr();

 }

 //red led on

 else if (39.5 <= tempValue && tempValue < 150.0)

 {

 HD44780_PutStr("Temp is ");

 HD44780_PutStr(voltageValue);

 HD44780_GotoXY(0,1);

 HD44780_PutStr("degrees C");

 set_rgb(0, 0, 0);

 set_rgb(255, 0, 0); //turn on red LED (red set to 255 max, green and blue set to 0)

 printf("\rThe temp is %+4.1f °C\n", tempValue);

 HAL_Delay(3000);

 HD44780_ClrScr();

 }

 }

Any suggestions for how I can fix this problem of a single incorrect value printing first? I'm assuming it's because of the LCD display.

0 REPLIES 0