MOULDLKnowledge BaseElectrical Knowledge Base3D Model DownloadsMembership
中文版Model Store »
Knowledge Base › Electrical Knowledge Base › Configuring the STM32 internal ADC and sampling pitfalls
HomeModels
Table of contents

On this page

Electrical & Electronic Basics (600 articles)

Quick Reference

  • PLC & Programming
  • Electrical & Electronic Basics (monthly)(每月更新中)
  • Electrical Control & Drawings (monthly)(每月更新中)
  • Drives & Actuators (monthly)(每月更新中)
  • Sensors & Detection (monthly)(每月更新中)
  • Industrial Communication & Networks (monthly)(每月更新中)
  • HMI & Host Systems (monthly)(每月更新中)
  • Industrial Robots & Motion Control (monthly)(每月更新中)
  • Mechatronic System Integration (monthly)(每月更新中)
  • Fault Diagnosis & Standards (monthly)(每月更新中)

Configuring the STM32 internal ADC and sampling pitfalls

The STM32 built-in multi-channel ADC works well when configured right and misreads when not. This page covers the clock, channels, sampling time, calibration and DMA essentials.

1. How it works

Clock and resolution: the ADC clock comes from APB2 through a prescaler with a hard ceiling, about 14MHz on F1, 30MHz on F4 and higher on H7; exceed it and accuracy falls. Resolution is selectable at 6, 8, 10 or 12 bits, slower as it rises, and results can be aligned left or right by range. Multiple channels are queued in the regular group or the injected group.

2. Numbers and practice

Sampling time: each channel carries its own sampling time, 1.5 to 239.5 cycles in ADC_SMPR. A high-impedance source, a divider or a sensor, needs a long sampling time so the internal sample capacitor charges fully, or readings sag low. The rule: sampling time times the ADC clock must cover the settling of Rsource with Cin. Too short a time is the number one cause of inaccurate readings on high-impedance signals.

3. Pitfalls and fixes

Calibration and DMA: run the power-on calibration, HAL_ADCEx_Calibration_Start, to remove offset; feed Vref from a stable reference or use the internal VREFINT for ratio measurements. For continuous multi-channel work let DMA move results into memory instead of reading one by one in an interrupt. Input voltage must never exceed Vref, overvoltage latches or damages the pin, so clamp the front end.

4. STM32 ADC configuration points

SettingWatchConsequence
ClockUnder the ceilingAccuracy drops
Sampling timeMatch source impedanceReadings sag low
ResolutionHigher is slowerA trade-off
CalibrationRun at power-upRemoves offset
DMAMove multi-channel resultsEfficiency
OvervoltageNever above VrefDamage

5. Notes

Lengthen the sampling time for high-impedance sources, calibrate at power-up, and use DMA for multiple channels. Clamp the front end so inputs never exceed Vref.

6. References

The quantities and component characteristics discussed here follow current national standards and the corresponding IEC documents: MCU ADC configuration per GB/T 4728 and IEC 60748 series. Always check the device datasheet and the product design file for ratings and derating.

7. Cautions

⚠️ Three common pitfalls:
① Sampling time too short for a high-impedance source:the sample capacitor never fills and readings sag
② Calibration skipped:offset stays and the whole range reads wrong
③ Input above Vref:the ADC latches up or the pin is damaged

Locate the issue with the approach above, then confirm with measurements; safety-related circuits must follow the standards and manufacturer requirements.

Related reading

Related 3D models: Ready to build it? Download free 3D models & CAD drawings at MOULDL »
MOULDL · Electrical Knowledge Base · free self-study reference for electrical & automation engineers, 3D CAD models at MOULDL