sunnuntai 16. helmikuuta 2025

STM32 and MCU temperature measurement offset

This is not specific to STM32-series, but can affect other similar setup too.

STM32 has a internal thermistor that can be used to measure MCU temperature (and by association,  rougly device temperature MCU is in). I was testing this recently, and found out it seems to read a bit high (30C when in 22C room), but I assume this is due to device being a bit warmer inside.

However, when I turned power on to one major functional block of the device, measured MCU temperature insatntly jumped quite a bit - to about 60C. My first assumption naturally was that voltage from that block somehow leaks back to MCU and causes said drift. But no, no such defect was found, all voltages read nominal values.

After short period of head scratching, I figured the most likely reason for this. Said block is relatively power-hungry, and caused system-wide voltage drop of about a tenth of a volt. MCUs thermistor is connected directly to this system voltage (MCU's VDD), and thermistor's measurement range is fairly narrow, so analog voltage of thermistor reading shifted as a result.

I'm also using 2.048v voltage reference to AD converter which isn't affected by this drop. As a result of said thermistor voltage shift and non-shifted reference, AD reading shifted a bit too. Not much, but enough to throw off the reading.  (if I had used VDD directly as reference, this error would not have happened, but then all other measurement I want to make would be affected by VDD voltage drifts - which would be much worse.) 

(note also that due to 2.048v reference, I already had to compensate measurement since calibration values stored on MCU are measured with 3.3v VDD/reference. Therefore adding shifting 3.3v compensation was relatively tiny addition)

Unfortunately STM32 does not offer possibility to internally measure VDD voltage, so I had to add external voltage divider for that and then measure that (fortunately I still had unused IO pin with analog function available), but after that I could compensate temperature calibration with input voltage drift. After that, no more temperature drift, regardless of system load.

Another fun troubleshooting day at the office.