EN

PWM calculator

Duty cycle, microcontroller timer and RC filter to get a DC voltage

1. The PWM signal

Signal
Hz
%
V
Ω

2. Microcontroller timer

Timer
MHz

Uses the frequency and duty cycle from section 1. It looks for the smallest prescaler (1, 2, 4… 1024) that allows that frequency, because it gives the highest resolution. Each manufacturer has its own prescalers and register names: check your microcontroller's reference manual.

Maximum frequency for each resolution

3. RC filter: from PWM to DC voltage

Low-pass filter
kΩ
µF
mV

What PWM is

Pulse width modulation (PWM) means switching an output on and off at a fixed frequency while varying the fraction of time it is on. That fraction is the duty cycle D. If switching is fast enough for the load, the load only sees the average value: a motor runs slower, an LED dims, a resistor heats less. Since the transistor is always fully on or fully off, it dissipates very little power, unlike a linear regulator.

\[ D = \frac{t_{on}}{T} \qquad V_{\text{avg}} = D\,V \qquad V_{\text{rms}} = V\sqrt{D} \qquad P = \frac{V^2}{R}\,D \]

Mind the difference between average and RMS value: a DC motor responds to the average, but the heating of a resistor depends on the RMS. With D = 25 % the average voltage is a quarter, but the RMS voltage is half.

Which frequency to choose

ApplicationTypical frequencyWhy
LED brightness500 Hz to 2 kHz or moreAbove about 100 Hz flicker is not visible; higher if cameras are filming
Small DC motors15 to 25 kHzAbove human hearing, to avoid the whine
Heating, solid state relays0.1 to 10 HzThermal inertia is huge; a zero-crossing SSR needs long periods
Hobby servos50 HzThe angle is set by the pulse width (1 to 2 ms), not by the duty cycle
Switch-mode power supplies100 kHz to 2 MHzSmaller inductors and capacitors

Resolution versus frequency

A timer counts pulses of its clock from 0 up to TOP and starts again; the output changes when the counter reaches the compare value. The number of possible duty cycle steps is TOP + 1, so resolution and frequency are linked:

\[ f_{PWM} = \frac{f_{clk}}{N\,(TOP+1)} \qquad \text{resolution} = \log_2(TOP+1)\ \text{bits} \]

where N is the prescaler. At 16 MHz and 8 bits the maximum frequency is 62.5 kHz; with 16 bits it drops to 244 Hz. In phase-correct mode the counter counts up and down, the frequency halves but the pulses stay centred, which is preferred for motors and H-bridges. If you need more resolution at high frequency, some microcontrollers have high-resolution timers or a timer clock faster than the CPU clock.

RC filter: turning PWM into a voltage

An RC low-pass filter passes the average value and attenuates the switching: it is a cheap digital-to-analog converter. R and C must satisfy two opposite conditions. For low ripple, the cut-off frequency must be much lower than the PWM frequency (100 times lower gives small ripple); but the lower it is, the longer the output takes to settle after a change (about 4.6 time constants to reach 1 %).

\[ f_c = \frac{1}{2\pi R C} \qquad \Delta V_{pp} = V\,\frac{(1-e^{-t_{on}/\tau})(1-e^{-t_{off}/\tau})}{1-e^{-T/\tau}} \qquad \tau = RC \]

Ripple is highest at D = 50 %. That is why a high PWM frequency is worthwhile, even at the cost of resolution. For less ripple with the same speed, use two RC stages in cascade (second order) or an active filter. The filter output cannot drive a load: if it has to feed something, add an op-amp as a voltage follower. If you need accuracy, a real DAC is a better choice: see ADC and DAC conversion.

Related: transistor as a switch (switching losses with PWM), LED resistor, ULN2803 for relays and loads and RLC circuit.