Calculate Arduino PWM frequency from the timer, prescaler and TOP value in fast or phase-correct mode, or find the settings for a target frequency.
- All Physics Calculators
- PWM to Voltage Calculator
- Duty Cycle Calculator
- Square Wave Frequency Calculator
- Frequency to Period Calculator
- Baud Rate Calculator
Calculator Functions
The calculator gives the PWM frequency of an Arduino timer, with its period and duty cycle resolution, or the timer settings that produce a frequency you want. Select a function to open it in the calculator.
| Function | Use it when | You enter |
|---|---|---|
| PWM frequency from timer settings | You know how the timer is set up, or want the default analogWrite() frequency | Timer, prescaler, PWM mode and TOP value |
| Timer settings for a target frequency | You need a certain frequency, such as 25 kHz for a fan or 50 Hz for a servo | Target frequency, timer and PWM mode |
Both functions use the system clock, which is 16 MHz on a standard Arduino and can be changed to 8 MHz, 20 MHz or a custom value. The timers are those of the ATmega328P on the Uno and Nano: Timer 0 drives pins 5 and 6, Timer 1 pins 9 and 10, and Timer 2 pins 3 and 11 [2]. The calculator opens on the Arduino defaults, so selecting Calculate straight away shows the frequency of analogWrite() on pins 5 and 6. To turn a duty cycle into an average voltage, use the PWM to Voltage Calculator.
Arduino PWM Frequency Formulas
| f | PWM frequency (Hz). The first line is fast PWM and the second is phase-correct PWM |
| F | System clock frequency (Hz), 16,000,000 on a standard Arduino |
| N | Prescaler: 1, 8, 64, 256 or 1024, and also 32 or 128 on Timer 2 |
| TOP | Highest value the timer counts to: 255 for 8-bit PWM, or the value in OCRnA or ICR1 |
PWM frequency from timer settings
In fast PWM the timer counts from 0 up to TOP and starts again, which takes TOP + 1 ticks. In phase-correct PWM it counts up to TOP and back down, which takes 2 × TOP ticks, so the frequency is about half [2]. Arduino sets every timer’s prescaler to 64, runs Timer 0 in fast PWM and Timers 1 and 2 in phase-correct PWM [2]. That gives the two default frequencies:
| Uno pins | Timer and mode | Calculation | Frequency |
|---|---|---|---|
| 5 and 6 | Timer 0, fast PWM | 16,000,000 ÷ (64 × 256) | 976.56 Hz |
| 9 and 10 | Timer 1, phase-correct | 16,000,000 ÷ (2 × 64 × 255) | 490.20 Hz |
| 3 and 11 | Timer 2, phase-correct | 16,000,000 ÷ (2 × 64 × 255) | 490.20 Hz |
Timer 1 is 16-bit, so its TOP can also be 511, 1023 or any value up to 65,535 held in ICR1 or OCR1A [1]. Timers 0 and 2 are 8-bit, so their TOP cannot pass 255. The duty cycle resolution is the number of steps between off and fully on: TOP + 1 in fast PWM and TOP in phase-correct PWM.
Timer settings for a target frequency
TOP = F ÷ (2 × N × f)
These are the two formulas solved for TOP, fast PWM first. The calculator tries each prescaler the timer has, rounds TOP to a whole number, and keeps the smallest prescaler whose TOP fits the timer, because a larger TOP gives finer duty cycle steps. For 25 kHz on Timer 1 in fast PWM, TOP = 16,000,000 ÷ (1 × 25,000) − 1 = 639. The result table lists the other prescalers that also reach the frequency, with the error each one leaves after rounding.
Limits of This Estimate
- The timers, prescalers and pin numbers are those of the ATmega328P boards such as the Uno and Nano [1]. Other boards, including the Mega, Leonardo, Due and ESP32, have different timers or pin assignments.
- Timer 0 also runs millis() and delay(), so changing its prescaler or TOP changes how fast they count [2].
- On Timers 0 and 2, a TOP other than 255 is stored in OCRnA, which leaves only output B for PWM: pin 5 on Timer 0 and pin 3 on Timer 2 [2].
- The result uses the nominal clock frequency. A real board’s oscillator is slightly off, and the output frequency is off by the same fraction.
Sources
- Microchip, ATmega328P Datasheet: PWM frequency equations, prescaler values and Timer 1 TOP values
- Arduino Documentation, Secrets of Arduino PWM: Arduino’s default timer settings, the pin for each timer output, and using OCRnA as TOP
