Built-in project · Raspberry Pi Pico

MicroPython meteor light on a Pico’s LED bar, PWM

MicroPython on a Raspberry Pi Pico: PWM on all ten pins of an LED bar graph draws a bright head with a fading tail that runs along it 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.

Intermediate About 25 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.

The circuit runs here once the page has loaded.

What you'll learn

  • Using PWM to set an LED to many brightness levels
  • Why brightness steps of a quarter look even to the eye
  • Drawing a moving pattern with a head and a tail
  • Looping through a list of pins with a list comprehension
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Raspberry Pi Pico
  • Parts: 12

How it works

The Pico drives all ten segments of the LED bar graph from its own pin, GP16 to GP22 and GP26 to GP28, each through a 220 ohm resistor. Every pin uses PWM at 10 kHz, so each segment can be dim, not just on or off.

A meteor is a bright head with a fading tail behind it. The tail table has five levels, each a quarter as bright as the one before it. The head moves one segment every 60 ms, and the five segments behind it get the dimmer levels.

The head runs from one end off the far end, then back. The script prints meteor at the end of each round trip.

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 pinConnects to
Resistor, 220 Ω (1) pin 1Raspberry Pi Pico pin GP28
Resistor, 220 Ω (2) pin 1Raspberry Pi Pico pin GP27
Resistor, 220 Ω (3) pin 1Raspberry Pi Pico pin GP26
Resistor, 220 Ω (4) pin 1Raspberry Pi Pico pin GP22
Resistor, 220 Ω (5) pin 1Raspberry Pi Pico pin GP21
Resistor, 220 Ω (6) pin 1Raspberry Pi Pico pin GP20
Resistor, 220 Ω (7) pin 1Raspberry Pi Pico pin GP19
Resistor, 220 Ω (8) pin 1Raspberry Pi Pico pin GP18
Resistor, 220 Ω (9) pin 1Raspberry Pi Pico pin GP17
Resistor, 220 Ω (10) pin 1Raspberry Pi Pico pin GP16
Resistor, 220 Ω (1) pin 2LED bar graph, 10 segments pin A1
Resistor, 220 Ω (2) pin 2LED bar graph, 10 segments pin A2
Resistor, 220 Ω (3) pin 2LED bar graph, 10 segments pin A3
Resistor, 220 Ω (4) pin 2LED bar graph, 10 segments pin A4
Resistor, 220 Ω (5) pin 2LED bar graph, 10 segments pin A5
Resistor, 220 Ω (6) pin 2LED bar graph, 10 segments pin A6
Resistor, 220 Ω (7) pin 2LED bar graph, 10 segments pin A7
Resistor, 220 Ω (8) pin 2LED bar graph, 10 segments pin A8
Resistor, 220 Ω (9) pin 2LED bar graph, 10 segments pin A9
Resistor, 220 Ω (10) 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 · Raspberry Pi Pico
# A meteor: a bright head and a fading tail of PWM levels run along the
# LED bar graph and back. Every one of the ten pins has a PWM channel.
from machine import Pin, PWM
import time

pins = [16, 17, 18, 19, 20, 21, 22, 26, 27, 28]
leds = [PWM(Pin(n)) for n in pins]
for led in leds:
    led.freq(10000)

tail = [65535, 16384, 4096, 1024, 256]   # each step a quarter of the last

def draw(head, step):
    for n, led in enumerate(leds):
        behind = (head - n) * step       # how far n is behind the head
        led.duty_u16(tail[behind] if 0 <= behind < len(tail) else 0)

while True:
    for head in range(10 + len(tail)):
        draw(head, 1)
        time.sleep_ms(60)
    for head in range(9, -len(tail) - 1, -1):
        draw(head, -1)
        time.sleep_ms(60)
    print("meteor")

Code walkthrough

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

draw main.py

Draws one frame. For each LED it works out how far behind the head it is, using head - n times step. If that distance is inside the tail list, the LED gets that brightness with duty_u16. Otherwise it is off. The step of 1 or -1 flips the tail's direction.

def draw(head, step):
    for n, led in enumerate(leds):
        behind = (head - n) * step       # how far n is behind the head
        led.duty_u16(tail[behind] if 0 <= behind < len(tail) else 0)

Troubleshooting

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

  • Some segments do not light. This project
    Check each segment's resistor and its pin. The list in the code skips from GP22 to GP26.
  • The tail is missing or the segments flicker. This project
    Check that every segment's cathode goes to ground, and that no pin is shared.
  • 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.