working on a project that involves monitoring temperature using an LM35 sensor with an STM32F4

Hello everyone, I'm currently working on a project that involves monitoring temperature using an LM35 sensor with an STM32F4 microcontroller. I'm utilizing FreeRTOS for task scheduling and a CAN bus library for communication. However, I'm encountering issues with inaccurate temperature readings. The readings appear to be significantly different from expected values. Could you please assist me in identifying potential issues in my code and suggest corrections to ensure accurate temperature measurements? Here's the relevant snippet from my sensor task implementation:
void sensor_task(void *pvParameters) {
while (1) {
// Read temperature sensor data using ADC library function
uint16_t raw_adc_data = read_temperature_sensor();

// Convert raw ADC data to temperature value (assuming linear conversion for now)
float temperature = raw_adc_data * 0.1;

// Send the temperature data for processing and transmission
send_temperature_data(temperature);

// Delay task for a specific period
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
void sensor_task(void *pvParameters) {
while (1) {
// Read temperature sensor data using ADC library function
uint16_t raw_adc_data = read_temperature_sensor();

// Convert raw ADC data to temperature value (assuming linear conversion for now)
float temperature = raw_adc_data * 0.1;

// Send the temperature data for processing and transmission
send_temperature_data(temperature);

// Delay task for a specific period
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
@Middleware & OS
Solution:
That's right , @Dtynin You need to take into account the reference voltage of your ADC and its resolution. For example, if you are using a 12-bit ADC with a reference voltage of 3.3V, the conversion should look something like this:
float temperature = (raw_adc_data * 3.3 / 4096) * 100;
float temperature = (raw_adc_data * 3.3 / 4096) * 100;
This ensures that the raw ADC value is properly converted to the voltage output by the LM35 and then to the corresponding temperature....
Jump to solution
10 Replies
ZacckOsiemo
ZacckOsiemo4mo ago
Your conversion seems to be almost too simplistic are you doing some preprocessing when reading?
Solution
Enthernet Code
Enthernet Code4mo ago
That's right , @Dtynin You need to take into account the reference voltage of your ADC and its resolution. For example, if you are using a 12-bit ADC with a reference voltage of 3.3V, the conversion should look something like this:
float temperature = (raw_adc_data * 3.3 / 4096) * 100;
float temperature = (raw_adc_data * 3.3 / 4096) * 100;
This ensures that the raw ADC value is properly converted to the voltage output by the LM35 and then to the corresponding temperature.
Dtynin
Dtynin4mo ago
Oh thanks, @Enthernet Code your detailed explanation on the conversion factor is very helpful. I will adjust the conversion to account for the reference voltage and ADC resolution.
UC GEE
UC GEE4mo ago
In addition to what @Enthernet Code said, @Dtynin make sure your ADC is properly calibrated and configured. Secondly,curtail the noise in the power supply or grounding issues because it can equally affect the moneyreadings.Try as well to add a small capacitor (e.g., 0.1µF) close to the sensor can help stabilize the readings. I believe these points will be helpful to your project.
Dtynin
Dtynin4mo ago
@UC GEE I'll check the calibration and configuration of my ADC and ensure proper grounding. Adding a capacitor for stabilization is a great idea. thanks
Edison_ngunjiri
Edison_ngunjiri4mo ago
Where is the function to read temp sensor?
Great~~~
Great~~~4mo ago
and also I think that it can be helpful by considering relationship between real temperature and capture adc value. currently it was assumed to linear relationship but I think that via several live experiments, we need to interpolate formula between adc value and real temperature.
Great~~~
Great~~~4mo ago
There are several alternatives for temperature sensor and I think that it is not problem due to sensor. when selecting any sensor, we have to consider its specification from datasheet and we need to preprocess and interpolate for sensor data. it is same case for LM35 too, I think.
Dtynin
Dtynin4mo ago
// Function to read temperature sensor using ADC library function
uint16_t read_temperature_sensor() {
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ADC_CHANNEL_0; // Assuming the sensor is connected to ADC channel 0
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;

if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
// Channel configuration error
printf("ADC Channel Configuration Error\n");
return 0;
}

if (HAL_ADC_Start(&hadc1) != HAL_OK) {
// ADC start error
printf("ADC Start Error\n");
return 0;
}

if (HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY) != HAL_OK) {
// ADC conversion error
printf("ADC PollForConversion Error\n");
return 0;
}

uint16_t raw_adc_data = HAL_ADC_GetValue(&hadc1);

if (HAL_ADC_Stop(&hadc1) != HAL_OK) {
// ADC stop error
printf("ADC Stop Error\n");
}

return raw_adc_data;
}
// Function to read temperature sensor using ADC library function
uint16_t read_temperature_sensor() {
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ADC_CHANNEL_0; // Assuming the sensor is connected to ADC channel 0
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;

if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
// Channel configuration error
printf("ADC Channel Configuration Error\n");
return 0;
}

if (HAL_ADC_Start(&hadc1) != HAL_OK) {
// ADC start error
printf("ADC Start Error\n");
return 0;
}

if (HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY) != HAL_OK) {
// ADC conversion error
printf("ADC PollForConversion Error\n");
return 0;
}

uint16_t raw_adc_data = HAL_ADC_GetValue(&hadc1);

if (HAL_ADC_Stop(&hadc1) != HAL_OK) {
// ADC stop error
printf("ADC Stop Error\n");
}

return raw_adc_data;
}
Edison_ngunjiri
Edison_ngunjiri4mo ago
Try initialize the ADC peripheral once ,and then put all the code to start the adc until get value in read_temperature_sensor().
Want results from more Discord servers?
Add your server