Built-in project · ESP32-DevKitC V4

MicroPython flowing light on a ten-segment LED bar, on an ESP32-DevKitC V4

MicroPython on an ESP32: a ten-segment LED bar on GPIO 15, 2, 0, 4, 5, 18, 19, 21, 22 and 23 lights one segment at a time, up the bar and back. The whole build, an ESP32-DevKitC V4 and 11 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 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $11.30. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • MicroPython on an ESP32
  • Controlling ten outputs from a list of pins
  • One resistor per LED
  • Lists, enumerate and range in Python
  • Timing a pattern with time.sleep_ms
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: ESP32-DevKitC V4
  • Parts: 12

How it works

This is MicroPython, not Arduino C++. The ESP32 runs main.py directly. Ten GPIO pins each go through a 220 ohm resistor to one segment of the LED bar, and all the bar's cathodes go to ground.

The program lights one segment at a time, from segment 1 up to segment 10 and then back down to segment 2, a tenth of a second each. That makes a light that flows back and forth. After each full sweep it prints sweep.

Parts list

12 parts, plus the jumper wires. They retail for about $11.30; on Mokxi they're free.

Kits that can build it

This starter kit has 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 pinConnects to
Resistor, 220 Ω (10) pin 1ESP32-DevKitC V4 pin 23
Resistor, 220 Ω (9) pin 1ESP32-DevKitC V4 pin 22
Resistor, 220 Ω (8) pin 1ESP32-DevKitC V4 pin 21
Resistor, 220 Ω (7) pin 1ESP32-DevKitC V4 pin 19
Resistor, 220 Ω (6) pin 1ESP32-DevKitC V4 pin 18
Resistor, 220 Ω (5) pin 1ESP32-DevKitC V4 pin 5
Resistor, 220 Ω (4) pin 1ESP32-DevKitC V4 pin 4
Resistor, 220 Ω (3) pin 1ESP32-DevKitC V4 pin 0
Resistor, 220 Ω (2) pin 1ESP32-DevKitC V4 pin 2
Resistor, 220 Ω (1) pin 1ESP32-DevKitC V4 pin 15
Resistor, 220 Ω (10) pin 2LED bar graph, 10 segments pin A1
Resistor, 220 Ω (9) pin 2LED bar graph, 10 segments pin A2
Resistor, 220 Ω (8) pin 2LED bar graph, 10 segments pin A3
Resistor, 220 Ω (7) pin 2LED bar graph, 10 segments pin A4
Resistor, 220 Ω (6) pin 2LED bar graph, 10 segments pin A5
Resistor, 220 Ω (5) pin 2LED bar graph, 10 segments pin A6
Resistor, 220 Ω (4) pin 2LED bar graph, 10 segments pin A7
Resistor, 220 Ω (3) pin 2LED bar graph, 10 segments pin A8
Resistor, 220 Ω (2) pin 2LED bar graph, 10 segments pin A9
Resistor, 220 Ω (1) pin 2LED bar graph, 10 segments pin A10
LED bar graph, 10 segments pin C10Ground
LED bar graph, 10 segments pin C9Ground
LED bar graph, 10 segments pin C8Ground
LED bar graph, 10 segments pin C7Ground
LED bar graph, 10 segments pin C6Ground
LED bar graph, 10 segments pin C5Ground
LED bar graph, 10 segments pin C4Ground
LED bar graph, 10 segments pin C3Ground
LED bar graph, 10 segments pin C2Ground
LED bar graph, 10 segments pin C1Ground

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.

main.py · ESP32-DevKitC V4
# Flowing light: one segment of the bar graph lit at a time, from
# segment 1 to segment 10 and back, a tenth of a second each.
from machine import Pin
import time

pins = [15, 2, 0, 4, 5, 18, 19, 21, 22, 23]
leds = [Pin(n, Pin.OUT, value=0) for n in pins]

def light(i):
    for n, led in enumerate(leds):
        led.value(1 if n == i else 0)

while True:
    for i in list(range(10)) + list(range(8, 0, -1)):
        light(i)
        time.sleep_ms(100)
    print("sweep")

Code walkthrough

The key functions of the sketch, and what each one does.

light main.py

Turns on exactly one segment. It goes through every LED in the list: the one whose number matches i gets value 1, all others get 0. Calling it again with a new number moves the light.

def light(i):
    for n, led in enumerate(leds):
        led.value(1 if n == i else 0)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The light skips or runs in the wrong order This project
    The pins list in main.py must match the wiring. Segment 1 is GPIO 15 and segment 10 is GPIO 23.
  • The whole bar is dark This project
    Check that every segment's cathode goes to ground and each resistor sits between a pin and an anode.
  • Segments are dim, uneven, or the chip is damaged. LED bar graph
    Every segment is its own LED and needs its own current-limiting resistor, about 220 to 330 ohms at 5 V. One shared resistor makes brightness change with how many are lit.
  • The bar lights in reverse order or a segment never lights. LED bar graph
    Check pin 1 and the orientation. Anode and cathode sides are on opposite rows. Some bars are numbered the other way around, so test with one segment at a time.
  • 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 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.