MicroPython flowing light on a Pico’s LED bar graph
MicroPython on a Raspberry Pi Pico: a ten-bar LED graph on GP16 to GP22 and GP26 to GP28 lights one bar at a time, along the graph and back. The whole build, a Raspberry Pi Pico 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 $5.30. On Mokxi they're free.
What you'll learn
- Controlling ten outputs from a list of pins
- Lighting one LED at a time by comparing an index
- Building a back-and-forth sequence from two ranges
- Using one resistor per LED
- Difficulty: Beginner
- Time: About 20 minutes
- Board: Raspberry Pi Pico
- Parts: 12
How it works
Ten Pico pins, GP16 to GP22 and GP26 to GP28, each go through a 220 ohm resistor to one segment of the LED bar. The other ends of the segments go to ground.
The program lights exactly one bar at a time, for a tenth of a second each. The light runs from the first bar to the tenth, then back down the other way. After each full sweep it prints sweep.
Parts list
12 parts, plus the jumper wires. They retail for about $5.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 pin | Connects to |
|---|---|
| Resistor, 220 Ω (1) pin 1 | Raspberry Pi Pico pin GP28 |
| Resistor, 220 Ω (2) pin 1 | Raspberry Pi Pico pin GP27 |
| Resistor, 220 Ω (3) pin 1 | Raspberry Pi Pico pin GP26 |
| Resistor, 220 Ω (4) pin 1 | Raspberry Pi Pico pin GP22 |
| Resistor, 220 Ω (5) pin 1 | Raspberry Pi Pico pin GP21 |
| Resistor, 220 Ω (6) pin 1 | Raspberry Pi Pico pin GP20 |
| Resistor, 220 Ω (7) pin 1 | Raspberry Pi Pico pin GP19 |
| Resistor, 220 Ω (8) pin 1 | Raspberry Pi Pico pin GP18 |
| Resistor, 220 Ω (9) pin 1 | Raspberry Pi Pico pin GP17 |
| Resistor, 220 Ω (10) pin 1 | Raspberry Pi Pico pin GP16 |
| Resistor, 220 Ω (1) pin 2 | LED bar graph, 10 segments pin A1 |
| Resistor, 220 Ω (2) pin 2 | LED bar graph, 10 segments pin A2 |
| Resistor, 220 Ω (3) pin 2 | LED bar graph, 10 segments pin A3 |
| Resistor, 220 Ω (4) pin 2 | LED bar graph, 10 segments pin A4 |
| Resistor, 220 Ω (5) pin 2 | LED bar graph, 10 segments pin A5 |
| Resistor, 220 Ω (6) pin 2 | LED bar graph, 10 segments pin A6 |
| Resistor, 220 Ω (7) pin 2 | LED bar graph, 10 segments pin A7 |
| Resistor, 220 Ω (8) pin 2 | LED bar graph, 10 segments pin A8 |
| Resistor, 220 Ω (9) pin 2 | LED bar graph, 10 segments pin A9 |
| Resistor, 220 Ω (10) pin 2 | LED bar graph, 10 segments pin A10 |
| LED bar graph, 10 segments pin C10 | Ground |
| LED bar graph, 10 segments pin C9 | Ground |
| LED bar graph, 10 segments pin C8 | Ground |
| LED bar graph, 10 segments pin C7 | Ground |
| LED bar graph, 10 segments pin C6 | Ground |
| LED bar graph, 10 segments pin C5 | Ground |
| LED bar graph, 10 segments pin C4 | Ground |
| LED bar graph, 10 segments pin C3 | Ground |
| LED bar graph, 10 segments pin C2 | Ground |
| LED bar graph, 10 segments pin C1 | 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.
# Flowing light: one bar of the LED bar graph lit at a time, along the
# bar and back, a tenth of a second each.
from machine import Pin
import time
pins = [16, 17, 18, 19, 20, 21, 22, 26, 27, 28]
leds = [Pin(n, Pin.OUT, value=0) for n in pins]
def light(i):
for n, led in enumerate(leds):
led.value(n == i)
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
Goes through every LED in leds and sets it on only if its position matches i. So calling light(3) lights bar 3 and turns every other bar off.
def light(i):
for n, led in enumerate(leds):
led.value(n == i)Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The light jumps instead of flowing This project
Check thepinslist matches your wiring. The pins skip from GP22 to GP26. - No bar lights This project
Check each segment's resistor and that the cathode pins go to ground. - 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 Pico does not show up as a drive, or the program will not load. Raspberry Pi Pico
Hold the BOOTSEL button while you plug in USB. A drive named RPI-RP2 appears. Then copy the UF2 file onto it. - A 5 V signal or sensor is wired to a GPIO pin. Raspberry Pi Pico
Pico GPIO pins are 3.3 V only and are not 5 V tolerant. Use a level shifter or a voltage divider for any 5 V output going into the board.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.