adc-temperature.c

STM32H7 Nucleo-144 (flagship: NUCLEO-H743ZI) · File : adc-temperature.c · Language : C · Use case : Lire le capteur de température interne via l'ADC

The internal thermal sensor hangs off a dedicated ADC channel: this program converts it, applies the datasheet typical calibration and prints the chip temperature in Celsius over the serial port.

Hardware

A Nucleo H7 board (STM32H743ZI, Cortex-M7 @ 480 MHz) programmed via ST-LINK (USB), 3.3 V pins. None: internal sensor + USB cable for the serial terminal.

Wiring

No wiring. In CubeMX enable the ADC « Temperature Sensor » channel and the TSVREFE clock.

How it works

HAL_ADC_PollForConversion waits for end of conversion; the 12-bit value becomes a voltage then a temperature using V25=760 mV and slope 2.5 mV/°C (typical values).

Running it

STM32CubeIDE project based on the Nucleo-H743ZI: drop the code into main.c, declare peripherals in the .ioc, then Run over ST-LINK. The ST-LINK serial monitor shows printf at 115200.

Variants

Add hardware oversampling in CubeMX, or also read the internal reference VREFINT to correct the supply voltage.

adc-temperature.c

// Temperature interne via l'ADC (canal interne) + printf.
#include "main.h"
#include <stdio.h>

extern ADC_HandleTypeDef hadc1;   // canal interne temperature (16)
extern UART_HandleTypeDef huart2;

int __io_putchar(int ch) {
  HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
  return ch;
}

uint16_t brut;
uint32_t t0 = 0;

int main(void) {
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  MX_ADC1_Init();     // canal temperature + TSVREFE active dans CubeMX

  for (;;) {
    if (HAL_GetTick() - t0 >= 1000) {
      t0 = HAL_GetTick();
      HAL_ADC_Start(&hadc1);
      if (HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK) {
        brut = HAL_ADC_GetValue(&hadc1);
        // Calibrage typique ST : V25 = 760 mV a 25 C, slope 2,5 mV/C.
        float tension = brut * 3300.0f / 4095.0f;       // mV
        float temp = 25.0f + (tension - 760.0f) / 2.5f; // C
        printf("brut=%u  T=%.1f C\r\n", brut, temp);
      }
      HAL_ADC_Stop(&hadc1);
    }
  }
}

← Back to the board