152.Internal temperature sensor returned unexpected value (troubleshooting guide)
ADC in STM32 features a temperature sensor on one of its inputs. This is a circuit, which generates voltage linearly proportional to the internal temperature of the chip. Users often test this as this is a relatively convenient test to try the ADC; however, sometimes the results are confusing.
Here are some guidelines how to achieve correct result from temperature sensor:
- Make sure you have correctly connected VSSA/VREF- and VDDA/VREF+ to stable voltage sources within datasheet values.
- Make sure the ADC has enabled clock in RCC, and its frequency after prescaling is within the datasheet limits.
- In STM32 families where applicable, make sure you have performed ADC calibration before taking measurement.
- To make sure the ADC performs conversions properly, you can enable the internal voltage reference's channel and take a measurement on it, comparing the result to the expected value in datasheet, and/or the factory calibration value VREFINT_CAL stored in the system memory.
- Note, that in STM32 models with multiple ADC units, temperature reference is available usually only in one of them.
- Temperature sensor has to be enabled, and the exact method depends on the STM32 family. In most families, this is accomplished by setting a register bit in ADC itself, often ADC_CCR.TSEN. For added confusion, in the 'L4 and 'L5 families, the same bit is called ADCx_CCR.CH17SEL, and simultaneously to enabling the temperature sensor it also flips a switch at given ADC MUX input (from internal DAC connection). In some families, temperature sensor has a common enable bit with the internal VREFINT reference, ADC_CR2.TSVREFE. Uniquely in the 'L0 family, besides ADC_CCR.TSEN, it has to be enabled also in SYSCFG_CFGR3.ENBUF_SENSOR_ADC; and this fact is not mentioned in the Temperature sensor subchapter of ADC chapter.
- After enabling the temperature sensor, a startup time specified in the Datasheet has to be observed.
- Select the proper input in the ADC MUX. In some 'F4 and 'F7, the ADC input used for temperature measurement is shared with VBAT measurement; in that case, make sure VBAT measurement is not enabled.
- Make sure appropriate sampling time is set for given channel, according to minimum sampling time for Temperature sensor, given in Datasheet.
- Calculating converted value to temperature may be confusing, here are some first-principle guidelines. If you use library functions/macros, make sure you understand what are the individual parameters, and supply them in the expected physical units. With macros, note that improperly used types may lead to incorrect results e.g. when mixing unsigned and signed integer variables, and/or surprisingly lengthy calculations and/or unexpected FLASH and RAM consumptiond due to imported libraries, e.g. when double are used inadvertently.
- Note, that the TS_CAL1/TS_CAL2 calibration values are taken at different temperatures and different reference voltages for different STM32 families. Refer to the Datasheet for the details. Be cautious, sometimes there are errors in the documentation.
- In some STM32 models (in older families, and in the "Value Line" models), one or both TS_CAL1/TS_CAL2 calibration values are unavailable. In that case, you have to resort to using the Average slope and Voltage at X°C parameters for the Temperature sensor in Datasheet. Note, that these values have a relatively high variation and errors up to several tens of °C may be expected in this case.
- Note, that the temperature sensor is not a precision instrument. Even with using the calibration values, expect +-5°C of deviation. ST recommends to use it as a monitor of relative temperature changes, rather than as an absolute thermometer.
- ADC measurements are subject to noise from internal and external sources, and given the relatively high slope of the ADC characteristics (cca 2 bins/°C at VREF=3V), it has noticeable impact on the calculated temperature. Consider filtering/smoothing of measured values.
- Note, that the temperature sensor measures the internal temperature of the chip, not the ambient temperature. It may be surprisingly quite a couple of degrees higher than ambient, depending on the current consumption and degree of cooling applied to the chip.