NewAI Part GeneratorFree to get started
FREE TOOL

PWM frequency calculator (STM32 and AVR)

For an STM32 or AVR timer, this PWM frequency calculator turns the clock and registers into frequency, resolution and duty cycle, or finds the prescaler and period for the rate you want. The search favors resolution and shows what each setting costs in frequency error.

Find
Examples
Hz
Counter width
%

Optional: CCR or OCR, gives its duty cycle.

PWM frequency

20 kHz

Resolution
11.81 bits duty step 0.0278 %
CCR for 50.0 % duty
1800 gives 50.000 %
Counter clock
72 MHz
PWM period
50 µs

Timer clock is not always PCLK: on STM32F4 with the default TIMPRE = 0, when the APB prescaler is above 1, timers run at twice PCLK (RM0090, section 6.2). Check your clock tree.

Resolution available at each PWM frequency
0510151101001k10k100k1M10MPWM frequency (Hz)Resolution (bits)20 kHz

How to calculate PWM frequency

PWM frequency is the timer clock divided by the prescaler and the counts per period. An STM32 at 72 MHz with PSC = 0 and ARR = 3599 gives 72 MHz ÷ (1 × 3600) = 20 kHz with 11.8 bits of resolution. An AVR in fast PWM uses f = fclk ÷ (N (1 + TOP)).

PWM frequency formula and duty cycle

A timer counts clock ticks up to a limit and repeats. The limit sets the period and a compare register sets where the output changes.

STM32 edge-aligned: f = fCK ÷ ((PSC + 1)(ARR + 1))     duty = CCR ÷ (ARR + 1)
fCK
timer clock in Hz, before the prescaler
PSC
prescaler register, 0 to 65535 (the clock divides by PSC + 1)
ARR
auto-reload register
CCR
capture/compare register, PWM mode 1
STM32 center-aligned: f = fCK ÷ (2 (PSC + 1) ARR)     duty = CCR ÷ ARR

In center-aligned mode the counter runs from 0 to ARR − 1 and then from ARR down to 1, which is 2 × ARR counts. The counter does not run while ARR is 0.

AVR fast PWM: f = fclk ÷ (N (1 + TOP))     duty = (OCR + 1) ÷ (TOP + 1)
fclk
I/O clock in Hz
N
prescaler: 1, 8, 64, 256 or 1024 (Timer2 also has 32 and 128)
TOP
highest count: fixed at 255, 511 or 1023, or set by ICR1 or OCRxA
OCR
output compare register, non-inverting output
AVR phase correct: f = fclk ÷ (2 N TOP)     duty = OCR ÷ TOP

AVR phase and frequency correct PWM uses the same formula as phase correct. A CTC toggle output gives f = fclk ÷ (2 N (1 + OCR)) at a fixed 50 % duty. Resolution, on either family, is the number of bits it takes to count the period:

resolution = log2(TOP + 1) bits

The PWM calculator’s generic up-counter uses f = fclk ÷ (divider × counts), with the output high while the count is below the compare value, the same as STM32 PWM mode 1. In the target-frequency search its divider runs from 1 to 65536.

PWM frequency calculation examples

Example 1: the default PWM on an Arduino Uno

Timer1 is 8-bit phase correct with N = 64: f = 16 MHz ÷ (2 × 64 × 255) = 490.2 Hz. Timer0 is 8-bit fast PWM with the same prescaler: f = 16 MHz ÷ (64 × 256) = 976.6 Hz. That is why pins 5 and 6 run at about twice the rate of the others.

Example 2: 20 kHz at 30 % duty on an STM32 at 72 MHz

The period is 72 MHz ÷ 20 kHz = 3600 counts, which fits a 16-bit counter with no prescaler: PSC = 0, ARR = 3599, resolution 11.8 bits. For 30 % duty, CCR = 0.3 × 3600 = 1080, which gives 30.00 %. In center-aligned mode one period is 2 × ARR counts, so the same frequency takes ARR = 1800: 72 MHz ÷ (2 × 1800) = 20 kHz.

Example 3: a 50 Hz servo signal from ATmega328P Timer1

Fast PWM with ICR1 as TOP. N = 1 needs a count of 320,000, too big for 16 bits, so the next tap is N = 8: TOP = 16 MHz ÷ (8 × 50 Hz) − 1 = 39999. A 1.5 ms pulse is 7.50 % of the 20 ms period, so OCR1A = 0.075 × 40000 − 1 = 2999, in steps of 500 ns.

AVR and Arduino PWM frequency table at 16 MHz

Common TOP values on an ATmega328P and Arduino Uno at each prescaler. Timer0 and Timer2 are 8-bit; 10-bit and 16-bit modes are on Timer1. For a custom TOP, use the AVR timer calculator above.

NFast, 8-bitPhase correct, 8-bitFast, 10-bitPhase correct, 10-bitFast, 16-bit
162.5 kHz31.37 kHz15.63 kHz7.82 kHz244.1 Hz
87.813 kHz3.922 kHz1.953 kHz977.5 Hz30.52 Hz
64976.6 Hz490.2 Hz244.1 Hz122.2 Hz3.815 Hz
256244.1 Hz122.5 Hz61.04 Hz30.55 Hz953.7 mHz
102461.04 Hz30.64 Hz15.26 Hz7.637 Hz238.4 mHz

