# Reading internal temperature sensor STM32

I'm trying to read internal temperature sensor. Each time, the value from ADC conversion is 296 which results in negative temperature. Should I add something to the code below, enable some peripheral or my calculations are wrong?

#define TEMP_SENSOR_AVG_SLOPE_MV_PER_CELSIUS                        2.5f
#define TEMP_SENSOR_VOLTAGE_MV_AT_25                                760.0f

int32_t sensorValue, temperature;

// Disable Vbat signal from input channel and wake up temp sensor from power down mode

{
temperature = (int32_t)((sensorValue - TEMP_SENSOR_VOLTAGE_MV_AT_25) / TEMP_SENSOR_AVG_SLOPE_MV_PER_CELSIUS + 25);
}
else
{
temperature = -273;
}

return temperature;


--EDIT

#define TEMP_SENSOR_AVG_SLOPE_MV_PER_CELSIUS                        2.5f
#define TEMP_SENSOR_VOLTAGE_MV_AT_25                                760.0f
#define TEMP110_CAL_VALUE                                           ((uint16_t*)((uint32_t)0x1FFF7A2E))
#define TEMP30_CAL_VALUE                                            ((uint16_t*)((uint32_t)0x1FFF7A2C))
#define TEMP110                                                     110.0f
#define TEMP30                                                      30.0f

int32_t temperature;
float sensorValue;

// Disable Vbat signal from input channel and wake up temp sensor from power down mode

{
temperature = (int32_t)((TEMP110 - TEMP30) / ((float)(*TEMP110_CAL_VALUE) - (float)(*TEMP30_CAL_VALUE)) * (sensorValue - (float)(*TEMP30_CAL_VALUE)) + TEMP30);
}
else
{
temperature = -273;
}

return temperature;


  ADC_ChannelConfTypeDef sConfig;

/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
{
Error_Handler();
}

/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Rank = 1;
sConfig.Offset = 0;
{
Error_Handler();
}


I'm getting the output around 35 degrees. Is it okay to have such a big offset?

• What are the definitions of ADC_REFERENCE_VOLTAGE_MV and the other constants? – bitsmack Aug 16 '17 at 20:40
• ADC_REFERENCE_VOLTAGE_MV is a bandgap voltage = 1210mV; ADC_MAX_OUTPUT_VALUE = 4095; TEMP_SENSOR_VOLTAGE_MV_AT_25 = 760mV; TEMP_SENSOR_AVG_SLOPE_MV_PER_CELSIUS = 2.5mV/C; – VIPPER Aug 16 '17 at 20:48
• Thank you, but can you post the actual #define statements? It may be a signed/unsigned error or something similar. You can edit them into your question. – bitsmack Aug 16 '17 at 20:54
• This is good code. However, asking how one should improve your code or something that should be added is not for us to say because this is your project. But you can ask us if there's something else that you want to add but you're not sure how script such thing. Figuring out how to take measurements (like temperature) is something you need to figure out on your own unless you're attempted something but there's something wrong and you can't figure out what's wrong. Don't forget to check the error log if something happens. – KingDuken Aug 16 '17 at 21:27
• @VIPPER always mention the model – P__J__ Aug 16 '17 at 21:54

Like PeterJ pointed out, the first flaw is that you have to set the ADC_CCR_TSVREFE bit and not reset it.

I've no idea how you set the sample and hold time, but I hope it is correct. It has to be at least 10 µs for an accurate measurement (datasheet section about temperature sensor).

Your next big flaw is in your thinking that the reference voltage is 1.21 V. That is the nominal value for the $V_{refint}$. This voltage is not the reference voltage for the ADC. The reference voltage is usually $V_{DDA}$ or a different externally supplied voltage on the $V_{REF+}$-pin. But it can't be higher than $V_{DDA}$ nor can it be lower than $V_{DDA}$-1.2 V.

With your conversion result of 950, and taking 3.3 V you would end up with 27.2 °C, which seems like a good start. If you have 3 V it'd be -0.6 °C, which also seems okay.

Why do I consider -0.6 °C a good value? Because calculation using the average values is crap.

I don't know why, but STM doesn't promote their calibrated values much. Every device has two ADC raw values taken at 30 °C and 110 °C at 3.3 V stored internally. Using those values, you end up with more reasonable temperature values without performing calibration.

Something along these lines should do:

// see datasheet for position of the calibration values, this is for STM32F429
const uint16_t* const ADC_TEMP_3V3_30C =  reinterpret_cast<uint16_t*>(0x1FFF7A2C);
const uint16_t* const ADC_TEMP_3V3_110C =  reinterpret_cast<uint16_t*>(0x1FFF7A2E);
const float CALIBRATION_REFERENCE_VOLTAGE = 3.3F;

const float REFERENCE_VOLTAGE = 3.0F; // supplied with Vref+ or VDDA

// scale constants to current reference voltage



I'm used to C++, so maybe not your coding style, but that shouldn't be a big problem.

• I've found these calibration values in the reference manual of STM32F0. This is really strange that in STM32F4 reference manual I couldn't find it. Moreover, they described the procedure of reading temperature sensor value with ADC and didn't mention about any calibration values. However, these values appear in the datasheet... from my point of view, it's a little mess in their docs. – VIPPER Aug 17 '17 at 18:06
• Regarding STM32L1 AN3964 app note, the calibration reference voltage is 3V, not 3.3V as you mentioned above. Is that correct? – VIPPER Aug 17 '17 at 18:22
• I have no idea how you came up with 27.2 degrees :D – VIPPER Aug 17 '17 at 18:47
• @VIPPER not sure why you care for the STM32L1, but yeah in that device the calibration is done at 3 V. On the F429 it is done at 3.3 V. As for the 27.2 °C: I used your formulas with 950 as ADC value but changed the ADC reference voltage from 1210 mV to 3300 mV. – Arsenal Aug 18 '17 at 6:32
• And for your edit: we got a deviation of up to 6 K with the internal sensor. If it is more, something seems wrong (measure Vdda to make sure it is 3.3 V for example) – Arsenal Aug 18 '17 at 6:37
// Disable Vbat signal from input channel and wake up temp sensor from power down mode
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.2859804630279541, "perplexity": 2605.7962894924135}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2020-16/segments/1585370521574.59/warc/CC-MAIN-20200404073139-20200404103139-00392.warc.gz"}