Analog input & PWM · Lesson
10 min read
PWM: fading an LED
How quickly switching a pin on and off looks like a dimmer.
What You Will Learn
- Explain duty cycle
- Use analogWrite(pin, 0–255)
- Know which pins support PWM
Digital pins can only be 0 V or 5 V. PWM (pulse-width modulation) switches the pin hundreds of times per second. The fraction of time it's on — the duty cycle — sets the average power. Your eye sees a dimmer LED.
| analogWrite value | Duty cycle | LED |
|---|---|---|
| 0 | 0 % | off |
| 64 | 25 % | dim |
| 128 | 50 % | medium |
| 255 | 100 % | full |
Fade with PWMarduino-uno
Note: The LED breathes. Try a different step size, or move it to pin 7 (no PWM) and read the warning.
On the Uno, PWM runs at about 490 Hz (980 Hz on pins 5 and 6). The ESP32 uses LEDC with a frequency you choose.
Check Your Understanding
Test what you learned in this lesson