/    Calculators » Microcontroller timer PWM calculator
Support the project on Patreon


Calculator KB-45

Microcontroller timer PWM calculator

Timer settings without manual calculations or off-by-one errors.

KB-45Microcontroller timer PWM calculatorCalculates in real time

Result

PWM frequency7.813 kHz
Resolution8 bit
Duration of one step500 ns
Compare value128
Average voltage2.5 V
TOP for desired frequency1999
Share calculation

In brief: Calculate microcontroller timer PWM: frequency from the clock, prescaler and TOP (ARR), resolution, compare value for the duty cycle, and average voltage. Example: PWM frequency: 7.813 kHz.

Calculation example

Input data
Clock frequency16 MHz
Prescaler8
TOP (ARR)255
Duty cycle50 %
Output voltage5 V
Desired frequency1000 Hz
Result
PWM frequency7.813 kHz
Resolution8 bit
Duration of one step500 ns
Compare value128
Average voltage2.5 V
TOP for desired frequency1999

How it is calculated

f = fclk / (PSC · (TOP + 1))
bits = log₂(TOP + 1)

The timer counts clock cycles from zero to TOP, and one complete count makes one PWM period. The higher the frequency at the same clock rate, the fewer steps are available for adjustment. On STM32, the value written to the PSC register is one less than the prescaler.

What to look for

  • For LEDs, use a frequency above 200 Hz so flicker isn't noticeable on camera or to the eye.
  • For motors, use PWM above 16–20 kHz to eliminate audible whine.

Frequently asked questions

How do you calculate a timer's PWM frequency?

f = fclk / (PSC · (TOP + 1)). At 16 MHz, with a prescaler of 8 and TOP 255, the result is 7.81 kHz with 8-bit resolution.

How do you get 1 kHz PWM?

TOP = fclk / (PSC · f) − 1. At 16 MHz with a prescaler of 8, TOP is 1999, giving about 11-bit resolution.

What PWM frequency do you need for motors and LEDs?

For LEDs, use at least 200 Hz, or 1 kHz to avoid flicker on camera. For motors, 16–20 kHz is better: the audible whine goes away. At the same clock rate, a higher frequency means lower resolution.

Updated · kirich.blog · The calculation runs in your browser; no data is sent anywhere.

See also