Timers & Motor Control

# PWM Timer Prescaler & ARR Calculator

Solve exact `PSC`, `ARR` and `CCR` register values with maximum duty-cycle resolution for STM32, AVR, RP2040 and ESP32 hardware timers.

MCU timer architectureSTM32 16-bit Timer (TIM1/TIM3/TIM4)STM32 32-bit Timer (TIM2/TIM5)AVR ATmega 16-bit Timer1 (Fast PWM)RP2040 / RP2350 PWM Slice (16-bit TOP)ESP32 / ESP32-S3 LEDC (APB 80 MHz)f\_PWM = f\_TIM / ((PSC + 1) \* (ARR + 1))

Timer kernel clock (MHz)

Target PWM frequency (Hz)50 Hz servo, 20 kHz BLDC/FOC, 100 kHz buck converter

Duty cycle (%)

Alignment modeEdge-aligned (up-counting)Center-aligned (up/down motor FOC)

Quick presets:

Best PSC / Divider 0 Effective divider /1

ARR / TOP 4199 CCR = 2100 (50%)

Actual Frequency 20000.00 Hz Error +0.0000%

Duty Resolution 12.04 bits 4200 discrete steps

| PSC reg | Divider | ARR / TOP | CCR  | Actual Hz | Error % | Resolution |
| ------- | ------- | --------- | ---- | --------- | ------- | ---------- |
| 0       | /1      | 4199      | 2100 | 20000.00  | 0.0000% | 12.04 bit  |
| 1       | /2      | 2099      | 1050 | 20000.00  | 0.0000% | 11.04 bit  |
| 2       | /3      | 1399      | 700  | 20000.00  | 0.0000% | 10.45 bit  |
| 3       | /4      | 1049      | 525  | 20000.00  | 0.0000% | 10.04 bit  |
| 4       | /5      | 839       | 420  | 20000.00  | 0.0000% | 9.71 bit   |
| 5       | /6      | 699       | 350  | 20000.00  | 0.0000% | 9.45 bit   |
| 6       | /7      | 599       | 300  | 20000.00  | 0.0000% | 9.23 bit   |
| 7       | /8      | 524       | 263  | 20000.00  | 0.0000% | 9.04 bit   |

## Timer register formulas

Off-by-one errors in `PSC` and `ARR` shift switching frequencies and break closed-loop FOC timing. On STM32, hardware adds one internally to both registers, so a 100-tick period writes 99.

STM32 TIM3 bare-metal PWM setup

```
TIM3->PSC = psc_reg;          /* effective divider = psc_reg + 1 */
TIM3->ARR = arr_reg;          /* edge-aligned period = arr_reg + 1 ticks */
TIM3->CCR1 = ccr_reg;         /* duty = ccr_reg / (arr_reg + 1) */
TIM3->CCMR1 |= (6U << 4) | TIM_CCMR1_OC1PE; /* PWM mode 1 + preload */
TIM3->CCER |= TIM_CCER_CC1E;
TIM3->CR1 |= TIM_CR1_ARPE | TIM_CR1_CEN;
```

## Frequently asked questions

| Question                                                         | Answer                                                                                                                                                                                                          |
| ---------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Why does maximizing ARR instead of PSC improve PWM quality?      | Every count in ARR adds one discrete duty-cycle step. Keeping PSC as small as possible maximizes ARR and pushes duty resolution closer to 12 to 16 bits, which matters for motor current loops and LED dimming. |
| How does center-aligned PWM change the frequency formula?        | A center-aligned timer counts up from 0 to ARR and back down to 0 each switching cycle. That doubles the period compared to edge-aligned mode, so f\_PWM equals f\_TIM divided by (2 \* (PSC + 1) \* ARR).      |
| Why is my STM32 PWM frequency twice what I calculated from APB1? | Whenever the APB1 or APB2 prescaler is greater than 1, STM32 multiplies the timer kernel clock by 2\. Enter the doubled timer clock here, not the raw APB peripheral clock.                                     |

[Tool: STM32 PLL Clock Calculator](https://circuittwin.ai/tools/stm32-pll-clock-calculator) [Board: NUCLEO-H743ZI2 (HAL TIM oracle)](https://circuittwin.ai/boards/nucleo-h743zi2) [Twin: PCA9685 16-ch PWM Driver](https://circuittwin.ai/twins/pca9685) [Guide: Marlin Firmware CI](https://circuittwin.ai/guides/marlin-firmware-testing-ci)

> ## Measure real timer periods and dead-time on your compiled ELF
>
> Run your motor-control or servo firmware on CircuitTwin and inspect virtual-time GPIO pin transitions in deterministic CI runs.
>
> [Start now](https://app.circuittwin.ai/try) [STM32 PLL Clock Calculator](https://circuittwin.ai/tools/stm32-pll-clock-calculator) [Connect a coding agent](https://circuittwin.ai/docs/agents)
