2025-09-23 9:33 AM - last edited on 2025-09-23 10:11 AM by mƎALLEm
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;
}
Solved! Go to Solution.
2025-09-23 11:38 AM - edited 2025-09-23 11:38 AM
Increase your sampling period to max.
2025-09-23 9:40 AM
Hello,
You didn't provide the MCU partnumber: How to write your question to maximize your chances to find a solution
2025-09-23 9:54 AM
@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.
2025-09-23 10:09 AM
tks; stm32h753zi
regards
2025-09-23 11:38 AM - edited 2025-09-23 11:38 AM
Increase your sampling period to max.