cancel
Showing results for 
Search instead for 
Did you mean: 

How to calibrate a temperature sensor using ADC4 on STM32U575

KDJEM.1
ST Employee

Introduction

The calibration of temperature sensors is a critical step to ensure accurate measurements in embedded systems. The STM32U575 microcontroller integrates two analog-to-digital converters (ADC1 and ADC4).

  • ADC1 supports configurable resolutions of 14, 12, 10, or 8 bits.
  • ADC4 supports configurable resolutions of 12, 10, 8, or 6 bits.

Both ADCs can measure temperature. However, calibration is performed exclusively by ADC1, as it uses specific calibration values (TS_CAL) provided with a 14-bit resolution.

This guide explains how to configure and use ADC4 to perform accurate temperature measurements while leveraging the calibration data from ADC1.

1. Calibration process for accurate temperature measurement using ADC4

As mentioned previously, the two ADCs can measure temperature; however, calibration is performed exclusively by ADC1 with 14-bit resolution. This is because the calibration process relies on TS_CAL1 and TS_CAL2 values, which are the temperature sensor's 14-bit raw data acquired by ADC1, with VDDA = VREF+ = 3.0 V, at 30°C and 130°C, respectively.

These values are stored in memory at the following addresses:

  • TS_CAL1: 0x0BFA0710 to 0x0BFA0711
  • TS_CAL2: 0x0BFA0742 to 0x0BFA0743

Two calibration methods can be applied:

1. First method: Convert the TS_CAL1 and TS_CAL2 values into a voltage (in volts). An example of calibration with TS_CAL1 = 0x0EB5 and TS_CAL2 = 0x137E is shown below:

  • TS_CAL1 = 0x0EB5 => 3765 => Vref x (3765 / 2^14) = 3.0 x (3765 / 16384) = 0.689392 V
  • TS_CAL2 = 0x137E => 4990 => Vref x (4990 / 2^14) = 3.0 x (4990/16384) = 0.913696 V

These voltage values can be used for calibration. The integrated temperature sensor generates an output voltage proportional to the temperature.

2. The second method: Use the TS_CAL1 and TS_CAL2 values to scale to 12 bits.

An example of calibration with TS_CAL1 = 0x0EB5 and TS_CAL2 = 0x137E is shown below.

For this method, you can convert the 14-bit ADC result to its 12-bit equivalent by shifting the result 2 bits to the right:

  • TS_CAL1 = 0x0EB5 = 0b0000 1110 1011 0101 => two bits right shift: 0b0000 0011 1010 1101 => 0x03AD
  • TS_CAL2 = 0x137E = 0b0001 0011 0111 1110 => two bits right shift: 0b0000 0100 1101 1111 => rounded at 2nd LSB will result 0b0000 0100 1110 0000 => 0x4E0

The next section provides a step-by-step example of how to calibrate the temperature sensor using ADC4 on the NUCLEO-U575ZI-Q board. 

2. Step by step example 

2.1. Prerequisites

Hardware:

  • Micro-USB cable to power and program the board
  • NUCLEO-U575ZI-Q
KDJEM1_3-1748526581559.png

 

Software:

The versions used in this article are:

2.2. Steps to calibrate the temperature sensor using ADC4

Step 1: Open STM32CubeMX and click the ACCESS TO BOARD SELECTOR button

 

KDJEM1_2-1749651714367.png

 

Step 2: Enter the part number in the Commercial Part Number field (in this example, the NUCLEO-U575ZI-Q board is used), select it, and then click Start Project

 

KDJEM1_3-1749651803201.png

Step 3: A pop-up is displayed. Click [without TrustZone activated], and then click [OK]

 

KDJEM1_6-1748527245899.png

Step 4: Another pop-up is displayed. Unselect all LEDs and buttons by clicking [Unselect All], and then click [OK]

 

KDJEM1_7-1748527577708.png

Step 5:  ADC4 Mode and Configuration

  • In the Analog menu, select ADC4, and then select Temperature Sensor Channel.
KDJEM1_1-1748529451679.png
 
  • In the parameter settings, enable Continuous Conversion Mode and set Sampling Time Common1 and Sampling Time Common2 to 814.5 cycles. Select TempSens, enable Analog WatchDog3 Mode, and set the High Threshold to 913 and the Low Threshold to 689, as shown in the screenshot below.

