Calculator KB-45
Microcontroller timer PWM calculator
Timer settings without manual calculations or off-by-one errors.
Result
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
| Clock frequency | 16 MHz |
|---|---|
| Prescaler | 8 |
| TOP (ARR) | 255 |
| Duty cycle | 50 % |
| Output voltage | 5 V |
| Desired frequency | 1000 Hz |
| PWM frequency | 7.813 kHz |
|---|---|
| Resolution | 8 bit |
| Duration of one step | 500 ns |
| Compare value | 128 |
| Average voltage | 2.5 V |
| TOP for desired frequency | 1999 |
How it is calculated
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.
