Pico chaser
A dot sweeps back and forth along six LEDs on GP2 to GP7, one GPIO each. The whole build, a Raspberry Pi Pico and 12 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 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $9.90. On Mokxi they're free.
What you'll learn
- Driving several LEDs from separate GPIO pins
- Arrays of pin numbers
- Using a
forloop to sweep a pattern - One resistor per LED
- Timing a pattern with
delay
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Raspberry Pi Pico
- Parts: 14
How it works
Six red LEDs sit on GP2 to GP7, each through its own 220 ohm resistor to a shared ground. One lit dot sweeps along the row and back, like a scanner.
The sketch keeps the six pins in an array. A helper turns on exactly one LED and turns the other five off. The loop walks the dot from the first LED to the last, then back toward the first.
The dot turns around one LED short of each end. So the two end LEDs light once per pass and the middle four light twice. Each step lasts 90 ms.
Parts list
14 parts, plus the jumper wires. They retail for about $9.90; on Mokxi they're free.
- 1 × Raspberry Pi PicoIn Fight Stick Kit.
- 1 × Full-size breadboard
- 6 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 6 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
Kits that can build it
These 4 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 Ω (1) pin 1 | Raspberry Pi Pico pin GP2 |
| Resistor, 220 Ω (2) pin 1 | Raspberry Pi Pico pin GP3 |
| Resistor, 220 Ω (3) pin 1 | Raspberry Pi Pico pin GP4 |
| Resistor, 220 Ω (4) pin 1 | Raspberry Pi Pico pin GP5 |
| LED, red (1) pin C | Raspberry Pi Pico pin GND |
| LED, red (2) pin C | Raspberry Pi Pico pin GND |
| LED, red (3) pin C | Raspberry Pi Pico pin GND |
| LED, red (4) pin C | Raspberry Pi Pico pin GND |
| LED, red (5) pin C | Raspberry Pi Pico pin GND |
| LED, red (6) pin C | Raspberry Pi Pico pin GND |
| Resistor, 220 Ω (5) pin 1 | Raspberry Pi Pico pin GP6 |
| Resistor, 220 Ω (6) pin 1 | Raspberry Pi Pico pin GP7 |
| LED, red (1) pin A | Resistor, 220 Ω (1) pin 2 |
| LED, red (2) pin A | Resistor, 220 Ω (2) pin 2 |
| LED, red (3) pin A | Resistor, 220 Ω (3) pin 2 |
| LED, red (4) pin A | Resistor, 220 Ω (4) pin 2 |
| LED, red (5) pin A | Resistor, 220 Ω (5) pin 2 |
| LED, red (6) pin A | Resistor, 220 Ω (6) pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Chaser: a dot sweeping back and forth along six LEDs.
//
// One GPIO per lamp: GP2 to GP7, in order along the Pico's left header. No
// shift register, no PWM, just six pinMode calls and a loop, the same
// circuit as the ESP32-C3 Knight Rider example, wired to a different board.
//
// The sweep turns round one short of each end, so the two end lamps light
// once a pass and the middle ones twice.
const int LAMPS[] = {2, 3, 4, 5, 6, 7};
const int COUNT = 6;
const int STEP_MS = 90;
static void show(int lit) {
for (int i = 0; i < COUNT; i++) {
digitalWrite(LAMPS[i], i == lit ? HIGH : LOW);
}
}
void setup() {
for (int i = 0; i < COUNT; i++) {
pinMode(LAMPS[i], OUTPUT);
digitalWrite(LAMPS[i], LOW);
}
Serial.begin(115200);
Serial.println("Mokxi Pico: six lamps, one dot");
}
void loop() {
for (int i = 0; i < COUNT; i++) {
show(i);
delay(STEP_MS);
}
for (int i = COUNT - 2; i > 0; i--) {
show(i);
delay(STEP_MS);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
show sketch.ino
It takes the index of the LED that should be lit. A loop visits all six pins and writes HIGH to the one that matches and LOW to the rest. Calling it once per step guarantees only one LED is on at a time.
static void show(int lit) {
for (int i = 0; i < COUNT; i++) {
digitalWrite(LAMPS[i], i == lit ? HIGH : LOW);
}
}setup sketch.ino
A loop sets every pin in LAMPS as an output and starts it LOW so all LEDs begin dark. Then it opens serial at 115200 and prints a short start message.
void setup() {
for (int i = 0; i < COUNT; i++) {
pinMode(LAMPS[i], OUTPUT);
digitalWrite(LAMPS[i], LOW);
}
Serial.begin(115200);
Serial.println("Mokxi Pico: six lamps, one dot");
}loop sketch.ino
The first loop moves the dot from index 0 up to 5. The second runs from index 4 down to 1, stopping before the first LED so the end LEDs are not lit twice in a row. Each step calls show and then waits STEP_MS (90 ms).
void loop() {
for (int i = 0; i < COUNT; i++) {
show(i);
delay(STEP_MS);
}
for (int i = COUNT - 2; i > 0; i--) {
show(i);
delay(STEP_MS);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The dot jumps out of order This project
The LEDs must be wired in the same order as the pins inLAMPS: GP2, GP3, GP4, GP5, GP6, GP7. - An LED never lights This project
Check the LED is the right way around, with the anode on the resistor and the cathode on 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 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.