STM32 prescaler and ARR table at 72 MHz

The best-resolution setting for common frequencies on a 16-bit edge-aligned timer with a 72 MHz clock. For any other clock, use the STM32 timer calculator above.

FrequencyPSCARRErrorResolution
50 Hz2165454-0.0007 %16.0 bits
100 Hz1065454-0.0007 %16.0 bits
1 kHz135999exact15.1 bits
10 kHz07199exact12.8 bits
20 kHz03599exact11.8 bits
50 kHz01439exact10.5 bits
100 kHz0719exact9.5 bits
1 MHz071exact6.2 bits

What goes wrong in real circuits

The usual mistake is the timer clock. On STM32F4 with the default TIMPRE = 0, the timers on an APB bus run at twice PCLK whenever that bus prescaler is above 1, so read the clock tree and enter that clock in the STM32 PWM calculator instead of assuming the core clock. Remember that PSC + 1 and ARR + 1 count, not PSC and ARR. On an 8-bit AVR timer, setting a custom TOP uses OCRxA, so channel A stops being a PWM output; Timer1 avoids this by using ICR1 as TOP. Change the compare value through the buffered registers (preload enabled on STM32, or any PWM mode on AVR) so a new duty takes effect at the update event on STM32, or at TOP or BOTTOM on AVR, and does not produce a glitch. Prescaler taps also limit an AVR: the frequency you ask for may only be reachable with less resolution than the timer has, and the AVR PWM calculator shows the tradeoff.

Glossary

Timer clock
The clock entering the prescaler. On STM32 this can be twice the APB clock.
Prescaler (PSC or N)
A divider between the timer clock and the counter.
ARR or TOP
The register that sets the highest value the counter reaches, and so the period.
Compare register (CCR or OCR)
The value that sets where the output changes state, and so the duty cycle.
Resolution
log2 of the number of counts in a period, in bits.
Edge-aligned PWM
The counter runs one way, so every pulse starts at the same edge of the period.
Center-aligned PWM
The counter runs up and then down, so pulses are centered in the period. AVR calls this phase correct.

Related guides

Frequently asked questions

How do you calculate PWM frequency from a timer?

Divide the timer clock by the prescaler and by the number of counts in one period. On an STM32 in edge-aligned mode that is f = fCK ÷ ((PSC + 1)(ARR + 1)). On an AVR in fast PWM it is f = fclk ÷ (N (1 + TOP)).

What PWM frequency does an Arduino Uno use?

Pins 3, 9, 10 and 11 run at 490.2 Hz and pins 5 and 6 run at 976.6 Hz. The Arduino core sets Timer1 and Timer2 to 8-bit phase correct PWM and Timer0 to fast PWM, all with a prescaler of 64, on a 16 MHz clock. To use the tool at the top of this page as an Arduino PWM frequency calculator for any other prescaler or TOP, choose an AVR timer and a 16 MHz clock.

What is the PWM duty cycle formula?

On an STM32 in edge-aligned PWM mode 1, duty = CCR ÷ (ARR + 1). On an AVR in non-inverting fast PWM, duty = (OCR + 1) ÷ (TOP + 1). For 30 % duty at ARR = 3599, CCR = 1080, which gives 30.00 %.

What is the difference between PSC and ARR on an STM32?

PSC divides the timer clock before it reaches the counter, so the counter ticks at fCK ÷ (PSC + 1). ARR sets how far the counter goes before it restarts, so it sets both the period and the number of duty steps. For the most resolution, keep PSC as small as you can and let ARR do the work.

How does PWM resolution depend on frequency?

Resolution is the number of counts in a period, so raising the frequency lowers it. A 72 MHz timer at 20 kHz has 3600 counts per period (11.8 bits). At 1 MHz it has 72 counts (6.2 bits).

Why is my STM32 PWM frequency twice what I calculated?

Usually the timer clock. On STM32F4 with the default TIMPRE = 0, when the APB prescaler is more than 1, the timers on that bus run at twice PCLK. Going the other way, center-aligned mode counts up and down, so it runs at half the frequency of edge-aligned mode for the same ARR.

Should I use fast PWM or phase correct PWM?

Fast PWM reaches twice the frequency for the same TOP and suits power regulation and DAC use. Phase correct counts up and down, so its pulses stay centered as the duty changes. The ATmega328P data sheet recommends the dual-slope modes for motor control.

How do I get exactly 0 % or 100 % duty?

On an STM32 in PWM mode 1, CCR = 0 holds the output low and CCR above ARR holds it high. On AVR fast PWM, OCR = TOP is constant high but OCR = 0 still gives a one-count spike, so a true off means disconnecting the pin from the timer. AVR phase correct reaches both ends with OCR = 0 and OCR = TOP.

More free PCB tools

See all free electronics tools →

Sources

Design the whole board, not one number

ProtoFlow is free AI PCB design software: describe a circuit and get a schematic with real parts, PCB layout with an autorouter, SPICE simulation and Gerber files. Native KiCad, Altium and EAGLE import and export. Free on macOS, Windows and Linux.

Get for Mac

Free to start. No card required.