Reading temperature from adc value(Non linear)
Hi,
I am using STM32G491RE. I have to read temperature values from ADC. We used voltage divider circuit to map the voltage values with temperature values it is non linear change between volatge and temperature.
I tried below way
const uint16_t adc_values[182] = {/* mention all voltage values*/};
const uint16_t temperature_values[182]={/* mention corresponding temperature values*/};
const int calibration_points = sizeof(adc_values) / sizeof(adc_values[0]);
float mapADCtoTemperature(uint16_t adc_value) {
for (int i = 0; i < calibration_points; i++) {
if(adc_value==adc_values[i])
{
return temperature_values[i];
}
}
return -1.0;
}
while(1)
{
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
uint16_t adc_value = HAL_ADC_GetValue(&hadc1);
float voltage=(float)adc_value/4095.0 * 3.3;
float temperature = mapADCtoTemperature(voltage);
char buff[100];
sprintf(buff,"ADC value: %d,voltage:%.2f V,Temperature: % .2f C\r\n",adc_value,voltage,temperature);
}
With above code I am getting only few correct temperature values.Is there any other way to do it.
Can anyone suggest.
Thanks
