A 12-bit converter gives 4096 levels, yet in many designs the result wanders by a dozen LSB or reads consistently low. Usually the converter is not to blame – the way the signal is connected and the configuration are. Below is what we check before we call a measurement finished, based on ST’s AN2834 and the datasheets.
Where the errors come from
AN2834 separates the converter’s own errors (offset, gain, non-linearity – given in the datasheet) from external ones: an inaccurate reference voltage, supply noise, too high a source impedance and noise coupled from the board. In practice the external ones dominate – and they are the ones we control.
1. Source impedance versus sampling time
A SAR ADC connects a sampling capacitor CADC (a few pF) to the input during the sampling time. If the source resistance is high, the capacitor does not charge fully and the result reads low. The datasheet gives the condition for settling within ¼ LSB:
R_AIN < T_S / (C_ADC × ln(2^(N+2))) − R_ADC
TS is the sampling time, N the resolution, RADC the sampling switch resistance. Take CADC and RADC from the datasheet of your specific device.
Example: measuring battery voltage through a 100 kΩ / 100 kΩ divider. The impedance seen by the ADC is the two resistors in parallel: 50 kΩ. STM32G0, 16 MHz ADC clock, 12 bits, assuming CADC = 5 pF and neglecting the small RADC. Denominator: 5 pF × ln(214) ≈ 5 pF × 9.70 ≈ 48.5 pF.
- 12.5 cycles → TS ≈ 0.78 µs → RAIN max ≈ 16 kΩ – not enough;
- 39.5 cycles → TS ≈ 2.47 µs → RAIN max ≈ 51 kΩ – borderline, no margin for RADC and tolerances;
- 79.5 cycles → TS ≈ 4.97 µs → RAIN max ≈ 102 kΩ – with margin.
We pick 79.5 cycles, or reduce the divider resistors (at the cost of current), or add an input capacitor – see below.
2. A capacitor at the ADC input
A capacitor CEXT from the input to ground acts as a charge reservoir: when sampling, charge flows from CEXT into CADC, and the voltage drops by roughly V × CADC / CEXT. With CEXT = 100 nF and CADC = 5 pF that is 0.005% – about 0.2 LSB at 12 bits. The condition: between conversions of that channel the capacitor must recharge through the source resistance. For a 50 kΩ divider and 100 nF the time constant is 5 ms, so this works for slow measurements (battery voltage, temperature), not for sampling an AC signal.
3. Calibration
Most newer families (G0, G4, L4, H7, U5) have built-in offset calibration. Run it after power-up, with the converter disabled and the ADC voltage regulator enabled – in HAL e.g. HAL_ADCEx_Calibration_Start(&hadc1) (with an ADC_SINGLE_ENDED argument on some families). The calibration factor is lost, among other cases, when the converter’s supply is switched off (deep power-down), and its value depends on temperature and voltage – after a large change in operating conditions it is worth recalibrating.
4. Reference voltage and VREFINT
The ADC result is proportional to VREF+. In small packages VREF+ is tied to VDDA, so any regulator error (±2–3%) goes straight into the measurement. The fix is to measure the internal reference VREFINT, whose value ST measures for every device in production and stores in system memory (VREFINT_CAL):
V_DDA = VREFINT_CAL_VREF × VREFINT_CAL / VREFINT_DATA
V_channel = V_DDA × DATA_channel / 4095
VREFINT_CAL_VREF is the voltage at which the calibration was done – 3.0 V on STM32G0, G4 and L4, 3.3 V on STM32F0 and F3 (always check the datasheet). The LL library provides __LL_ADC_CALC_VREFANALOG_VOLTAGE() and __LL_ADC_CALC_DATA_TO_VOLTAGE(). The VREFINT channel must be enabled (VREFEN bit) and sampled for a long time – the datasheet gives the minimum sampling time, in the order of a few µs.
When better than about 1% accuracy is needed, we use an external reference on VREF+ (in packages where the pin is separate) or the internal VREFINT buffer VREFBUF (e.g. STM32G4, L4, H7).
5. Oversampling and averaging
Averaging reduces noise, and oversampling adds resolution: every 4× oversampling gives 1 extra bit, provided the signal carries at least about 1 LSB of noise (without noise every sample is identical and nothing is gained). STM32G0, G4, L4 and H7 have a hardware oversampler: up to 256 samples and a 0–8 bit right shift, with no CPU load. For example, 16 samples with a 2-bit shift give a 14-bit result.
Oversampling does not remove systematic errors – offset, gain error or reference error. That is why calibration and VREFINT are needed regardless.
6. Board and supply
- VDDA and VREF+ decoupled as the datasheet specifies (typically 100 nF + 1 µF at the pins), VDDA preferably fed from the digital supply through an LC filter or ferrite.
- Analog traces away from clocks, PWM and the switching regulator; a solid ground plane under the analog path.
- During a conversion, avoid switching large currents on GPIO pins next to the analog input.
- When switching between channels with very different voltages and high-impedance sources, the first sample may carry “memory” of the previous channel – lengthen the sampling time or discard the first result.
RedCan checklist: ADC measurement
- Source impedance calculated for every channel, sampling time chosen with the datasheet formula.
- High-resistance dividers: input capacitor and sufficiently infrequent conversions.
- ADC calibration at start-up and after a large temperature or voltage change.
- VDDA computed from VREFINT and VREFINT_CAL, not assumed to be a constant 3.3 V.
- VREFINT channel sampled for at least the datasheet minimum.
- Hardware oversampling instead of software averaging where the family has it.
- VDDA/VREF+ decoupling and filtering, analog path away from digital signals.
- Verification against a calibrator or reference multimeter at several points across the range.
Related reading: low-power Stop-mode design and HSE and LSE crystal selection.
Further reading
- AN2834 – How to optimize the ADC accuracy in the STM32 MCUs (STMicroelectronics) – error sources, source impedance, calibration, averaging and board recommendations.
- ADC measurements recommendations (ST Wiki) – practical ADC guidance from ST.
- The datasheet of your device – the ADC characteristics table (CADC, RADC, the RAIN formula) and the VREFINT_CAL address.
If the measurements in your device do not match expectations, we can help through an electronics design review.