on 2025-07-08 12:00 AM
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).
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.
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:
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:
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:
The next section provides a step-by-step example of how to calibrate the temperature sensor using ADC4 on the NUCLEO-U575ZI-Q board.
The versions used in this article are:
Step 1: Open STM32CubeMX and click the ACCESS TO BOARD SELECTOR button
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
Step 3: A pop-up is displayed. Click [without TrustZone activated], and then click [OK]
Step 4: Another pop-up is displayed. Unselect all LEDs and buttons by clicking [Unselect All], and then click [OK]
Step 5: ADC4 Mode and Configuration
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:
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
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:
Temperature in °C = ((130 - 30) / (7941 - 1412)) × (912 - 1412) + 30
= 0.015 × (-500) + 30
= -7.65 + 30
= 22.34°C ≈ 23°C
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.