Built-in project · Raspberry Pi Pico W

Pico W LED bar graph fills and empties

MicroPython on a Pico W: a ten-segment LED bar on GP6 to GP15, a 220 ohm on each segment, fills up one segment at a time and then empties. The whole build, a Raspberry Pi Pico W 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 $7.30. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Driving ten LEDs with a list of pins
  • Why each LED needs its own resistor
  • Looping through a list in MicroPython
  • Timing a pattern with a delay
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 12

How it works

Ten Pico W pins, GP6 to GP15, each go through a 220 ohm resistor to one segment of an LED bar. The other end of every segment goes to ground, so a pin set high lights its segment.

The program lights the segments one by one, 200 ms apart, until the bar is full. Then it turns them off in the same order. It prints full when the bar fills and empty when it has gone out, and repeats forever.

Parts list

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

Kits that can build it

These 2 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 pinConnects to
Resistor, 220 Ω (1) pin 2Raspberry Pi Pico W pin GP6
Resistor, 220 Ω (2) pin 2Raspberry Pi Pico W pin GP7
Resistor, 220 Ω (3) pin 2Raspberry Pi Pico W pin GP8
Resistor, 220 Ω (4) pin 2Raspberry Pi Pico W pin GP9
Resistor, 220 Ω (5) pin 2Raspberry Pi Pico W pin GP10
Resistor, 220 Ω (6) pin 2Raspberry Pi Pico W pin GP11
Resistor, 220 Ω (7) pin 2Raspberry Pi Pico W pin GP12
Resistor, 220 Ω (8) pin 2Raspberry Pi Pico W pin GP13
Resistor, 220 Ω (9) pin 2Raspberry Pi Pico W pin GP14
Resistor, 220 Ω (10) pin 2Raspberry Pi Pico W pin GP15
Resistor, 220 Ω (10) pin 1LED bar graph, 10 segments pin A1
Resistor, 220 Ω (9) pin 1LED bar graph, 10 segments pin A2
Resistor, 220 Ω (8) pin 1LED bar graph, 10 segments pin A3
Resistor, 220 Ω (7) pin 1LED bar graph, 10 segments pin A4
Resistor, 220 Ω (6) pin 1LED bar graph, 10 segments pin A5
Resistor, 220 Ω (5) pin 1LED bar graph, 10 segments pin A6
Resistor, 220 Ω (4) pin 1LED bar graph, 10 segments pin A7
Resistor, 220 Ω (3) pin 1LED bar graph, 10 segments pin A8
Resistor, 220 Ω (2) pin 1LED bar graph, 10 segments pin A9
Resistor, 220 Ω (1) pin 1LED 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 · Raspberry Pi Pico W
# Display the level: the ten segments of the LED bar on GP6 to GP15
# light one after another, a fifth of a second apart, until the bar is
# full, then go out the same way.
from machine import Pin
import time

segments = [Pin(n, Pin.OUT, value=0) for n in range(6, 16)]

while True:
    for state in (1, 0):
        for seg in segments:
            seg.value(state)
            time.sleep_ms(200)
        print("full" if state else "empty")

Troubleshooting

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

  • One segment never lights This project
    Check that segment's resistor and its wire to the matching GP pin.
  • The bar fills in a jumbled order This project
    Wire GP6 through GP15 to the segments in order so the fill runs end to end.
  • 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.
  • Pins are powered from 5 V, or a 5 V signal goes into a GPIO. The RP2040 pins are 3.3 V only and are not 5 V tolerant. Raspberry Pi Pico W
    Power the board from VBUS or VSYS at 5 V, but keep GPIO signals at 3.3 V. Use a level shifter or a divider for 5 V parts.
  • WiFi code fails to connect, or the on-board LED code does nothing. The LED on the Pico W is not on GP25 like on the plain Pico. Raspberry Pi Pico W
    Use the board's LED_BUILTIN name so the library drives it through the WiFi chip. Join a 2.4 GHz network only, with the right SSID and password, and wait for the link before sending.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.