KDJEM1_0-1748536177863.png

  • In the NVIC settings, select ADC4 (12-bit) global interrupt to enable it, as shown in the screenshot below.

KDJEM1_3-1748529921446.png

Step 6: Generate code

In the Manager Project section at the top, enter the project name in the Project Name field. Select STM32CubeIDE in the Toolchain / IDE field, and click the Generate Code button, as shown in the screenshot below:

KDJEM1_4-1749652119450.png

Step 7: Edit main.c

To configure ADC4 and retrieve the temperature value using ADC4, you must add specific functions to the main.c file in the dedicated user code section

/* USER CODE BEGIN PV */
__IO int uhADCxConvertedData[2] = {0} ; /* ADC group regular conversion data */
__IO uint32_t ITamp1IntStatus,ITamp7IntStatus,ITamp12IntStatus,ITamp13IntStatus;
__IO int Vrefint = 0;
__IO int VCore = 0;
__IO int temperature = 0;
int Vrefanalog = 3300;
/* USER CODE END PV */

 

/* USER CODE BEGIN 2 */
  /* Start ADC group regular conversion */
  if (HAL_ADC_Start_IT(&hadc4) != HAL_OK)
  {
    /* Error: ADC conversion start could not be performed */
    Error_Handler();
  }
   // Enable temperature monitoring
  HAL_PWREx_EnableMonitoring();
  /* USER CODE END 2 */

 

 /* USER CODE BEGIN ADC4_Init 2 */

  /* Perform ADC calibration */
  if (HAL_ADCEx_Calibration_Start(&hadc4, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED) != HAL_OK)
  {
    /* Calibration Error */
    Error_Handler();
  }

  /* USER CODE END ADC4_Init 2 */

 

/* USER CODE BEGIN 4 */

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
  static int i = 0;
  uhADCxConvertedData[i] = HAL_ADC_GetValue(hadc);
  i++;
  if (i == 2)
    i = 0;
/* Calculate Temp for debugging */
  temperature = __HAL_ADC_CALC_TEMPERATURE(ADC4,Vrefanalog,uhADCxConvertedData[1],ADC_RESOLUTION_12B);
}
/* USER CODE END 4 */

 

Step 8: Add the temperature variable to the Watch Expressions in the debugger to observe the calibrated temperature value

KDJEM1_6-1751896522852.png

Step 9: Checking temperature value with the theoretical formula

To verify the returned temperature, refer to the following formula:

Temperature in °C = ((TS_CAL2_TEMP - TS_CAL1_TEMP) / (TS_CAL2 - TS_CAL1)) × (TS_DATA - TS_CAL1) + TS_CAL1_TEMP

With:

  • TS_CAL2_TEMP = 130°C (± 5°C)
  • TS_CAL1_TEMP = 30°C (± 5°C)
  • TS_CAL2 at address 0x0BFA0742 - 0x0BFA0743 is 0x7C15 = 0b0111 1100 0001 0101 => two-bit right shift: 0b0001 1111 0000 0101 => 0x1F05 = 7941.

KDJEM1_2-1751896372291.png

  •  TS_CAL1 at address 0x0BFA0710 - 0x0BFA0711 is 0x1610 = 0b0001 0110 0001 0000 => two-bit right shift: 0b0000 0101 1000 0100 => 0x0584 = 1412

KDJEM1_1-1751896691818.png

  • TS_DATA is the actual temperature sensor output value converted by the ADC.
    In this case, uhADCxConvertedData represents the TS_DATA content, which is 912.

KDJEM1_5-1751896460085.png

Temperature in °C = ((130 - 30) / (7941 - 1412)) × (912 - 1412) + 30
                              = 0.015 × (-500) + 30
                              = -7.65 + 30
                              = 22.34°C ≈ 23°C

Conclusion

After following this guide, you should now be able to calibrate the temperature using ADC4. If you encounter any issues, create a post in the ST Community STM32 MCUs Products forum for further assistance.

Related links

Version history
Last update:
‎2025-07-07 7:42 AM
Updated by: