STM32 ADC Reference Voltage: Measure VDD with VREFINT and Set a Custom VREF
Every ADC reading on an STM32 is a ratio of the input voltage to the reference voltage. When the real reference differs from the value used in the code, every calculated voltage is wrong, even though the ADC itself works correctly. The error is common in battery powered designs, where the supply voltage falls as the battery discharges.
This tutorial shows three ways to handle the reference voltage. You will measure VDD with the internal VREFINT channel on a Nucleo F446RE, apply an external VREF+ on an STM32H750 board to get more resolution from a low-voltage sensor, and generate a reference with the VREFBUF peripheral. This is Part 10 of the STM32 ADC series. If you have not read an ADC channel before, begin with the single channel polling tutorial, and the next part covers ADC differential mode.
Table of contents
STM32 ADC Reference Voltage Basics
The ADC converts an analog voltage into a number by comparing it with a reference voltage (VREF). An input equal to VREF produces the maximum digital value, which is 4095 for a 12-bit ADC, and an input of 0 V produces 0. The reference therefore sets the upper limit of the measurable range.
ADC value = Vin / Vref x (2^n - 1) (12-bit: 4095, 16-bit: 65535)
Vin = ADC value x Vref / (2^n - 1)
LSB = Vref / 2^n = 3.3 V / 4096 = about 0.806 mVWhy the Reference Voltage Matters
A noisy or drifting reference produces noisy or drifting readings, and a reference that differs from the value in the code produces a constant error. Suppose a device is powered by a battery and the supply falls from 3.3 V to 2.0 V while VDD is the ADC reference. An input of 1.0 V then gives a raw value of about 2047. The code still assumes 3.3 V, so it calculates about 1.65 V, which is an error of 65 percent. A full-scale input of 2.0 V gives 4095, and the code would report 3.3 V.
The fix is to measure the real reference and use it in the formula, which the first example in this tutorial does with VREFINT. The effect of other settings on accuracy is covered in the ADC hardware oversampling tutorial, which explains how to gain resolution and reduce noise.
Internal and External Reference Voltage
STM32 devices offer two kinds of reference. The internal reference VREFINT is a fixed voltage of about 1.2 V that does not depend on VDD and is calibrated in the factory. It is not used as the ADC reference for normal readings, but it can be measured to calculate VDD or VREF+. The external reference is supplied on the VREF+ pin, either from a precision source or from the VREFBUF peripheral, and it sets the real full-scale voltage of the ADC. A 2.5 V reference IC, for example, gives a stable full scale that does not follow the supply.
STM32 ADC Reference Pins
| Pin or source | Purpose | Note |
|---|---|---|
| VDD and VSS | Digital supply | Most devices work from about 1.8 V up to 3.6 V, so check the datasheet |
| VDDA and VSSA | Analog supply | Tied to VDD on most development boards |
| VREF+ and VREF- | External reference input | Not present on every package, and tied to VDDA by default |
| VREFINT | Internal fixed reference of about 1.2 V | Read through an ADC channel and independent of VDD |
| VREFBUF | On-chip buffer that outputs a reference on VREF+ | Available only on some devices |
Whether a separate VREF+ pin exists, and whether it is connected to VDDA or VDD, depends on both the microcontroller package and the design of the board. The same microcontroller can have VREF+ tied to VDD on one board and routed to its own pin on another, so always check the schematic of the board in addition to the datasheet of the chip. The two boards used here show both cases.
STM32 ADC VREFINT on Nucleo F446RE
The first example measures VDD on a Nucleo F446RE with the internal reference channel.
Reference Pins of the F446RE
The datasheet of the STM32F446 shows how the reference pins depend on the package.
- The 144-pin package has VREF+, VDDA and VSSA as separate pins.
- The 100-pin package has separate VREF+ and VDDA pins.
- The Nucleo F446RE uses the 64-pin package, where VREF+ and VDDA are joined inside the package.
On the Nucleo schematic, VDDA and VREF+ are connected to VDD.
Solder bridge SB57 on this board links VDDA to VDD, and opening it allows a separate supply for the analog pin. Because VREF+ and VDDA are one pin on this package, they stay together, so a separate external reference voltage is not possible on this board. This tutorial leaves the board unmodified and changes VDD instead. VDDA and VREF+ then change with it, and the VREFINT channel measures this supply.
Why Measure VDD
A device that runs from a battery sees VDD fall as the battery discharges. If the ADC uses VDD as its reference, the same input voltage then produces a different raw value, and the calculation with a fixed 3.3 V is wrong. The solution has two steps.
- Measure VDD regularly with the internal reference VREFINT.
- Use the measured VDD as VREF in the ADC formula.
The measured value can also trigger a low battery warning. To repeat the measurement at fixed intervals, the conversion can be started by a timer, as shown in the ADC external trigger tutorial.
CubeMX Configuration for the F446RE
Enable the Vrefint channel in ADC1 together with one input channel, which is channel 0 here. Both channels are converted in one sequence with DMA, so scan mode must be on with two conversions, and the rank order decides the order of the results. Vrefint is converted first, so it lands in ADC_VAL[0], and channel 0 follows in ADC_VAL[1]. The DMA is used in circular mode with continuous conversion so that the values keep refreshing. The setup of a multi-channel DMA sequence is described in the multiple channels circular DMA tutorial.
The sampling time of the Vrefint channel needs care. The datasheet of the F446 requires at least 10 µs for this channel, so choose a number of cycles that gives 10 µs or more at your ADC clock. At an ADC clock of 21 MHz, 10 µs equals 210 cycles, which means that the 480 cycle setting is the next suitable choice. The calculation of conversion time is explained in the ADC conversion time tutorial.
The screenshot shows a Vrefint value of about 1.21 V. The factory calibration value of VREFINT is stored in memory, and it was measured at VDDA = 3.3 V.
VREFINT Code for the F446RE
The formula uses the calibration value. The factory data was captured with a 3.3 V supply, so for any other supply the ratio of the two readings gives the actual voltage.
VREF+ (mV) = 3300 x VREFINT_CAL / VREFINT_DATAuint16_t ADC_VAL[2]; /* [0] = VREFINT, [1] = input on channel 0 */
volatile uint32_t vref = 0; /* VDDA and VREF+ in mV */
volatile uint32_t voltage = 0; /* input voltage in mV */
/* Factory value of VREFINT measured at VDDA = 3.3 V.
If VREFINT_CAL_ADDR is not defined, include the LL ADC header of your series. */
#define VREFINT_CAL_VALUE (*VREFINT_CAL_ADDR)In main, start the ADC with DMA for the two conversions.
if (HAL_ADC_Start_DMA(&hadc1, (uint32_t *)ADC_VAL, 2) != HAL_OK)
{
Error_Handler();
}When both conversions have finished, the DMA calls the conversion complete callback, where the supply and the input voltage are calculated.
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
if ((hadc->Instance == ADC1) && (ADC_VAL[0] != 0)) /* avoid a division by zero */
{
vref = (3300UL * VREFINT_CAL_VALUE) / ADC_VAL[0]; /* VDDA in mV */
voltage = ((uint32_t)ADC_VAL[1] * vref) / 4095UL; /* input in mV, 12-bit ADC */
}
}The calibration value belongs to the resolution given in the datasheet, which is 12 bits on the F4 series. If the ADC runs at another resolution, scale the calibration value accordingly.
Test Setup and Result on the F446RE
To vary VDD, the board must not take its power from the ST-Link. Remove the jumper that selects the ST-Link as the power source and feed the board from a bench supply on a 5V pin of the board. Connect the grounds, keep the voltage below the maximum rating of the device and note that the on-board regulator limits how low the supply can go.
With a supply of about 2 V, the variable vref also shows about 2000 mV. The input on channel 0 is calculated with this value.
- With 0.5 V at the input, the live expression shows 499 mV, and the raw value is 1047.
- With 2 V at the input, the live expression shows 1997 mV, and the raw value is 4095.
The second case is full scale, so the result of the calculation equals the reference itself. An input at or above VREF always gives 4095, and the calculated voltage then only shows VREF and no longer the input.
The same method works as a battery monitor, because the measured VDD can drive an alarm when it falls below a limit. Printing the value to a terminal is easy with the UART transmit tutorial.
STM32 ADC External VREF+ on H750
On the Nucleo F446RE, VREF+ follows VDD, so the VREFINT channel could measure VDD directly. The STM32H750 development board used next has separate pins for VREF+ and VDDA. VDD stays constant here, and the external voltage on VREF+ is the one that varies and is measured.
Hardware Change on the H750 Board
The schematic connects VREF+ to 3.3 V through a 0 Ω resistor, R11, while VDDA has its own connection to 3.3 V.
Removing R11 turns VREF+ into an independent pin that accepts an external reference voltage.
The external voltage on VREF+ must stay between the minimum VREF+ given in the datasheet and the voltage on VDDA, so never apply more than VDDA to this pin.
CubeMX Configuration for the H750
The Vrefint channel of the H750 belongs to ADC3, and the example reads it in blocking mode, so continuous conversion is disabled there.
The datasheet of the H750 requires a sampling time of at least 4.3 µs for this channel, so select a sampling time that gives 4.3 µs or more at your ADC clock.
The screenshot shows about 1.216 V for Vrefint. The calibration value is again stored in memory and was measured at VDDA = 3.3 V. The input signal uses ADC1 channel 3 in single-ended mode, and ADC1 runs in circular DMA mode so that it keeps converting without help from the CPU. For a different way to read a single channel, the single channel interrupt and DMA tutorial explains the DMA options.
VREFINT Code for the H750
The H750 ADC has a resolution of 16 bits, so the full-scale value is 65535. The array holds the input reading from ADC1 in position 0 and the Vrefint reading from ADC3 in position 1.
uint16_t ADC_VAL[2]; /* [0] = ADC1 channel 3 (DMA), [1] = VREFINT from ADC3 */
volatile uint32_t vref = 0; /* VREF+ in mV */
volatile uint32_t voltage = 0; /* input voltage in mV */
volatile uint8_t isDone = 0; /* set by the DMA callback */
/* Factory value of VREFINT measured at VDDA = 3.3 V */
#define VREFINT_CAL_VALUE (*VREFINT_CAL_ADDR)
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
if (hadc->Instance == ADC1)
{
isDone = 1; /* the data is processed in the main loop */
}
}The main loop reads the Vrefint channel of ADC3 in blocking mode and uses the result to calculate the actual VREF+. It then converts the raw value of ADC1 with that reference.
/* Calibrate both ADCs once after power-up; the exact calibration mode depends on the HAL version */
HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
HAL_ADCEx_Calibration_Start(&hadc3, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)&ADC_VAL[0], 1);
while (1)
{
/* Read VREFINT on ADC3 */
HAL_ADC_Start(&hadc3);
if (HAL_ADC_PollForConversion(&hadc3, 10) == HAL_OK)
{
ADC_VAL[1] = (uint16_t)HAL_ADC_GetValue(&hadc3);
if (ADC_VAL[1] != 0)
{
vref = (3300UL * VREFINT_CAL_VALUE) / ADC_VAL[1]; /* VREF+ in mV */
}
}
HAL_ADC_Stop(&hadc3);
if (isDone)
{
isDone = 0;
voltage = ((uint32_t)ADC_VAL[0] * vref) / 65535UL; /* input in mV, 16-bit ADC */
}
HAL_Delay(250);
}The code first calculates VREF+ from the internal reference and then uses that value as the reference of the input conversion. If the voltage on VREF+ changes, the calculated input voltage follows it. On the H7 series the DMA buffer must be in a RAM region that the DMA can reach, such as the AXI SRAM, and with the data cache enabled the buffer needs cache maintenance.
Result with an External VREF+ on the H750
In the first capture the voltage on VREF+ is about 2 V, and the debugger shows a vref of about 2000 mV. With about 0.6 V at the input, the variable voltage shows 625 mV.
When the input rises to 2 V, the calculated voltage matches the input, and the raw value reaches 65535, which is the full scale of the 16-bit ADC.
A custom reference is useful for sensors with a low output range. For a sensor that outputs 0 V to 2 V, a VREF+ of 2 V uses the whole ADC range. A 3.3 V reference would use only about 61 percent of the range for the same sensor, which means a step of 50 µV instead of 30.5 µV at 16 bits. The gain in resolution is real only as far as it exceeds the noise of the signal and of the reference.
STM32 ADC VREFBUF on H750
Some STM32 devices contain the VREFBUF peripheral, an analog voltage reference buffer that generates the reference voltage internally. It is supplied from VDDA and outputs the reference on the VREF+ pin. On the H750 the selectable outputs are 1.5 V, 1.8 V, 2.048 V and 2.5 V, while other families offer fewer options. VDDA must be sufficiently above the selected voltage, so check the minimum supply for each output in the datasheet.
The VREFBUF can supply the internal ADC and also external components through the VREF+ pin. The load current on that pin is very limited, about 4 mA, so it is not suitable for powering a sensor with a significant current. The peripheral can also be left unused, and an external voltage source then feeds the VREF+ pin. The datasheet also specifies a capacitor on VREF+ for the buffer, so check the value for your device.
VREFBUF Configuration in CubeMX
Set the VREFBUF mode to Internal Voltage Reference and select an output voltage. This example uses 1.5 V. CubeMX then assigns the VREF+ pin as VREFBUF_OUT and generates the VREFBUF setup in main.c.
VREFBUF Result
The code from the previous example stays the same, which makes it possible to check that the generated voltage is correct, and the external supply on the VREF+ pin is removed. The debugger shows a vref of about 1500 mV, equal to the selected output, and the input voltage is measured correctly against this internal reference.
With a 1.5 V reference and 16 bits, one step is about 22.9 µV, but the input range then ends at 1.5 V. Choose the output so that the largest signal stays below VREF+.
Conclusion
In this tutorial we saw why the reference voltage decides the accuracy of every ADC reading, and we handled it in three ways. On the Nucleo F446RE the internal VREFINT channel and its factory calibration value gave the actual VDD, which then replaced the fixed 3.3 V in the voltage calculation. On the STM32H750 board an external VREF+ made it possible to match the full scale of the ADC to a low-voltage sensor, and the VREFBUF peripheral generated a 1.5 V reference without any extra hardware.
The main point is that an ADC value is only a ratio. If the real reference changes and the code does not follow it, the calculated voltage is wrong, which matters most for battery powered devices and for sensors with a small output range. The next part of the series, ADC differential mode, measures the voltage between two pins and is useful for noise rejection and small signals. For a time-critical conversion that must interrupt a running sequence, the injected conversion tutorial is a good next step.
Download Project Here
Download the CubeMX project files and HAL source code — all tested on real hardware.
If you find this useful, you can support us with a Donation as well.
Browse More STM32 ADC Tutorials

Arun is an embedded systems engineer with 10+ years of experience in STM32, ESP32, and AVR microcontrollers. He created ControllersTech to share practical tutorials on embedded software, HAL drivers, RTOS, and hardware design — grounded in real industrial automation experience.
Recommended Tools
Essential dev tools
Categories
Browse by platform


















Hello, I play with Nucleo H755. Maybe you know how many bits has VREFIN_CAL? 12-bit or 16-bit? I cannot find it in documentation. Do you know where I can find this information?