PWM
Pulse-width modulation controls average power by switching fully on and off with a variable duty cycle. Learn duty, frequency and resolution, carrier-based PWM, filtering, dead time, and how to choose PWM frequency for LEDs, motors and converters.
Instead of dissipating power in a resistor or linear transistor, PWM switches a load fully on and fully off very quickly. The fraction of time it's on — the duty cycle — sets the average voltage, and the load or a filter smooths the pulses.
PWM is everywhere: LED dimming, motor speed control, every DC–DC converter and inverter, servo signals and class-D audio amplifiers.
An analogy
PWM is like controlling a room's brightness by flicking the light switch on and off very fast: on 25 % of the time looks like quarter brightness, because your eye averages it.
Duty cycle and average
With a 12 V supply, 25 % duty gives 3 V average, 50 % gives 6 V, 100 % gives 12 V. Because the switch is either fully on (tiny voltage drop) or fully off (no current), it dissipates very little power.
Generating PWM
Microcontroller timers count up to a period value and toggle an output when the count passes a compare value. Analogue PWM compares a control voltage with a triangle or sawtooth carrier; the comparator output is the PWM signal.
Choosing the frequency
Higher frequency means smoother output and smaller filters, but more switching loss and lower resolution.
- LEDs: > 200 Hz to avoid visible flicker, several kHz to avoid camera banding.
- Motors: > 16–20 kHz to be inaudible.
- DC–DC converters: 100 kHz – several MHz.
- Hobby servos: 50 Hz frame with 1–2 ms pulse encoding position (not power control).
Resolution trade-off
A 72 MHz timer at 20 kHz has 3600 steps (≈ 11.8 bits); at 200 kHz only 360 steps (≈ 8.5 bits). Center-aligned (up-down) counting halves the counts again but produces symmetric pulses with lower harmonics — preferred for motor control.
Complementary PWM and dead time
Half-bridges drive high and low switches with complementary signals separated by dead time. Advanced timers generate these pairs in hardware, with break inputs that shut everything down within nanoseconds on a fault.
Spectrum and dithering
PWM energy sits at the switching frequency and its harmonics, which is what filters and EMC measures target. Spread-spectrum (dithered frequency) PWM smears that energy to lower peak emissions, and sigma-delta or dithered PWM increases effective resolution.
Formulas
- Average output:
V_avg = D · V_supply - Counts per period (edge-aligned):
N = f_clk / f_PWM - Resolution:
bits = log₂(N) - RC filter cut-off:
f_c = 1/(2πRC) (≪ f_PWM)
Worked example: Timer resolution
Given: f_clk = 72 MHz; f_PWM = 20 kHz, edge-aligned
Find: Counts per period and resolution.
- N = 72e6/20e3 = 3600
- bits = log₂ 3600 = 11.8
Answer: 3600 steps ≈ 11.8 bits
Worked example: LED dimming
Given: 12 V LED strip; D = 25 %
Find: Average voltage and (for a resistive model) power fraction.
- V_avg = 0.25 × 12 = 3 V
- Power fraction = D = 25 % (the LED is either fully on or off)
Answer: 3 V average, 25 % brightness
Common mistakes
- Wrong: Using a 490 Hz Arduino PWM for a motor and wondering about the whine. — Right: Move PWM above ~20 kHz for inaudible motor drive (with appropriate gate drive).
- Wrong: Filtering PWM with an RC whose cut-off is near f_PWM. — Right: Place the cut-off at least 10× (preferably 100×) below f_PWM, or use a higher-order filter.
Applications
- Motor speed control: A PWM-driven H-bridge sets DC motor speed; three-phase PWM drives BLDC and induction motors.
- Class-D audio: Audio is encoded as PWM at hundreds of kHz and filtered at the speaker, reaching 90 %+ efficiency.
- Heaters & fans: Low-frequency PWM controls heater power and 4-wire PC fans (25 kHz PWM input).
Key takeaways
- V_avg = D × V; switches are fully on or off, so losses are low.
- Resolution = log₂(f_clk/f_PWM) — higher frequency costs bits.
- LEDs > 200 Hz, motors > 20 kHz, converters 100 kHz+.
- Complementary PWM with dead time drives half-bridges.
Going further
- High-resolution PWM: HRPWM peripherals use delay lines to place edges with ~100–200 ps resolution, giving fine duty control at MHz frequencies for digital power supplies.
- Current sampling alignment: Sampling phase currents at the PWM center, when no switching is happening, avoids ringing noise; timers trigger the ADC automatically.
- Minimum pulse width: Gate drivers and dead time impose minimum on/off times, limiting the usable duty range near 0 % and 100 % — solved with pulse skipping or bootstrap-refresh strategies.