cancel
Showing results for 
Search instead for 
Did you mean: 

Thermistor reading error with ADC1

yogui_ricardo
Associate III
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;
}

 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Super User

Increase your sampling period to max.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

4 REPLIES 4
mƎALLEm
ST Employee

Hello,

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

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Andrew Neil
Super User

@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.

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

tks; stm32h753zi

regards

TDK
Super User

Increase your sampling period to max.

If you feel a post has answered your question, please click "Accept as Solution".