PWM: how a digital pin pretends to be an analogue one
A microcontroller pin is either high or low, yet it dims LEDs, sets motor speeds and positions servos. The trick is pulse-width modulation: switch the pin on and off fast, and vary the fraction of each period it is on — the duty cycle. Anything slower than the switching, a motor, the eye, an RC filter, averages the pulses into a steady level of duty × V. This calculator turns a frequency, duty and high level into the period, on and off times and the average, shows what the timer's resolution does to the steps and the register value, and with the timer clock works out the counter settings.
How to use the PWM calculator
- Type the frequency (490, 1k, 25k) and the duty cycle in percent, or drag the slider; set the high level to the pin's voltage, or to the motor supply if a driver follows.
- Pick the timer's bits (8-bit for analogWrite on an Arduino, 10–16 for timers and ESP32 channels) to see the steps, the smallest pulse and the register value for this duty.
- Optional: the timer clock (16 MHz Uno, 80 MHz ESP32) adds the counter top for every prescaler in the Calculation tab.
- The drawing shows the pulses with the average dashed across and the register as bits; the Duty table lists on time, average and register for round duty cycles; click the drawing for what an LED, a motor, a servo or an RC filter makes of the signal.
Duty cycle and average voltage
D = ton / T Vavg = D × Vhigh
The period T = 1 / f is fixed; the pulse width ton is what you control. The average of a wave that is Vhigh for a fraction D of the time and 0 for the rest is simply D × Vhigh, and that is what any load with inertia or a filter responds to: 75% duty from a 5 V pin behaves like 3.75 V. The RMS value, Vhigh × √D, is what heats a resistor, and the peak is always Vhigh, which the load must tolerate.
Your signal 75% × 5 V = 3.75 V average; on for 750 µs of every 1 ms.
Choosing the frequency
The frequency does not change the average; it changes what the load notices. For an LED, anything above about 100–200 Hz is flicker-free to the eye (cameras need more). For a motor, below a few hundred hertz it cogs, between 200 Hz and 20 kHz the windings whine audibly, and 20–25 kHz is the usual silent choice, at the cost of a little switching loss. Servos are the exception: they read the pulse width, 1–2 ms, inside a 20 ms (50 Hz) frame. Audio through PWM wants ten times the highest audio frequency. Arduino's analogWrite uses 490 Hz (980 Hz on two pins), which is fine for LEDs and audibly noisy on motors.
| Use | Frequency | Why |
|---|---|---|
| Hobby servo | 50 Hz | 1–2 ms pulse, 5–10% duty; the width is the command |
| LED dimming | 200 Hz – 5 kHz | flicker-free; higher for cameras and RGB mixing |
| DC motor | 20–25 kHz | above hearing, below the driver's switching limits |
| Heater, fan | 1–100 Hz | slow loads; low frequency keeps switching loss nil |
| PWM as a DAC | ≥ 10 × signal | so an RC filter can remove the carrier |
Resolution: bits, steps and the register
N = round(D × (2ⁿ − 1)) step = T / 2ⁿ
A timer counts from 0 to 2ⁿ − 1 once per period and drives the pin high while the count is below the compare register N. With 8 bits there are 256 steps, so the duty moves in jumps of 0.39% and a 1 kHz signal in jumps of 3.9 µs; 16 bits give 65536 steps and 15 ns at the same frequency. More bits need more counts per period, so for a given clock the top frequency falls: an 8-bit timer on 16 MHz manages 62.5 kHz, a 16-bit one only 244 Hz unless the top is lowered. Smooth LED fades want at least 10–12 bits because the eye is so sensitive at the dark end.
Timers and prescalers
top = fclk / (prescaler × fPWM) − 1
The timer ticks at the clock divided by a prescaler (1, 8, 64, 256, 1024 on AVR; any integer on STM32 and ESP32) and rolls over at a top value. The PWM frequency is the tick rate divided by top + 1, so choose the smallest prescaler that keeps the top within the timer's width: that preserves the most duty steps. Because the top must be a whole number, the frequency you get differs slightly from the one you asked for; the calculator shows both. On an ESP32 the LEDC peripheral does the arithmetic for you from a frequency and a bit depth, with the same trade-off: f × 2ⁿ must stay below the clock.
Your signal, step by step
- Period: 1 / 1 kHz = 1 ms; on 750 µs, off 250 µs.
- Average: 75% × 5 V = 3.75 V; RMS 4.33 V.
- 8-bit register: round(75% × 255) = 191, which actually gives 74.9%; each count is 3.91 µs or 19.6 mV.
Worked example: 1 kHz at 75% from a 5 V pin, 8-bit
T = 1 ms, so the pin is high for 750 µs and low for 250 µs of every millisecond, and a motor or an LED averages it to 3.75 V. An 8-bit timer has 255 as its top count, so the register is round(0.75 × 255) = 191, which really gives 74.9% — the nearest step. Each count is 3.91 µs. On a 16 MHz Arduino the same 1 kHz needs a top of 15999 at prescaler 1, which fits a 16-bit timer, or 249 at prescaler 64, which fits an 8-bit one.
Questions
Is PWM the same as a lower voltage?
For the average, yes; for the peaks, no. The load still sees the full voltage during each pulse, so a 3.3 V device on a 5 V PWM pin is over-voltaged 75% of the time even at 75% duty. Use a filter, a level shifter or a lower supply when the peak matters.
Why does my LED look the same from 50% to 100%?
The eye responds roughly to the square root of the light, so the big visual changes happen at low duty. Dimming curves (gamma correction) map the slider to duty = (slider)² or finer, and need 10 bits or more to avoid visible steps at the dark end.
Can I get a true analogue voltage out of PWM?
With an RC filter whose time constant is 30 times the period or more, the ripple drops below 1% and you have a slow DAC; the resolution is the timer's. For faster or cleaner signals use a real DAC.
What limits the PWM frequency?
The timer clock divided by the number of steps you want, and the driver's switching speed. A MOSFET driver at 100 kHz spends a measurable share of each period switching, and that is pure heat; most motor drives settle at 20–40 kHz for that reason.