STM32 Interal temp sensor - Page 1
Forum section: electronics / general

author: gab_22
author_rank: Contributor
timestamp: November 04, 2023, 05:52:59 pm
content: Hi,I'm trying to perform an internal temperature measurement with the integrated temp sensor of an STM32F303RE on a NUCLEO-303RE board. And something goes wrong. Indeed my measurements are incorrect.Here is the main loopCode: (C)/* Initialize all configured peripherals */MX_GPIO_Init();MX_USART2_UART_Init();MX_ADC1_Init();/* USER CODE BEGIN 2 */if(HAL_ADC_Start(&hadc1) != HAL_OK){Error_Handler();}/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */if (HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY) == HAL_OK){uint32_t raw_temp = HAL_ADC_GetValue(&hadc1);float temperature = ((1.43 - ((raw_temp/4096.0)*1.2)) / 0.0043) + 25.0;char buffer;sprintf(buffer, "Température brute : %lu, Température en Celsius : %.4f\r\n", raw_temp, temperature);HAL_UART_Transmit(&huart2, (uint8_t *)buffer, strlen(buffer), HAL_MAX_DELAY);HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);}else{HAL_UART_Transmit(&huart2, "error\r\n", 8, HAL_MAX_DELAY);}HAL_Delay(1000); // 1 second delay}/* USER CODE END 3 */I applied the formula explained in the ST Reference manual with the values specified in the datasheet of my STM32F303 chip.According to the reference manual, the raw to Celsius conversion formula is :Code:{(V_25 - Vts) / AVG_Slope} + 25.0And the main conversion formula I wrote:Code:float temperature = ((1.43 - ((raw_temp/4096.0)*1.2)) / 0.0043) + 25.0;Firstly I convert the RAW ADC Value in volts withCode:(raw_temp/4096.0)*1.2)The analog to digital conversion is done with 12 bits so 4096 values and a ref of 1,2 Volt. So I have my Vts value hereAnd here my results :Code:RAW ADC Temperature °C1731239,62121727239,89371723240,16621722240,23441719240,43881720240,37061718240,50691717240,5751717240,5751717240,5751716240,64321714240,77941716240,64321712240,91571705241,39261702241,5971699241,80141698241,86961697241,93771697241,93771695242,07391695242,0739On the results above, the board was in a freezer before the test (and I confirm the STM32 was quite cold) and I put my finger on it.So it's unexpected, I expect a first value of about 0°C ~ 10°C and an increment of 2 ~3°C per second when I put my finger on the chip. Also, I don't understand why the computed temperature is about 240°C. I checked the formula several, and for me, it's okay between the one given by ST in the ref manual, and my own in the code

author: SiliconWizard
author_rank: Super Contributor
timestamp: November 04, 2023, 10:30:02 pm
content: Since we don't see that in the code you posted, are you sure you have selected the appropriate ADC channel for the temperature sensor?

author: DavidAlfa
author_rank: Super Contributor
timestamp: November 05, 2023, 02:14:19 am
content: Try the maximum sampling time for that adc channel:Code:sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5

author: wek
author_rank: Frequent Contributor
timestamp: November 05, 2023, 08:32:57 am
content: QuoteNo.Reference to the ADC comes from the VREF+/VDDA pin, which on Nucleo is connected (through a choke) to the 3.3V VDD.The internal VREFINT voltage source serves only as an indirect reference and is connected to one of the ADC's inputs.A writeup on VREFINT and temperature measurement in STM32here, a list of commonly made errorshere.JW

author: harerod
author_rank: Frequent Contributor
timestamp: November 05, 2023, 07:09:15 pm
content: wek, thanks for collecting and sharing those gotchas. That should save heaps of time, even for those who had a look at the docu.Looking at this list, it still amazes me how much time we burnt in getting to know STM32's, starting with the F103 about 15 years ago, when docu was still scarce, then the F40x, which introduced new peripherals, then the F0, which added yet another flavor to the family. Not forgetting the fun with the L, G and H derivates.As a side quest we had fun with different IDE's, be it IAR, Keil or GCC.And then ST changing API's now and then, just to keep things interesting.What a Long Strange Trip It's Been...

author: wek
author_rank: Frequent Contributor
timestamp: November 06, 2023, 12:00:29 am
content: > how much time we burnt in getting to know STM32's, starting with the F103 about 15 years agoYes; but at least it's still STM32, for whatever it's worth. It used to be an entirely different CPU or MCU every 5 years...JW

author: harerod
author_rank: Frequent Contributor
timestamp: November 06, 2023, 08:46:19 am
content: Quote from: wek on November 06, 2023, 12:00:29 amMy thoughts exactly. At some point it happened that I had built more designs around STM32 than all the other families which I have known together. Z80, 68k, 6800, 8051, PIC, AVR, AVR32, TMS320, to name a few - most of those have been established for decades. Interesting enough, I had to write the least assembly for those CPU's, which evolved from the Acorn Risc Machine that gave us Virus on the Archimedes (https://en.wikipedia.org/wiki/Zarch).Over the years it was cute to see how little tricks, which we had figured out from reading the documentation, slowly turned into application notes.Well enough history. I just wanted to say thanks for that link. Should be mandatory reading.

author: peter-h
author_rank: Super Contributor
timestamp: November 06, 2023, 12:48:40 pm
content: See herehttps://www.eevblog.com/forum/microcontrollers/stm-32f4-reading-cpu-temperature/msg4980826/#msg4980826QuoteYes, but you now have a "platform" which can probably do every single one of the jobs you had done before. That is highly valuable. I am finishing off the 32F417/437 project (yes the one I have been posting questions on here for the past few years - it's taken a few years mainly because I've had to learn C and all kinds of other stuff at the same time, and came from asm background) and it's pretty obvious that this design can be used for absolutely anything I ever want to build in the rest of my life (say next 20 years). I can forget the Atmel AVR stuff which the company would discontinue every 5 mins, etc.

author: gab_22
author_rank: Contributor
timestamp: November 07, 2023, 08:40:35 pm
content: Hi,Thanks everyone for the responses. It works now.QuoteQuote from: wek on November 05, 2023, 08:32:57 amMany thanks for this helpful link. Indeed, the implementation is more complicated than I had imagined.