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.
| N | Fast, 8-bit | Phase correct, 8-bit | Fast, 10-bit | Phase correct, 10-bit | Fast, 16-bit |
|---|
| 1 | 62.5 kHz | 31.37 kHz | 15.63 kHz | 7.82 kHz | 244.1 Hz |
| 8 | 7.813 kHz | 3.922 kHz | 1.953 kHz | 977.5 Hz | 30.52 Hz |
| 64 | 976.6 Hz | 490.2 Hz | 244.1 Hz | 122.2 Hz | 3.815 Hz |
| 256 | 244.1 Hz | 122.5 Hz | 61.04 Hz | 30.55 Hz | 953.7 mHz |
| 1024 | 61.04 Hz | 30.64 Hz | 15.26 Hz | 7.637 Hz | 238.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.
| Frequency | PSC | ARR | Error | Resolution |
|---|
| 50 Hz | 21 | 65454 | -0.0007 % | 16.0 bits |
| 100 Hz | 10 | 65454 | -0.0007 % | 16.0 bits |
| 1 kHz | 1 | 35999 | exact | 15.1 bits |
| 10 kHz | 0 | 7199 | exact | 12.8 bits |
| 20 kHz | 0 | 3599 | exact | 11.8 bits |
| 50 kHz | 0 | 1439 | exact | 10.5 bits |
| 100 kHz | 0 | 719 | exact | 9.5 bits |
| 1 MHz | 0 | 71 | exact | 6.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.
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 →
- UART baud rate calculatorBaud-rate register values and the real baud rate error for AVR, STM32 and other UARTs at your clock frequency.
- Crystal load capacitance calculatorLoad capacitor values for a crystal oscillator from the crystal's CL and the stray capacitance, with nearest standard values.
- I2C pull-up resistor value calculatorMinimum and maximum I2C pull-up resistor for your supply voltage, bus capacitance and speed mode, per the I2C specification.
- 555 timer calculatorFrequency, period and duty cycle of a 555 in astable mode, or the pulse width in monostable, and the parts for a target.
- Switch debounce calculatorRC values for a hardware switch debounce circuit with a Schmitt-trigger input, from the bounce time you need to cover.
- R2R DAC calculator (R-2R ladder)Output voltage for any code of an R-2R resistor ladder DAC, the step size, output resistance and resistor-tolerance error.
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.