Skip to main content
Graduate II
September 23, 2025
Solved

Thermistor reading error with ADC1

  • September 23, 2025
  • 3 replies
  • 375 views
Post edited by ST moderator to be inline with the community rules especially with the code sharing.
1- In next time please use </> button to paste your code. Please read this post: How to insert source code.
2- Post is not related to CubeIDE usage but more to the product so it needs to be moved to "STM32 MCUs Products" forum. So please don't post your question in CubeIDE only if it's related to CubeIDE usage or to raise a limitation in it.
 
I have a 110k resistor in series with a 110k thermistor as well, at the midpoint I connect PC0, the other leg of the thermistor to 3.3v,  the sConfig.SamplingTime is 247.5.  But the readings are 110 Celsius when it should read 22 Celsius, what am I doing wrong,  this is my code, thanks
define R_SERIE 115000.0f // resistor fijo del divisor
#define BETA 3950.0f // coef. beta del NTC
#define T0 298.15f // 25 °C en Kelvin
#define R0 115000.0f // resistencia nominal NTC a 25°C

ADC_HandleTypeDef hadc1;
volatile uint16_t adc_val = 0;
volatile float temperature = 0.0f;
volatile int setpoint = 30;

float NTC_ReadTemperature(void)
{
 HAL_ADC_Start(&hadc1);
 HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
 uint32_t adc_val = HAL_ADC_GetValue(&hadc1);
 float vref = 3.3f;
 float v_adc = (vref * adc_val) / 65535.0f;
 float r_ntc = (R_SERIE * v_adc) / (vref - v_adc);
 float tempK = 1.0f / ((1.0f / T0) + (1.0f / BETA) * logf(r_ntc / R0));
 return tempK - 273.15f;
}

 

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

    Increase your sampling period to max.

    3 replies

    Technical Moderator
    September 23, 2025

    Hello,

    You didn't provide the MCU partnumber: How to write your question to maximize your chances to find a solution

     

    Graduate II
    September 23, 2025

    tks; stm32h753zi

    regards

    Super User
    September 23, 2025

    @yogui_ricardo wrote:
    the readings are 110 Celsius when it should read 22 Celsius,

    The ADC knows nothing about Celsius - it just reads a voltage.

    Have you checked the voltage at the ADC pin to see if it is actually what you expect, given the temperature?

    Have you checked the raw ADC reading to see if that corresponds to the voltage you measure?

    If you take the raw ADC reading and apply the calculations manually, do you get the expected result?

     

    As well as the MCU part number, you also need to say what board you are using - as noted in the link which @mƎALLEm provided.

    You didn't mention a ground connection.

    Rather than trying to describe connections in words, a schematic is far more effective.

    Some good, clear photos of your setup may also help.

    TDKAnswer
    Super User
    September 23, 2025

    Increase your sampling period to max.