STM32G0 internal temperature invalid readout
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-03 4:03 AM
Hello,
For some reason I'm unable to get valid readout of internal MCU temperature. Maybe someone have an ide why this might be happening? System is running on STM32G0B0 MCU. I init ADC over HAL generated code and enable only internal inputs (`ADC_CHANNEL_TEMPSENSOR`, `ADC_CHANNEL_VREFINT` and `ADC_CHANNEL_VBAT`), after init I run ADC calibration which reports that it did it's job. but when I start to read temperature periodically - every minute, I see following results (these are raw values domming from DMA so I assume it is `temp`, `Vref` and `Vbat`):
temp Vref Vbat
03B7 07E3 045B
050F 07E4 045D
0513 07E4 0460
0511 07E5 045B
0512 07E5 045B
0512 07E5 045A
0513 07E3 0457
0504 07E9 0481
0509 07E9 0488
0526 07EB 0497
0532 07ED 049C
051F 07EB 0499
0502 07E7 0482
0508 07E8 047B
050A 07E5 0475
0509 07E5 0470
050B 07E5 046F
050F 07E5 0470
050E 07E5 046D
050B 07E3 0467
0510 07E5 0466
0513 07E5 0465
0513 07E5 0466
050F 07E4 0467
0512 07E5 0464
050F 07E5 0463
0511 07E5 0464
If I plot these values to a chart, I see following:
When using `__LL_ADC_CALC_TEMPERATURE()` function apart 1st read (shows -83, I assume *C), but later is calculated to ~ -16*C and high peak is -9*C.
This is when system starts at room temperature (~ 22*C) and is heated to ~ 65*C then left to cool down. this process is visible in char, but in terms of temperature is very inaccurate.
Any hints on where and what could be checked 1st? - particulat MCU factory calibrations are - TEMPSENSOR_CAL1(1037), TEMPSENSOR_CAL2(1386).
P.S. Some interesting notes - before calibration same device was reading values equal to ~23*C as base temperature, but when heating increase was around same magnitude. Other devices shows different base temperature - but increase, when changing is more or less same.
Solved! Go to Solution.
- Labels:
-
ADC
-
STM32G0 Series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-04 7:20 AM
Issue is fixed.
In total there were 2 problems:
- ADC calibration was absent so result from device to device was very different - after calibration call was added all were working in same wring fashion.
/* ADC1 init function */
void MX_ADC1_Init(void)
{
* * *
/* USER CODE BEGIN ADC1_Init 2 */
if (HAL_ADCEx_Calibration_Start(&hadc1) != HAL_OK) {
}
/* USER CODE END ADC1_Init 2 */
}
​
- HAL driver function either buggy or I do not know how to use it. changed to simple calculation from RM - and it worked:
*temp = (((TEMPSENSOR_CAL2_TEMP - TEMPSENSOR_CAL1_TEMP) * (((*temp) * (*VREFINT_CAL_ADDR) / (*vcc)) - (*TEMPSENSOR_CAL1_ADDR))) / ((*TEMPSENSOR_CAL2_ADDR) - (*TEMPSENSOR_CAL1_ADDR))) + TEMPSENSOR_CAL1_TEMP;​
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-03 4:26 AM
Some more info needed:
- What's used as ADC voltage reference? VDDA? If yes, what's the voltage level?
- Are the measured Vbat and Vref values okay? Calculate these back to voltages.
- At which temperatures were the temp sensor cal values taken? And at which voltage? I'd look for the pointers to those and check all that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-03 5:57 AM
Increase sampling time to max.
Is this a custom board? If so, show the schematic.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-03 6:15 AM
In all STM32 I dealt with, the internal temperature channel was basically the forward voltage across a pn-junction.
The observed curve and course of values look reasonable in this regard.
I don't know the Cube code you mentioned in this regard.
But I assume you either have a bug in your temperature calculation, or __LL_ADC_CALC_TEMPERATURE() and such are buggy themselves.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-03 8:40 AM - edited ‎2025-06-03 8:46 AM
Some troubleshooting hints here.
JW
PS. From the values above and the datasheet, I've quickly calculated, that your VREF+ (or VDDA if it's a small-pin-count package) reference voltage is 2.45V; the temperature sensor output voltage is roughly 0.77V, which corresponds to roughly +30deg.C.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-04 7:20 AM
Issue is fixed.
In total there were 2 problems:
- ADC calibration was absent so result from device to device was very different - after calibration call was added all were working in same wring fashion.
/* ADC1 init function */
void MX_ADC1_Init(void)
{
* * *
/* USER CODE BEGIN ADC1_Init 2 */
if (HAL_ADCEx_Calibration_Start(&hadc1) != HAL_OK) {
}
/* USER CODE END ADC1_Init 2 */
}
​
- HAL driver function either buggy or I do not know how to use it. changed to simple calculation from RM - and it worked:
*temp = (((TEMPSENSOR_CAL2_TEMP - TEMPSENSOR_CAL1_TEMP) * (((*temp) * (*VREFINT_CAL_ADDR) / (*vcc)) - (*TEMPSENSOR_CAL1_ADDR))) / ((*TEMPSENSOR_CAL2_ADDR) - (*TEMPSENSOR_CAL1_ADDR))) + TEMPSENSOR_CAL1_TEMP;​
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-06 7:55 AM
__LL_ADC_CALC_TEMPERATURE() is correct, but RM0456 Reference manual for STM32U5 rev 6 is NOT!!
Description in "34.4.27 Temperature sensor and internal reference voltage" "Reading the temperature" p. 1416 does not take into account Vref which makes calculation acc.to reference manual produce incorrect result.
