Trouble getting accurate ADC readings
- March 9, 2026
- 1 reply
- 175 views
I'm trying to read a NTC temperature sensor, but the readings I'm getting are very inconsistent and at times just way off. I have nucleo on the way but this testing I'm doing is on a blue pill.
The sensor is sitting in a cup of water, so temperature should be very consistent. Here is the readings I'm getting which are done at 1 second intervals, I don't need speed. The temperature of the water is about 21C. The sensor accuracy is rated +/- 1.4C at 25C
Temp k: 293. c: 20.8
Temp k: 293. c: 20.7
Temp k: 294. c: 21.2
Temp k: 292. c: 19.5
Temp k: 307. c: 34.1
Temp k: 295. c: 22.3
Temp k: 294. c: 20.8
Temp k: 293. c: 20.7
Temp k: 294. c: 21.5
Temp k: 293. c: 20.7
Temp k: 294. c: 21.3
Temp k: 294. c: 21.4
Temp k: 293. c: 20.8
Temp k: 293. c: 20.5
Temp k: 294. c: 21.1
Temp k: 292. c: 19.7
Temp k: 266. c: -6.5
Temp k: 293. c: 20.5
Temp k: 293. c: 20.8
Temp k: 291. c: 18.8
Temp k: 266. c: -7.1
Temp k: 293. c: 20.6
Temp k: 294. c: 21.3
Temp k: 294. c: 21.3
Temp k: 293. c: 20.6
Temp k: 294. c: 20.9
Attached is the clock config. I've triad various settings, faster and slower.
ADC config:
hadc1.Instance = ADC1;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
How I'm reading at 1 second intervals:
if (HAL_OK == HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY)) {
vout = (HAL_ADC_GetValue(&hadc1) / 4095.0) * vin;
rout = rval * ((vin / vout) - 1);
temp_k = 1 / ((log(rout / rval) / coeff) + (1 / temp_base));
temp_c = temp_k - 273.15;
}
