Skip to main content
Explorer II
September 27, 2023
Solved

Reading temperature from adc value(Non linear)

  • September 27, 2023
  • 4 replies
  • 3191 views

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

 

    This topic has been closed for replies.
    Best answer by sireevenkat1

    Issue is solved. Thanks for suggestions. In my application voltage mapping with temperature already done by hardware team I just need to check the voltage values based on that need to perform the action.


    Thanks

    4 replies

    Graduate II
    September 27, 2023

    Edited as I misread the question.

    Explorer II
    September 27, 2023

    HI @###### ,

    Thanks for the suggestion but I am not using internal temperature sensor for temperature values reading. I am using voltage divider circuit(it has two resistor and one variable resistor) for temperature and voltage values mapping.
    What will be the TS_CAL2 & TS_CAL1 values if I am not using internal temperature sensor?
    I already have manually calculated temperature values for given voltage values . I just want to know how to get that temperature values for given voltage values through programming with stm32G491RE.

    Thanks

    Graduate II
    September 27, 2023

    Sorry, didn't read your question properly, I'll edit. The only thing possibly useful in what I posted above might be the internal reference data that is also stored.

    I would guess if you are getting a non-linear result then you are using a non-linear NTC thermistor? I think there are some packaged linear thermistors available that have a PTC.

    Alternatively I would suggest you create and store a Look Up Table in ROM of calibrated values and then a function to interpolate and find an estimate nearest value.

    There might be a mathematical method to fit a curve to the response but I wouldn't know how to do this.

    ST Employee
    September 28, 2023

    Hi @sireevenkat1 

     

    The forum moderator had marked your post as needing a little more investigation and direct support. An online support case has been created on your behalf, please stand by for just a moment and you will hear from us.

     

    Regards,

    Billy

    sireevenkat1AuthorAnswer
    Explorer II
    December 26, 2023

    Issue is solved. Thanks for suggestions. In my application voltage mapping with temperature already done by hardware team I just need to check the voltage values based on that need to perform the action.


    Thanks