Topics / ADC
Topic

ADC Analog-to-digital converters

Measure a voltage on a pin and get a number: 0 V reads as 0, the reference voltage as full scale.

Learn first:RCCGPIOPractise:Read an analog voltage
What it is

The ADC samples the voltage on an input, compares it with the reference voltage and returns a number of 16, 14, 12, 10 or 8 bits. At 16 bits, 0 V gives 0 and VREF+ gives 65535. Every input is a channel (INP0, INP1, … and INN0 for the negative side of a differential pair), and a sequence lists which channels to convert and in what order. Before each conversion the ADC charges a small capacitor from the pin for a set time, the sampling time.

The H7 ADC does not work out of the box, and each trap has its own register bit: the converter has its own kernel clock that is off after reset, it starts in deep power-down, it needs a calibration, and every channel must be preselected. The converter can also be started by a timer trigger and write its results to memory with DMA, which is how you sample at an exact rate without CPU work.

You need it for
  • Reading a potentiometer, a light sensor, a battery divider or any analog sensor on the Arduino A0 to A5 pins
  • Sampling at an exact rate: a timer trigger starts each conversion, and DMA stores the results
  • Measuring the chip itself: internal temperature sensor, VBAT and the internal reference VREFINT
  • Watching a level without the CPU: the analog watchdog raises an interrupt when a value leaves a window

Read in this order

The shortest path through the documents. Each link opens the exact page; the line under it says what to look for.

  1. RM043325.2 ADC main featuresp.913One page: resolution, channels, triggers, oversampler, watchdogs
  2. RM043325.4.3 ADC clocksp.917The two clock options (own kernel clock or AHB clock) and the BOOST rule
  3. RM043325.4.6 ADC deep-power-down mode (DEEPPWD) and ADC voltage regulator (ADVREGEN)p.923DEEPPWD and ADVREGEN: how to wake the ADC up, and the wait that follows
  4. RM043325.4.8 Calibration (ADCAL, ADCALDIF, ADCALLIN, ADC_CALFACT)p.924ADCAL: the calibration procedure, which must run with the ADC disabled
  5. RM043325.4.12 Channel preselection register (ADC_PCSEL)p.932Why every converted channel needs its ADC_PCSEL bit first
  6. RM043325.4.13 Channel-wise programmable sampling time (SMPR1, SMPR2)p.933The eight sampling times, set per channel in ADC_SMPR1 and ADC_SMPR2
  7. RM043325.4.19 Conversion on external trigger and trigger polarity (EXTSEL, EXTEN, JEXTSEL, JEXTEN)p.938Table of triggers: timer outputs such as tim3_trgo start a conversion by hardware
  8. RM043325.4.7 Single-ended and differential input channelsp.924INP only, or INP minus INN, and how the result is computed

Registers to learn first

In the order you usually program them. The other 24 registers of the chapter can wait until you need them.

1RCC_AHB1ENRADC12EN: bus clock of ADC1 and ADC2 (ADC3 has its own enable bit in another register)p.453
2RCC_D3CCIPRADCSEL: kernel clock of the ADCs. The reset value is PLL2_P, which is off, so pick another sourcep.417
3ADCx_CCRShared by ADC1 and ADC2: clock mode (CKMODE) and the divider PRESCp.1035
4ADC_CRPower and run control: DEEPPWD, ADVREGEN, ADCAL, ADEN, ADSTART and BOOSTp.1001
5ADC_PCSELOne bit per input channel; a channel must be set here before it is convertedp.932
6ADC_SQR1The conversion sequence: how many channels (L) and the first ones in orderp.1020
7ADC_SMPR2Sampling time of channels 10 to 18 (ADC_SMPR1 covers channels 0 to 9)p.1017
8ADC_CFGRResolution (RES), continuous mode, trigger source and polarity, DMA modep.1010
9ADC_DRThe conversion result. Reading it clears the end-of-conversion flagp.1024
All 32 registers in Analog-to-digital converters (ADC)

On this board

NetPinSignalNote
ARD_A0PC0analog ADC123_INP10Arduino A0 = PC0, ADC123_INP10: on ADC1, ADC2 and ADC3. The pin the ADC guide measures
ARD_A1PF8analog ADC3_INN3, analog ADC3_INP7Arduino A1 = PF8, ADC3 only (INP7, INN3)
ARD_A2PA0_Canalog ADC12_INN1, analog ADC12_INP0Arduino A2 = the PA0_C pad, ADC12_INP0. A separate analog pad next to PA0; the SYSCFG_PMCR bit PA0SO controls the switch between the two
ARD_A3PA1_Canalog ADC12_INP1Arduino A3 = the PA1_C pad, ADC12_INP1. Same kind of analog pad (switch bit PA1SO)
ARD_A4PC2_Canalog ADC3_INN1, analog ADC3_INP0Arduino A4 = the PC2_C pad, ADC3_INP0. Analog pad (switch bit PC2SO)
ARD_A5PC3_Canalog ADC3_INP1Arduino A5 = the PC3_C pad, ADC3_INP1. Analog pad (switch bit PC3SO)

Header pins you can wire to

Arduino and STMod+ header pins whose MCU pin can carry a ADC signal (from the datasheet AF tables).

On this chip

5 instances found in the datasheet’s alternate-function tables, with every pin that can carry each signal.

InstanceSignals and the pins that can carry them
ADC1INN2PF12INN16PA1INP2PF11INP6PF12INP16PA0INP17PA1
ADC2INN2PF14INP2PF13INP6PF14
ADC3INN1PC2_CINN2PF10INN3PF8INN4PF6INN5PF4INN13PH3INN14PH4INN15PH5INP0PC2_CINP1PC3_CINP2PF9INP3PF7INP4PF5INP5PF3INP6PF10INP7PF8INP8PF6INP9PF4INP13PH2INP14PH3INP15PH4INP16PH5
ADC12INN1PA0_CINN3PA7INN4PC5INN5PB0INN12PC3INN18PA5INP0PA0_CINP1PA1_CINP3PA6INP4PC4INP5PB1INP7PA7INP8PC5INP9PB0INP13PC3INP14PA2INP15PA3INP18PA4INP19PA5
ADC123INN10PC1INN11PC2INP10PC0INP11PC1INP12PC2

Map of the chapter

Every reference-manual chapter has the same shape. Sections marked ● are in the reading order above.

Common mistakes

  • No clock after reset. ADCSEL in RCC_D3CCIPR resets to PLL2_P, and PLL2 is off. Select another kernel clock or the ADC never starts.
  • Starting a conversion while the ADC is still in deep power-down. After reset DEEPPWD is 1 (reset value of ADC_CR is 0x2000 0000). Clear it, set ADVREGEN, wait for the regulator, then calibrate with ADCAL before ADEN.
  • Forgetting ADC_PCSEL. The channel in the sequence must also have its preselection bit set, or the result is not guaranteed.
  • A wrong BOOST setting. It must match the ADC clock, and the bit layout differs between silicon revisions V and Y, so check the ADC_CR entry for your chip.
  • Trusting 3.3 V as VREF+. On this board VREF+ is tied to the analog supply VDDA through R66 (0 Ω, schematic sheet 4), so supply noise appears in every reading. Measure it for accurate work.

Where to go next