← ME-6 · INTERFACING THE REAL WORLD

PWM

YOU WILL BE ABLE TO
  • Define duty cycle and compute a PWM signal's average voltage.
  • Explain why PWM controls power efficiently.
  • Design an RC filter to recover an analogue voltage from PWM.
  • Choose a PWM frequency for a given load.

A microcontroller has no analogue output — its pins are digital, and can produce only two voltages. Pulse-width modulation gets around this by switching fast between them and varying the fraction of time spent high. The duty cycle DD is that fraction, and over any interval long compared with the switching period the average voltage is simply the supply scaled by it.

Vavg=D⋅VsupplyV_{avg} = D \cdot V_{supply}the average of a PWM waveform
timev (V)avg 0.83 Vavg 2.48 V25% duty75% duty
Two duty cycles on a 3.3 V rail: the waveforms are always at a rail, but their averages are not.

Two quite different things are done with this, and confusing them causes real design errors. For loads that respond slowly compared with the switching — LED brightness perceived by the eye, motor speed governed by rotor inertia, a heater's temperature — no filter is needed at all: the load does the averaging itself, and PWM controls power directly. For producing an actual analogue VOLTAGE, a filter is required, and then it is CA-6's low-pass with a cutoff far below the switching frequency.

PWMRCvavg
Recovering an analogue voltage: an RC low-pass averages the PWM. Ripple falls as R, C, and the switching frequency rise.

The residual ripple on the filtered output is what remains of the switching, and a useful approximation for a simple RC is:

Vripple≈D(1−D) Vsupplyfpwm RCV_{ripple} \approx \frac{D(1-D)\,V_{supply}}{f_{pwm}\,RC}peak-to-peak ripple, worst at 50% duty

Note the shape of that result. Ripple is worst at 50%50\% duty and vanishes at either extreme; it falls in proportion to switching frequency, and in proportion to the time constant. So there are three dials, and only one is free: the load usually fixes what RCRC can be (a large RCRC makes the output slow to respond), and the supply is what it is, so raising fpwmf_{pwm} is the usual lever. The RP2040 makes that easy — its PWM blocks divide a 125125 MHz clock through a 16-bit counter, so kilohertz to hundreds of kilohertz is routine.

WORKED EXAMPLE

Filtering PWM into a control voltage

  1. Produce a 00–3.33.3 V analogue control voltage from a 3.33.3 V PWM at 1010 kHz, with under 1010 mV of ripple.
  2. Worst case is 50%50\% duty: D(1−D)=0.25D(1-D) = 0.25.
  3. Required: 0.25×3.3/(104×RC)≤0.010⇒RC≥0.008250.25 \times 3.3/(10^4 \times RC) \le 0.010 \Rightarrow RC \ge 0.00825 s.
  4. Choose R=10R = 10 kΩ, C=1C = 1 µF: RC=10RC = 10 ms, giving about 88 mV of ripple. ✓
  5. Cost of that choice: the output takes about 5RC=505RC = 50 ms to settle after a duty change (CA-5's five-time-constant rule). Fine for a setpoint, useless for audio.
R=10R = 10 kΩ, C=1C = 1 µF — 8 mV ripple, 50 ms settling.
WORKED EXAMPLE

Choosing a frequency for a motor

  1. A small DC motor is PWM-driven. Below about 100100 Hz the motor's torque pulses audibly and mechanically.
  2. In the audio band the switching is heard as a whine — commonly the most complained-about property of cheap motorised products.
  3. Above about 2020 kHz the whine is inaudible, but the driver's switching losses rise (each transition costs energy, ME-5's 12Cv2\tfrac{1}{2}Cv^2 again) and the MOSFET's gate drive matters more.
  4. Typical choice: 2020–2525 kHz — just above hearing, below where losses become troublesome.
Just above the audio band, for reasons that are half acoustic and half thermal.

PRACTICE

Q1

A 3.33.3 V PWM signal has a duty cycle of 30%30\%. What is its average voltage?

Q2

What duty cycle produces 2.02.0 V from a 55 V supply?

Q3

An LED is dimmed to half brightness two ways: by PWM at 50%50\% duty, and by a transistor in its linear region dropping 22 V while the LED takes 1010 mA. Compare the heat dissipated in the controlling device.

Q4

A filtered PWM output has too much ripple. Name the three quantities you could change, and state the practical cost of each.

PWM · ME-6 · iLearnSTEM Electronics