# ADC sampling in a digitally controlled converter: when, how fast, how many bits

A digital controller never sees the converter. It sees a list of numbers: one reading of the current or the voltage per sampling period, rounded to a whole number of ADC codes, and a little late. Everything the loop does, it does with those numbers. If the readings are wrong, the loop regulates the wrong thing, perfectly.

This post walks through the three questions every firmware engineer meets when a converter goes digital:

- **When** in the switching period to take the sample.
- **How fast** to sample, and what happens when the sampling and the switching drift apart.
- **How many bits** the ADC needs, and what the delay of the whole chain costs the loop.

Every waveform below is a Voltecx run of a real converter: the 12 V to 3.3 V, 5 A, 200 kHz buck from the [PI compensator post](https://voltecx-web.nguyenvanqui291.workers.dev/blog/buck-converter-pi-compensator-design) (L = 8.2 µH, C = 100 µF), with zero-order holds and ADC blocks reading it.

## The picture to keep in mind: a camera with a slow shutter

Think of the ADC as a camera that takes one photo per period. A **sample-and-hold** opens the shutter for a moment, freezes what it sees, and holds that picture until the next one. The converter keeps moving in between; the controller only knows the last photo.

Two things follow from that picture:

1. **The moment you press the shutter matters.** The inductor current is a triangle. A photo at the bottom of the triangle and a photo in the middle of a ramp show two different currents, even though the converter is the same.
2. **The photos are a bit old.** By the time the controller has read the photo, computed a new duty cycle and the PWM timer has used it, the converter has moved on. That age is a delay, and a delay in a feedback loop eats phase margin.

The ADC adds a third effect on top: it does not record the exact value but the nearest of $2^N$ levels. That is **quantisation**.

## Quantisation: the staircase

An $N$-bit ADC over a full-scale range $V_{FS}$ divides the range into $2^N$ steps of

$$\text{LSB} = \frac{V_{FS}}{2^N}$$

Every reading is the input rounded to the nearest step. The error is at most half a step either way, $\pm \tfrac{1}{2}\,\text{LSB}$, and for a signal that moves across many steps it behaves like noise with an RMS value of $\text{LSB}/\sqrt{12}$ (the classical quantisation-noise result, see for example Oppenheim and Schafer, *Discrete-Time Signal Processing*).

To see the staircase, feed a slow ramp from 0 to 4 V into a deliberately coarse ADC: 4 bits over 4 V, so $\text{LSB} = 4/16 = 0.25$ V.

![A ramp from 0 to 4 V through a 4-bit ADC over 4 V in Voltecx: the output steps by 0.25 V and the error (lower panel) is a sawtooth within ±125 mV, until the input passes the top code of 3.75 V.](https://voltecx-web.nguyenvanqui291.workers.dev/media/blog/adc-sampling-power-converters/staircase.svg)

Three things to read from the figure:

- Every step is exactly 0.25 V.
- The error is a sawtooth between −125 mV and +125 mV: half an LSB.
- The highest code is **3.75 V, not 4 V**. An $N$-bit ADC has codes $0 \dots 2^N - 1$, so its top reading is $V_{FS} - \text{LSB}$. Above that the ADC clips, and the error grows without limit. A sensor that runs into the top of the range looks like a converter whose current has stopped rising.

### How many bits do you need?

Start from the regulation you have to meet, not from the ADC you happen to have. A practical rule: one LSB should be several times smaller than the error band you must hold, because the loop can only regulate to within about one code, and offset, gain error and noise take their share too.

The buck's output is 3.3 V with 10.9 mV of ripple. Read it with two ADCs, both over 0 to 4 V:

| ADC | LSB | LSB as % of 3.3 V | Ripple in codes |
|---|---|---|---|
| 8 bits | 15.6 mV | 0.47 % | less than 1 |
| 12 bits | 0.98 mV | 0.03 % | about 11 |

![The buck output in Voltecx, v(out), and the same voltage through an 8-bit and a 12-bit ADC over 4 V. The 8-bit reading toggles between two codes, 3.281 V and 3.297 V; the 12-bit reading follows the ripple in 0.98 mV steps.](https://voltecx-web.nguyenvanqui291.workers.dev/media/blog/adc-sampling-power-converters/quantised-output.svg)

The 8-bit converter can see that the output is "about 3.29 V", nothing finer: it toggles between 3.281 V and 3.297 V. For a ±1 % regulation spec (±33 mV) that is already tight, because one code is half of the whole budget on each side. The 12-bit converter resolves 0.03 %: the bits are no longer the limit, the divider resistors and the voltage reference are.

There is a second reason to want more bits than the regulation needs: the **digital PWM**. If one step of the duty cycle moves the output by more than one ADC code, the loop can never settle. It keeps jumping between two duty steps on either side of the target, a *limit cycle*. The usual condition (Peterchev and Sanders, IEEE Transactions on Power Electronics, 2003) is that the DPWM must be finer than the ADC, with an integrator in the loop. A buck with $V_{in} = 12$ V and a PWM timer of 1000 counts moves the output by $12 \times 0.1\,\% = 12$ mV per duty step: more than one code of the 12-bit ADC above. Either the timer needs more counts, or the PWM needs a high-resolution mode.

## When to sample: the middle of the on-time

Now the timing. The inductor current of the buck is a triangle of $\Delta I = 1.46$ A peak to peak around its average of 4.99 A. For a current loop you nearly always want the **average** current, not whatever point of the ripple the ADC happens to land on.

For a triangle, the average sits exactly halfway along each ramp. So if you sample in the **middle of the on-time** (or the middle of the off-time), you read the average directly, without any filtering. In a center-aligned (triangle carrier) PWM, the middle of the on-time is the peak or the valley of the carrier, which is why microcontrollers let you trigger the ADC there.

![Three switching periods of the buck in Voltecx: the inductor current iL, the sample taken at the middle of the on-time (i_mid) and the sample taken at the start of the period (i_valley).](https://voltecx-web.nguyenvanqui291.workers.dev/media/blog/adc-sampling-power-converters/synchronous-sampling.svg)

The simulation shows the difference plainly:

- **Sample at the middle of the on-time** (0.69 µs after the switch turns on, half of $D T_s = 0.275 \times 5$ µs): the reading is 4.99 A, equal to the true average within 0.1 %, and it does not move from one period to the next.
- **Sample at the start of the period**, when the switch turns on: the reading is 4.26 A, the valley of the ripple. It is steady too, but it is **0.73 A low**, exactly $\Delta I / 2$. A current loop built on it would push the average 0.73 A above its reference.

Both are *synchronous*: the sample happens at the same point of every period, so the reading is constant in steady state. That is what you want. A synchronised sample at the wrong point gives a steady offset; an unsynchronised sample gives something worse.

### Practical points about the sampling instant

- **Avoid the edges.** Right after a switch turns on or off, the current and the sense signal ring. Sampling in the middle of a ramp keeps away from both edges.
- **Account for the sense path delay.** An isolated amplifier or a filter shifts the signal by a microsecond or two. Move the trigger later by the same amount.
- **Narrow pulses.** At very small or very large duty cycles the middle of the on-time is close to an edge. Many designs switch to sampling the middle of the off-time there.
- **DCM and boost input current.** When the current waveform is not a symmetric triangle (discontinuous mode, or the input current of a boost with a large ripple), the midpoint is no longer the average. Averaging several samples over the period helps (see oversampling below).

## How fast: aliasing and the slow beat

Suppose the ADC is triggered by its own timer at 195 kHz instead of by the PWM at 200 kHz. The two clocks are only 2.5 % apart. What does the controller see?

Each sample lands a little later in the switching period than the one before. After 40 samples the sample point has walked through the whole triangle and comes back to where it started. The reading traces out the ripple in slow motion:

$$f_{beat} = |f_{sw} - f_s| = |200\ \text{kHz} - 195\ \text{kHz}| = 5\ \text{kHz}$$

This is **aliasing**. The sampling theorem (Nyquist–Shannon) says a sampled signal can only represent frequencies below $f_s / 2$. The 200 kHz ripple is far above 97.5 kHz, so it does not disappear: it folds down and shows up at the difference frequency, 5 kHz. And 5 kHz is right where a voltage or current loop has its crossover.

![Upper panel: the inductor current sampled at 195 kHz (i_async) wanders from 4.26 A to 5.71 A and repeats every 200 µs, while the synchronous sample (i_mid) stays at 4.99 A. Lower panel: the same asynchronous sample taken behind a 20 kHz first-order filter.](https://voltecx-web.nguyenvanqui291.workers.dev/media/blog/adc-sampling-power-converters/aliasing-beat.svg)

The upper panel is the Voltecx run: the asynchronous reading sweeps the full ripple, 4.26 A to 5.71 A, and repeats every 200 µs. To the controller it looks like a real 5 kHz oscillation of 1.45 A in the current, and it will try to correct it, injecting a 5 kHz disturbance into the duty cycle. Nothing in the power stage is oscillating; the measurement made it up.

This is why sampling is nearly always **synchronised to the PWM**: one sample per period (or two, at the carrier peak and valley), triggered by the PWM timer itself, so the ripple aliases to exactly zero frequency, a constant that the right sampling instant makes equal to the average.

## The anti-alias filter, and what it costs

When the sampling cannot be synchronised (a voltage sensed by a slow ADC sequence, a second converter on its own clock, a noisy signal with spikes), the defence is an **anti-alias filter**: usually a resistor and a capacitor in front of the ADC pin. It attenuates the ripple before it reaches the sampler, so there is less to fold down.

The lower panel above is the same 195 kHz sample behind a first-order low-pass at $f_p = 20$ kHz. The beat is still there, but it is **0.114 A** instead of 1.45 A, about 13 times smaller, and the average is still 4.99 A. A first-order filter attenuates the 200 kHz fundamental of the ripple by about $f_{sw}/f_p = 10$, and the triangle's harmonics by more.

The cost is phase. A first-order pole at $f_p$ delays every signal in the loop, and at the crossover $f_c$ it takes

$$\varphi_{RC} = \arctan\frac{f_c}{f_p}$$

For a loop crossing at 5.78 kHz, the 20 kHz pole costs $\arctan(5.78/20) = 16.1°$ of phase margin. So the filter corner is a compromise:

- **High enough** above the crossover that its lag is small (a factor of 3 to 5 is common).
- **Low enough** below $f_s / 2$ that it attenuates what would alias.

With a crossover of $f_s/10$ the window between the two is narrow, and that is why synchronised sampling, which needs no filter for the ripple, is the default. Voltecx's `design.senseNetwork` puts the pole in the middle of that window on a log scale, $\sqrt{f_c \cdot f_s/2}$ by default, and reports its lag at the crossover so you can add it to the loop budget. The [sensing post](https://voltecx-web.nguyenvanqui291.workers.dev/blog/current-voltage-sensing-shunt-hall-divider) sizes one.

## Sampling delay and phase margin

Even with perfect sampling, a digital loop is late. In the usual timing:

1. the ADC samples at the start of a period,
2. the interrupt computes the new duty during that period,
3. the PWM uses it from the next period on,
4. and the PWM itself, seen as a sampler, adds about half a period.

That is about 1.5 sampling periods from sample to effect. A pure delay $T_d$ does not change the gain, but it shifts the phase by

$$\varphi_d = 360° \cdot f_c \cdot T_d$$

at the crossover (see Corradini, Maksimović, Mattavelli and Zane, *Digital Control of High-Frequency Switched-Mode Power Converters*, for the full treatment). Every microsecond of delay costs more at a higher crossover.

To put numbers on it, take the closed-loop buck example in Voltecx (48 V to 12 V, 100 kHz) with the Type III voltage loop that `design.voltageLoop` designs: crossover 5.78 kHz, 60° of phase margin, planned with half a sample (5 µs) of PWM delay. `design.digitalize` then recomputes the loop for each total delay.

![Phase margin and gain margin of the digitalised buck loop in Voltecx against the total delay from sample to duty, from the 5 µs the design planned for to 25 µs. The horizontal lines mark 45° and 6 dB.](https://voltecx-web.nguyenvanqui291.workers.dev/media/blog/adc-sampling-power-converters/phase-margin-vs-delay.svg)

| Total delay | Samples at 100 kHz | Phase margin | Gain margin |
|---|---|---|---|
| 5 µs | 0.5 | 60.0° | 12.2 dB |
| 10 µs | 1.0 | 49.6° | 8.9 dB |
| 15 µs | 1.5 | 39.2° | 6.4 dB |
| 25 µs | 2.5 | 18.4° | 2.6 dB |

Each extra 5 µs costs $360° \times 5.78\,\text{kHz} \times 5\,\mu\text{s} = 10.4°$, exactly as the formula says. The usual "sample, compute, update next period" timing (15 µs) leaves 39°: a loop that works but rings. The margin drops below 45° at 12.2 µs. Ways to win the phase back:

- **Update the duty as soon as the computation ends**, not at the next period. If the interrupt takes 30 % of the period, the delay drops by most of a period.
- **Sample later in the period**, as close as possible to the moment the result is needed, keeping the middle-of-ramp rule.
- **Design the analog compensator with the delay in it** from the start, instead of discovering the loss afterwards.
- **Lower the crossover.** It is the product $f_c T_d$ that matters.

## Oversampling and averaging

Sometimes one sample per period is not enough: the waveform is not a symmetric triangle, or the signal is noisy. Then take several samples per period and average them.

- **The average of $N$ samples spread evenly over one period** is the period's average current for any waveform shape, which fixes the DCM and boost-input cases above.
- **Averaging reduces noise.** Uncorrelated noise falls by $\sqrt{N}$, so 16 samples gain a factor of 4, which is 2 bits. This only works when the noise is at least about one LSB (it *dithers* the input across codes); a perfectly clean signal averaged 16 times gives the same code 16 times.
- **Averaging is a filter, and filters are late.** A moving average over one period behaves roughly like half a period of extra delay. Count it in the phase budget like any other delay.

## A checklist

1. Choose the full scale with headroom: the highest signal (including start-up overshoot and fault current) should stay below the top code.
2. Make one LSB several times smaller than the regulation band, and the DPWM step smaller than one LSB.
3. Trigger the ADC from the PWM timer, in the middle of the on-time or off-time, away from the edges.
4. If a signal cannot be sampled synchronously, filter it with a pole between the crossover and $f_s/2$ and count its lag.
5. Add up the delays (sense path, filter, conversion, computation, PWM) and check the phase margin with all of them.

## Try it in Voltecx

- The **Zero-Order Hold** block (`zoh`) samples at `ts` with a start `offset`, so `offset = D/(2·fsw)` gives the middle of the on-time. The **Sample and Hold** block (`sampleHold`) can also be clocked from a signal. The **ADC** block (`adc`) rounds to `bits` over `range`, starting at `min`, and samples at `fs` when it is set.
- Put an ammeter (a current meter) in series with the inductor to give these blocks a signal, as in the figures above.
- `design.digitalize` takes the analog loop design, discretises the compensator, adds the compute delay, the ADC bits and the PWM resolution, and reports the phase margin lost and the limit-cycle check. The [digital control tutorial](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/07-digital-control/) walks through it on the closed-loop buck.
- `design.senseNetwork` sizes the divider and the anti-alias capacitor in front of an STM32G474 ADC input, and `tools.codeGeneration` writes the C code of the control blocks.
