PWM Calculator — frequency, resolution and register values

The frequency-versus-resolution trade-off made explicit, with register values for your MCU and duty-cycle counts.

Example: 1 kHz PWM from a 16 MHz clock needs TOP = 15999 with a ÷1 prescaler, giving 14 bits of duty resolution.

Check it against real silicon

Chiprun runs your firmware on an emulated microcontroller and hands back the UART output, so you can confirm these numbers rather than trusting them.

Chiprun docs

Formula

f_pwm = f_clk / (prescaler × (TOP + 1))

Worked example

1 kHz PWM from a 16 MHz clock needs TOP = 15999 with a ÷1 prescaler, giving 14 bits of duty resolution.

  1. steps = f_clk / (prescaler × f_pwm)

    steps = 16 MHz / (1 × 1 kHz)

    steps = 16000

  2. TOP = steps − 1

    TOP = 16000 − 1

    TOP = 15999

    The counter includes zero, so a period of N steps means TOP = N − 1.

  3. compare = duty × steps

    compare = 0.5 × 16000

    compare = 8000 (50%)

  4. resolution = log₂(steps)

    log₂(16000)

    13.97 bits

    Frequency and resolution trade against each other — both come out of the same counter.

Frequently asked questions

Why does higher PWM frequency reduce resolution?

Both come out of the same counter. At a 16 MHz clock, a 1 kHz PWM period is 16,000 counter steps — about 14 bits of duty resolution. Push to 100 kHz and only 160 steps remain, around 7 bits. Raising the timer clock is the only way to get both.

What PWM frequency should I use for a motor?

Above 20 kHz if you want it silent, since that is beyond human hearing — audible whine from a motor is almost always the PWM frequency. Below that, higher frequencies mean smoother current but more switching loss in the driver.

What frequency do hobby servos need?

50 Hz, with a pulse between roughly 1 ms and 2 ms. Note that the duty cycle is tiny — 1 to 2 ms out of a 20 ms period is 5–10% — so you need good resolution at low duty, which usually means a large counter value.

Why is my duty cycle slightly off what I asked for?

The compare register is an integer, so achievable duty cycles are multiples of one counter step. With 200 steps the finest adjustment is 0.5%, and anything between lands on the nearest step. The tool shows both the requested and the actual value.

Related tools