Skip to content

Esp32 Gpio Pwm Res

DodaTech 1 min read

In this tutorial, you'll learn about ESP32 PWM Resolution Causes Coarse Brightness Steps. We cover key concepts, practical examples, and best practices.

The Problem

ESP32 LED brightness changes in visible steps instead of smooth gradients.

Quick Fix

Wrong

ledcSetup(0, 5000, 8);  // 8-bit = 256 levels, visible steps
LED makes abrupt brightness jumps. Dark area steps are especially visible.
ledcSetup(0, 5000, 12);  // 12-bit = 4096 levels, smooth
ledcAttachPin(2, 0);
for (int duty = 0; duty < 4096; duty += 16) {
  ledcWrite(0, duty);
  delay(10);
}
LED fades smoothly from off to full brightness with no visible steps.

Prevention

Use 10-12 bit resolution for smooth dimming (1024-4096 levels). Higher resolution reduces max frequency. Balance resolution and frequency: 12-bit @ 5 kHz is good for LEDs. Use gamma correction for perceptually linear brightness.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### How does PWM resolution affect frequency?

Max frequency = 40 MHz / 2^resolution. Higher resolution = lower max freq. 10-bit: 39 kHz, 12-bit: 9.7 kHz, 14-bit: 2.4 kHz.

What is gamma correction for PWM?

Human vision is logarithmic. Linear duty steps appear nonlinear. Apply gamma: actual = pow(duty/255, 2.2) * 255 for perceptually linear brightness.

Is 16-bit resolution useful?

16-bit gives 65536 levels but max frequency is only 610 Hz, causing visible flicker. 10-12 bit is the sweet spot for LEDs.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro