PWM LED Dimming - Analog Output on ESP32
Learn how to control LED brightness smoothly using Pulse Width Modulation (PWM) - the technique that makes digital pins behave like analog outputs!
🔌 What is PWM?
Pulse Width Modulation (PWM) is a technique that creates the illusion of analog voltage using rapid digital on/off switching. Instead of providing a steady voltage, PWM rapidly toggles between HIGH (3.3V) and LOW (0V).
- 0% duty cycle = Always OFF (LED off)
- 50% duty cycle = Half ON, half OFF (LED at 50% brightness)
- 100% duty cycle = Always ON (LED at full brightness)
Because the switching happens thousands of times per second, our eyes perceive a smooth, steady brightness level rather than flickering.
⚡ ESP32 PWM Advantages
ESP32 has significantly more powerful PWM capabilities compared to Arduino Uno:
| Feature | Arduino Uno | ESP32 |
|---|---|---|
| PWM Channels | 6 (fixed pins) | 16 independent |
| Frequency Range | ~490Hz or ~980Hz | 1Hz - 40MHz |
| Resolution | 8-bit (0-255) | Up to 16-bit (0-65535) |
| Pin Flexibility | Specific pins only | ANY GPIO pin |
analogWrite(). You must configure PWM channels first!
📊 Understanding PWM Duty Cycle
The chart below shows how different duty cycles affect the average voltage output: