PWM and timer calculator
Choose a board, a frequency and a duty cycle. You get the register values for its timer, the frequency it really makes, how many duty steps you have, and code for its toolchain.
Results are arithmetic on published formulas, not measurements. Check them on your hardware. Free, no sign-up
What PWM is
Pulse-width modulation switches a pin fully on and off, quickly and over and over. The frequency is how many on-off cycles happen each second; the duty cycle is the share of each cycle the pin spends on. An LED at 25 % duty looks dimmer and a motor at 25 % runs slower, because both respond to the average.
How a timer makes PWM
A hardware timer counts clock ticks, usually after a prescaler that divides the clock down. When the count reaches the period value (TOP on AVR, ARR on STM32, the wrap value on the RP2040) it starts again: that sets the frequency. A compare value sets where in the count the pin switches: that sets the duty cycle.
Frequency against resolution
The number of duty steps is the timer clock divided by the PWM frequency. A higher frequency means fewer steps: from its 80 MHz clock the ESP32 LEDC gets 13 bits at 5 kHz, but only 8 bits at 312 kHz. Pick the lowest frequency that suits the load.
LEDs: a few hundred hertz or more so the eye sees no flicker; 1 to 5 kHz is common. DC motors through a driver: often 20 kHz or more, above what people can hear. Hobby servos: 50 Hz, with a pulse of about 1 to 2 ms setting the angle.
The formula
f = fclk / (prescaler × (TOP + 1)) duty = compare / (TOP + 1) steps = TOP + 1, resolution = log2(steps) bits ESP32 LEDC: f = fclk / (divider × 2^bits) nRF52840: f = 16 MHz / (2^PRESCALER × COUNTERTOP)
Source: ATmega328P / ATmega2560 datasheet, Timer/Counter1 Fast PWM (mode 14, TOP = ICR1); RM0368 / RM0383, general-purpose timers; ESP32 and ESP32-S3/C3 Technical Reference Manuals, LED PWM Controller; RP2040 Datasheet 4.5.2.6 “Configuring PWM Period”; nRF52840 Product Specification, PWM.
Worked example
A servo signal on an Arduino Uno: 50 Hz at 7.5 % duty (a 1.5 ms pulse, the centre position). Timer1 with prescaler 8 counts at 2 MHz; TOP = 2 000 000 / 50 − 1 = 39999, so the frequency is exactly 50 Hz, and OCR1A = 0.075 × 40000 = 3000.
An ESP32 at 5 kHz: 80 MHz / 5 kHz = 16000 ticks per period, so the LEDC gets 13 bits (8192 steps) with a divider of 1.953125, exactly 5 kHz; 50 % duty is 4096.
Questions and answers
What PWM frequency should I use?
LEDs: 1 to 5 kHz avoids visible flicker. DC motors: 20 kHz or more to stay above hearing, if the motor driver can switch that fast. Servos: 50 Hz, with pulses of 1 to 2 ms. Buzzers: the pitch you want to hear.
Why do I get fewer duty steps at a higher frequency?
Each PWM period is counted in timer ticks. At a higher frequency the period is shorter, so it holds fewer ticks, and each tick is one duty step. Halving the frequency doubles the steps, up to the width of the timer.
Can changing a timer break other things?
Yes. On an Arduino Uno, Timer0 runs millis() and delay(), Timer1 is used by the Servo library and Timer2 by tone(). Reconfiguring one of them changes those functions and the analogWrite() pins on that timer. Pick a timer your sketch does not already use.
What frequency does analogWrite() use?
It depends on the board and its core. On an Arduino Uno it is about 490 Hz on pins 3, 9, 10 and 11 and about 980 Hz on pins 5 and 6, with 8-bit duty (0 to 255). To choose the frequency yourself, set the timer registers as this calculator shows.
Is the frequency shown here measured?
No. It is calculated from the register formulas in the datasheets, for the clock shown. If your board runs its clock at another speed, change the clock field. To check the real signal, use an oscilloscope or a logic analyser.
Learn the background
Free lessons of the BoardPilot course that explain the ideas behind this calculator:
Timers and interruptsPWM, state machines and more
Other calculators
LED resistor calculatorVoltage divider calculatorI2C pull-up resistor calculatorUART baud rate calculatorADC sample rate calculator
See it on a real board
BoardPilot uses these same calculations inside the app, next to a live 3D view of your board, its pins and wires, with guided debugging when something does not work. Try it in your browser, or download it for macOS and Windows.