ESP32 PWM (LEDC) calculator
On the ESP32, faster PWM means fewer duty steps. Pick a frequency, see how fine your control can be.
Common frequencies
| Frequency | Use | Max bits (ESP32) | Max bits (C3/S3) |
|---|---|---|---|
| 50 Hz | Hobby servo | 20 | 14 |
| 1 kHz | LED dimming (visible flicker-free) | 16 | 14 |
| 5 kHz | LED dimming, Arduino-ESP32 examples | 13 | 13 |
| 20 kHz | DC motor (above hearing) | 11 | 11 |
| 25 kHz | PC fan (Intel 4-wire spec) | 11 | 11 |
How it works
The LEDC peripheral counts a source clock. In one PWM period it can count at most source ÷ frequency ticks, so the number of duty steps is limited by:
max bits = floor(log₂(80 000 000 ÷ f)) (capped by the chip's timer width)We assume the 80 MHz APB clock, which Arduino-ESP32's ledcAttach() uses for these frequencies. The timer width limit is 20 bits on the original ESP32 and the C6, and 14 bits on the S2, S3 and C3 (ESP-IDF LEDC driver documentation, SOC_LEDC_TIMER_BIT_WIDTH).
Notes
- Arduino-ESP32 3.x:
ledcAttach(pin, freq, bits)thenledcWrite(pin, duty)with duty from 0 to 2bits − 1. The 2.x API (ledcSetup/ledcAttachPin) is gone. - If you ask for more bits than allowed,
ledcAttach()fails and returns false — check its return value. - Servos need 50 Hz; 14–16 bits gives smooth angle control (a 1–2 ms pulse spans about 820–1640 steps at 14 bits).