MicroPython blink on ESP32 GPIO 2, on an ESP32-DevKitC V4
MicroPython on an ESP32: machine.Pin turns an LED on GPIO 2 on for a second and off for a second, and prints each change to the Serial Monitor. 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
- Controlling a pin with machine.Pin
- Writing values 1 and 0 to an output
- Timing with time.sleep
- Running a loop forever with while True
- Difficulty: Beginner
- Time: About 10 minutes
- Board: ESP32-DevKitC V4
- Parts: 3
How it works
GPIO 2 goes through a 220 ohm resistor to a red LED, and the LED's other leg goes to ground. In MicroPython, Pin(2, Pin.OUT) makes GPIO 2 an output.
The script sets the pin to 1, prints LED on and waits a second. Then it sets 0, prints LED off and waits a second. The DevKitC's own blue LED is also on GPIO 2, so both blink together.
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.
# Blink: GPIO 2 drives the LED, on for a second, off for a second.
# The DevKitC's own blue LED is on GPIO 2 too, so both blink together.
from machine import Pin
import time
led = Pin(2, Pin.OUT)
while True:
led.value(1)
print("LED on")
time.sleep(1)
led.value(0)
print("LED off")
time.sleep(1)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The breadboard LED stays dark This project
Check that the LED anode (long leg) is on the resistor side and the cathode goes to ground. - 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.