MicroPython breathing LED with PWM on GPIO 2, on an ESP32-DevKitC V4
MicroPython on an ESP32: machine.PWM at 10 kHz on GPIO 2 ramps the duty up and down, so the LED fades in and out like breathing. The whole build, an ESP32-DevKitC V4 and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $10.15. On Mokxi they're free.
What you'll learn
- PWM and duty cycle
- Dimming an LED with machine.PWM
- Sweeping a value up and down with range
- The 0 to 1023 duty scale
- Difficulty: Beginner
- Time: About 10 minutes
- Board: ESP32-DevKitC V4
- Parts: 3
How it works
PWM turns a pin on and off very fast. Here it runs at 10 kHz on GPIO 2. The share of time the pin is on is the duty, and a higher duty makes the LED look brighter.
The loop raises the duty from 0 toward 1023 in steps of 8, with a 4 millisecond pause between each. Then it steps back down. The LED fades in and out, and the script prints breath after each cycle.
Parts list
3 parts, plus the jumper wires. They retail for about $10.15; on Mokxi they're free.
Kits that can build it
These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 220 Ω pin 1 | ESP32-DevKitC V4 pin 2 |
| LED, red pin A | Resistor, 220 Ω pin 2 |
| LED, red pin C | Ground |
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# Breathing LED: PWM on GPIO 2 at 10 kHz. The duty climbs from 0 to
# 1023 and falls back, so the LED fades in and out.
from machine import Pin, PWM
import time
led = PWM(Pin(2), freq=10000, duty=0)
while True:
for duty in range(0, 1024, 8):
led.duty(duty)
time.sleep_ms(4)
for duty in range(1023, -1, -8):
led.duty(duty)
time.sleep_ms(4)
print("breath")
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED just stays on or off This project
Check the wiring from GPIO 2 through the 220 ohm resistor to the LED. The duty must change over time for the fade to show. - The fade is too fast This project
Raise thetime.sleep_ms(4)value or lower the step size of 8. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED. - The board is not found, or upload fails with a connecting message. ESP32-DevKitC V4
Use a USB cable that carries data, not charge only. Pick the ESP32 Dev Module board. If it still fails, hold BOOT while uploading starts, then release. - Some pins never read right, or the board will not boot with a wire attached. ESP32-DevKitC V4
GPIO34 to 39 are input only and have no pull-ups. GPIO6 to 11 run the flash, so leave them alone. GPIO0, 2 and 12 are boot pins; avoid pulling them the wrong way at reset.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.