C7 · Lesson 30 · PWM and sigma-delta outputs
Making an analog level from a counter, a comparator or an accumulator, and how fast its ripple dies
Digital chips have plenty of pins that can only be 0 or 1, yet they dim LEDs smoothly, set motor speeds and even play audio. The trick is to switch fast and let something slow average it: an RC filter, a motor's inductance, an eye. A counter and a comparator from lesson 29 make pulse-width modulation, whose average is the fraction of each period spent high. An adder and a register make a sigma-delta bitstream with the same average but its 1s spread out as evenly as possible, which pushes the ripple to much higher frequencies where a filter removes it easily.
- A first-order sigma-delta modulator built from a 3-bit accumulator. On each clock it adds the input X (0 to 7) to the accumulator, and the carry out of that addition is the output bit.
- With X = 2 (binary 010), the output is 1 on one clock in every four: an average of 2/8. Change X and watch the density of 1s follow it, always as evenly spread as the clock allows.
Predict
An 8-bit PWM and an 8-bit sigma-delta modulator both run from a 16 MHz clock with an input of 64 (a quarter of full scale), and both feed the same RC filter. How do their ripples compare?
The PWM repeats every 256 clocks: one 64-clock pulse per period, at 62.5 kHz. The sigma-delta puts out a 1 every 4 clocks, a 4 MHz pattern. Through an RC filter much slower than both, ripple scales with the pattern's period, so the sigma-delta's is 256/4 = 64 times smaller. Near codes that don't divide evenly, its pattern is longer and the advantage shrinks, but it never does worse than the PWM.
Name it
Pulse-width modulation
An -bit counter counts from 0 to and wraps; a comparator sets the output high while the count is below the input code . The duty cycle is , the frequency is
and the average output is , with steps of . Resolution and frequency trade off: each extra bit halves the PWM frequency.
Filtering and ripple
An RC low-pass with passes the average and shrinks the ripple. During the high time , the capacitor charges with current about , so the peak-to-peak ripple is
largest at 50% duty. Higher frequency or a slower filter reduces it; a slower filter also slows the response to a new code, with time constant .
First-order sigma-delta
Replace the counter with an accumulator: every clock, add to an -bit register and output the carry. The carries come at an average rate , the same as the PWM's duty, but spread as evenly as possible: the 1s and 0s alternate as often as the code allows. For , the pattern repeats every clocks, so the ripple is times the PWM's. This is the same idea as the delta-sigma converters of signal processing, here in its simplest form: the accumulator keeps the running error, and the output corrects it on every clock.
The catch: many more edges per second, which cost switching power and, on a long trace or a driver with slow edges, add error from unequal rise and fall times. PWM is usually preferred for power stages, sigma-delta for references and audio.
Worked example
A 16 MHz microcontroller makes PWM with an 8-bit and a 10-bit counter. What are the PWM frequency and voltage step for each on 3.3 V?
Show the solution
- 8-bit counter. The PWM frequency and the voltage step are
- 10-bit counter.
- The trade-off. Four times the resolution costs four times the period, and so, for the same filter, four times the ripple.
Answer 62.5 kHz with 12.9 mV steps for 8 bits, and 15.6 kHz with 3.2 mV steps for 10 bits.
Worked example
The 8-bit PWM (3.3 V, 62.5 kHz) feeds an RC filter with = 1 ms. Find the worst-case ripple, and the time constant needed to bring it under one LSB.
Show the solution
- Worst-case ripple. At = 0.5, with = 16 µs: about one LSB (12.9 mV) already.
- A quarter LSB. Getting comfortably below, say to a quarter LSB, needs of about 4 ms.
- The cost. That makes the output take about = 20 ms to settle after a change, the PWM digital-to-analog converter's basic trade-off.
Answer about 13.2 mV, one LSB; a quarter LSB needs of about 4 ms, which takes about 20 ms to settle.
Worked example
An 8-bit sigma-delta modulator at 16 MHz is given = 64 and feeds the same 1 ms filter. What pattern does it produce, and what ripple?
Show the solution
- The pattern. Adding 64 each clock to an 8-bit accumulator carries every fourth clock: 0001 repeating, a 4 MHz pattern whose high time is one clock, 62.5 ns.
- Its ripple.
- The PWM at the same code.
- Compare. The sigma-delta ripple is 64 times less, so the filter could be 64 times faster for the same ripple.
Answer 0001 repeating at 4 MHz, with about 0.155 mV of ripple, 64 times less than the PWM's 9.9 mV.
Check
A PWM generator runs its 8-bit counter from a 16 MHz clock. What is the PWM frequency, in kHz?
Write units the way you normally would: 1.5 kHz, 1500 Hz and 0.0015 MHz are the same answer.
Check
A 3.3 V PWM at 62.5 kHz and duty 0.2 drives an RC filter with = 10 ms. What's the peak-to-peak ripple, in mV?
Write units the way you normally would: 1.5 mV, 1500 µV and 0.0015 V are the same answer.
Check
A 3.3 V, 8-bit PWM from a 64 MHz clock must have at most 1 LSB of ripple at its worst duty. What filter time constant does it need?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
A 10-bit first-order sigma-delta should output a 1 once every 4 clocks. What input code does that?
Check
For an 8-bit code , by what factor is the sigma-delta's ripple smaller than the PWM's through the same slow filter? (Give the ratio of sigma-delta to PWM.)
Check
A designer uses a sigma-delta output to drive the gate of a large power MOSFET switching a motor. Why might PWM be the better choice there?
Check
Derive the PWM ripple through an RC filter with .
Step 1
The capacitor sits near the average . What current flows into it while the output is high?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
It flows for . How much does the capacitor's voltage rise?
Check
Simulate both modulators through an RC filter. With an N-bit counter (6) and code k = 5, build 64 periods' worth of bits ( clocks) for PWM (1 while count < k) and for the sigma-delta (carry of an N-bit accumulator starting at 0). Filter each with the exact one-pole update v = v + (b - v)*a per clock, a = 1 - exp(-1/50) (RC = 50 clocks), starting from v = k/2**N. Over the last two PWM periods, set pwm_pp and sd_pp to the peak-to-peak values, and ratio = sd_pp/pwm_pp.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does a sigma-delta bitstream have less ripple after a filter than PWM at the same average and clock rate, even though both contain the same number of 1s per clocks?
A low-pass filter removes high frequencies better than low ones, so what matters is where each bitstream puts its fluctuation. PWM gathers all the 1s of a period into one pulse, so its fluctuation repeats once per period, at , the lowest frequency available, and the filter's capacitor charges and discharges over a whole long pulse. The sigma-delta interleaves the 1s with the 0s as evenly as possible, so its pattern repeats much faster, often every few clocks, and the capacitor only moves a little before the bitstream reverses. Same average, same energy at the input, but the fluctuation sits at higher frequency, where the filter attenuates it more.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.