cancel
Showing results for
Did you mean:

ST Employee

Using the STM32 ADC's internal reference voltage

Introduction

In some applications, specifically battery-powered ones, it is common for the supply voltage to drop as the battery discharges. Sometimes, both VDDA and VREF+ is powered directly by the battery. The problem with this is if the VDDA is based off of this battery voltage, then you may have difficulties to get precise voltage conversions from the ADC. This is due to the math that converts the raw ADC value to a voltage relies on knowing VDDA/VREF+.

One solution to this is to separate VDDA and VREF+, using an external LDO to drop the VREF+ voltage below the battery voltage. This is done to ensure that the LDO maintains a stable voltage for VREF+ even as the battery is close to discharged. On some packages, VDDA, and VREF+ are internally bonded, which removes this as a solution.
For those who do not want the following:

• Having to rely on an external solution
• Have a package where VDDA and VREF+ are internally bonded
• Reduce the number of components used

VREF internal can be used to calculate the voltage of VDDA in software.

As an example, I am using the STM32CubeMX/STM32CubeIDE with the NUCLEO-C031. However, this example generally applies to any STM32 with an internal VREF.

1. CubeMX Setup

In CubeMX, we set up two channels: A channel of a voltage we want to read, and another channel, which is the VREF internal channel. We need to understand how long to sample the VREF internal channel. The datasheet for C031 shows that the internal reference voltage needs to be sampled for at least 4us. From DS13867, Table 27:

In CubeMX, we enable the ADC channels we want to use:
In the clock tree, the ADC is getting its clock from the system clock running at 48MHz, which is prescaled by 2 in the ADC configuration.

Knowing our ADC clock is 24MHz, we can calculate the minimum sampling time:
``4u / (1 / 24M) = 96``

Out of the available sampling time options, 160.5 cycles is the only option that is greater than or equal to 96. Therefore, we use that sampling time.

I am using the same sampling time for Channel 0, but you can feel free to use whatever sampling time you prefer for that channel. That is all for the CubeMX configuration and now we can generate code.

2. VREF Internal Theory

During factory programming, a calibration value of the raw ADC data of VREF internal at a known temperature and VDDA was collected and stored in ROM. From DS13867, Table 6:

Using this calibration value presented below, along with the measured VREF internal in the application, the VDDA can be calculated. The formula below was obtained from the C0x1 reference manual, RM0490 page 274:

Since the HAL library provides a macro to do this calculation, there is not a need to implement this, but it is good to understand how it works. It is also worth mentioning that VREF internal does have some variance over temperature, which is roughly characterized in the datasheet, DS13867 Figure 13:
Since the characterizations are not precise and there is per device variance, for best performance in extreme temperature ranges it would be necessary to characterize this temperature coefficient experimentally.

3. Using VREF internal in the application

The HAL libraries make the usage of VREF internal quite simple. In this example, I am using a simple polling method to get the ADC readings, but this could be implemented using interrupts or DMA as well.

``````/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */

// Start the conversion sequence

/*
* Rank 1: vref internal
*/

/*
* Rank 2: measurement channel
*/

/*
* This macro calculates the vdda voltage (as a uint32_t representing the voltage in milliVolts)
* using the vref internal raw adc value, and the internal calibration value in ROM
*/

/*
* A helper function for doing a raw ADC value to voltage conversion, essentially
*
* vdda_voltage * measurement_raw / 2^12
*
*/

/* USER CODE END 2 */``````

Conclusion

In a situation where your application is battery powered, your VDDA voltage will drop over time as the battery discharges. If absolute voltage measurements are required regardless of VDDA's voltage, the internal reference voltage can be used to measure the voltage of VDDA. By using the measured voltage of VDDA in our ADC data conversions, precise voltage measurements can be made without a consistent VDDA.