PWM Frequency Calculator

Work out the timer settings that make a microcontroller actually produce the PWM frequency you want. Enter a target frequency, your clock and the timer width, and the solver returns the prescaler and top value (ARR) that hit it — ranked by how close they get and how much duty resolution is left over. Already have settings you suspect are wrong? Switch to forward mode, type in your prescaler and top value, and see the real frequency, period and duty steps come out. A live waveform shows the signal at any duty cycle, and ready-to-paste snippets cover Arduino (ATmega328P), ESP32 LEDC, STM32 HAL and Raspberry Pi GPIO. Everything is computed in this tab — nothing is uploaded and nothing is executed.

100% private. The solver, the waveform and every code snippet are generated with JavaScript in this browser tab. Your clock values, target frequencies and settings never leave the page — there is no server-side calculation and nothing to send.

1. Target and hardware

Platform presets

2. Target frequency

Common targets

Results

Ready
PWM frequency
—
Period
—
one full cycle
Timer tick rate
—
clock after prescale
Ticks per period
—
duty resolution

Checks on this configuration

Best settings found (click a row to use it)

PrescalerTop value (ARR)Actual frequencyError vs targetTicks / period

Code snippet

How to use it

Pick a platform preset if you have one — it fills in the timer clock and the timer width, which are the two values people most often get wrong. Then set your target frequency (or type one in), and the best settings appear immediately: a prescaler, a top value, the actual frequency they produce, and how much duty resolution is left over. Click any row in the table to switch the snippet and the waveform to that candidate. If you already have settings from a datasheet or a forum answer, switch the mode to Forward, enter the prescaler and top value, and read back the real frequency — including whether the top value even fits in the timer you selected.

The duty slider only affects the waveform preview and the OCR / compare value in the snippet; it never changes the frequency, which is set entirely by clock, prescaler and top value.

Worked example: a 50 Hz servo signal on an Arduino Uno

Servos expect a 20 ms period, i.e. exactly 50 Hz. Select the Arduino Uno preset (16 MHz, 16-bit Timer1) and target 50 Hz. The solver has to find a prescaler from the ATmega’s fixed set (½, ¼, ⅛, …, /256) and a top value under 65,535 such that 16,000,000 / (prescaler × (top + 1)) = 50. The winner is prescaler /8 with top 39999: the timer ticks at 2,000,000 Hz, one period takes 40,000 ticks, and the output is 50.000 Hz with zero error — plus 40,000 duty steps, far more than the roughly 200 a servo position command needs. Prescaler /16 with top 19999 also lands exactly on 50 Hz; it is listed below because it leaves less resolution, not because it is less accurate. If you had picked an 8-bit timer instead, there would be no answer at all: the largest period it can hold is 256 × 256 = 65,536 ticks, which only reaches down to about 244 Hz on a 16 MHz clock. The tool says so plainly — “no setting fits” — instead of handing you numbers that would wrap the counter. That is exactly why the Uno’s servo PWM lives in the 16-bit Timer1, not Timer0.

How it works

A PWM timer divides its input clock by an integer prescaler, then counts from zero up to a top value (the ARR or auto-reload register) and reloads. One output period therefore takes top + 1 ticks, which is where the off-by-one that plagues hand-rolled calculations comes from: with top = 39999 the period is 40,000 ticks, not 39,999. The frequency is:

edge-aligned:     f = f_clock / (prescaler × (top + 1))
center-aligned:   f = f_clock / (prescaler × 2 × (top + 1))

The solver inverts that: for a target frequency it computes the ideal prescaler × (top+1) product, then walks every legal prescaler, rounds the implied top value to an integer, and keeps the candidates that fit inside the timer width. They are ranked by relative error first, then by ticks per period — more ticks means finer duty control at the same frequency, which is what smooth LED dimming and quiet motor drivers actually need. Because every register holds an integer, an exact hit is not guaranteed: the error column shows when you are one tick off and how much that costs in hertz.

Common PWM frequencies and why they are what they are

FrequencyUsed forWhy that value
50 HzServo position control20 ms period is the standard hobby-servo protocol; 0–100% maps to roughly 0.5–2.5 ms pulses.
1–3 kHzLED dimming, small fan controlAbove ~200 Hz the flicker is imperceptible to most eyes; 1–3 kHz keeps it invisible under side lighting.
440 Hz – 2 kHzBuzzers and beeper tonesAudible range; 440 Hz is the A4 reference tone. Square waves here sound harsh, which is exactly what a buzzer wants.
16–21 kHzBrushed/brushless motor driversAbove the audible band, so switching noise is inaudible; 16 kHz and 20 kHz are common ESC and H-bridge defaults.
40 kHzUltrasonic distance sensorsThe HC-SR04 family transmits at 40 kHz; the timer must stay close enough that the module locks on.
20–100 kHzSwitch-mode power stages, buck convertersHigh enough for small inductors, low enough that MOSFET switching losses stay down.

Common mistakes this calculator catches

  • Using the CPU clock instead of the timer clock. On an ESP32 the LEDC peripheral runs from the 80 MHz APB bus, not the 240 MHz CPU; on a Raspberry Pi the PWM block has its own clock that defaults to 19.2 MHz. Feed the wrong number in and every result is off by the ratio between them — the platform presets use the correct peripheral clocks, and the notes under the results remind you which one is in play.
  • The top + 1 off-by-one. Datasheets specify the ARR as a count value, so a period of “40,000 ticks” needs top = 39999. Calculating with top instead of top + 1 produces a signal slightly faster than intended — at servo frequencies that is enough to drift the pulse position.
  • Forgetting center-aligned mode doubles the tick count. The same prescaler and top that give 50 kHz edge-aligned give 25 kHz center-aligned. If your motor driver is configured for center alignment, halve your target before solving.
  • Picking a top value the timer cannot hold. An 8-bit timer tops out at 255. Asking it for 39999 does not silently work — on an ATmega the overflow simply wraps and the frequency becomes something else entirely. Forward mode flags any top above the selected width.
  • Assuming prescalers are free integers. The ATmega’s CS bits only allow 1, 2, 4, 8, 16, 32, 64, 128 and 256; the ESP32 LEDC allows 1–256. The solver respects those sets per platform instead of suggesting a /5 you cannot actually program on an AVR.
  • Trading resolution away without noticing. Any prescaler that divides evenly into the target produces zero error, but a larger prescaler leaves fewer ticks per period. At 50 Hz on 16 MHz, /8 leaves 40,000 duty steps and /256 leaves only 1,250 — both are “exact”, only one gives you smooth dimming.

Luke Keam
Spot a bug or want a custom utility built? I'd love to hear from you. Get in touch
Luke Keam | techgeek.biz