Author Topic: STM32 Interal temp sensor  (Read 1634 times)

0 Members and 1 Guest are viewing this topic.

Offline gab_22Topic starter

  • Contributor
  • Posts: 10
  • Country: fr
STM32 Interal temp sensor
« on: November 04, 2023, 05:52:59 pm »
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 loop
Code: (C) [Select]
  /* 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[50];
       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: [Select]
{(V_25 - Vts) / AVG_Slope} + 25.0
And the main conversion formula I wrote:
Code: [Select]
float temperature = ((1.43 - ((raw_temp/4096.0)*1.2)) / 0.0043) + 25.0;
Firstly I convert the RAW ADC Value in volts with
 
Code: [Select]
(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 here

And here my results :

Code: [Select]
RAW ADC Temperature °C
1731 239,6212
1727 239,8937
1723 240,1662
1722 240,2344
1719 240,4388
1720 240,3706
1718 240,5069
1717 240,575
1717 240,575
1717 240,575
1716 240,6432
1714 240,7794
1716 240,6432
1712 240,9157
1705 241,3926
1702 241,597
1699 241,8014
1698 241,8696
1697 241,9377
1697 241,9377
1695 242,0739
1695 242,0739

On the results above, the board was in a freezer before the test (and I confirm the STM32 was quite cold  :D ) 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
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14481
  • Country: fr
Re: STM32 Interal temp sensor
« Reply #1 on: November 04, 2023, 10:30:02 pm »
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?
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5912
  • Country: es
Re: STM32 Interal temp sensor
« Reply #2 on: November 05, 2023, 02:14:19 am »
Try the maximum sampling time for that adc channel:
Code: [Select]
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 495
  • Country: sk
Re: STM32 Interal temp sensor
« Reply #3 on: November 05, 2023, 08:32:57 am »
Quote
The analog to digital conversion is done with 12 bits so 4096 values and a ref of 1,2 Volt.
No.

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 STM32 here, a list of commonly made errors here.

JW
 
The following users thanked this post: harerod, gab_22

Offline harerod

  • Frequent Contributor
  • **
  • Posts: 449
  • Country: de
  • ee - digital & analog
    • My services:
Re: STM32 Interal temp sensor
« Reply #4 on: November 05, 2023, 07:09:15 pm »
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...
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 495
  • Country: sk
Re: STM32 Interal temp sensor
« Reply #5 on: November 06, 2023, 12:00:29 am »
> how much time we burnt in getting to know STM32's, starting with the F103 about 15 years ago

Yes; 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
 

Offline harerod

  • Frequent Contributor
  • **
  • Posts: 449
  • Country: de
  • ee - digital & analog
    • My services:
Re: STM32 Interal temp sensor
« Reply #6 on: November 06, 2023, 08:46:19 am »
> how much time we burnt in getting to know STM32's, starting with the F103 about 15 years ago

Yes; 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...

My 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.
 

Online peter-h

  • Super Contributor
  • ***
  • Posts: 3698
  • Country: gb
  • Doing electronics since the 1960s...
Re: STM32 Interal temp sensor
« Reply #7 on: November 06, 2023, 12:48:40 pm »
See here
https://www.eevblog.com/forum/microcontrollers/stm-32f4-reading-cpu-temperature/msg4980826/#msg4980826

Quote
What a Long Strange Trip It's Been...

Yes, 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.
« Last Edit: November 06, 2023, 12:57:09 pm by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 
The following users thanked this post: gab_22

Offline gab_22Topic starter

  • Contributor
  • Posts: 10
  • Country: fr
Re: STM32 Interal temp sensor
« Reply #8 on: November 07, 2023, 08:40:35 pm »
Hi,
Thanks everyone for the responses. It works now. :-+

Quote
RAW, °C
1740, 35.1142
1740, 35.1142
1728, 37.3059
1722, 38.4018
1719, 38.9498
1716, 39.4977
1715, 39.6804
1714, 39.8630
1716, 39.4977

Quote
The analog to digital conversion is done with 12 bits so 4096 values and a ref of 1,2 Volt.
No.

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 STM32 here, a list of commonly made errors here.

JW
Many thanks for this helpful link. Indeed, the implementation is more complicated than I had imagined.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf