Flowing lights on pins 2 to 9, on an Arduino Uno
Eight LEDs on pins 2 to 9, each through 220 ohm: a for loop lights them one after another, up the row and back down. The whole build, an Arduino Uno and 16 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 $33. On Mokxi they're free.
What you'll learn
- Using a
forloop to repeat work across pins - Setting many pins as outputs in one pass
- Constants for first, last and speed
- Counting up and then down
- One resistor per LED
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 18
How it works
Eight red LEDs sit on pins 2 to 9, each with its own 220 ohm resistor and a shared ground. The pins are consecutive, so one for loop can walk along the row using the pin number as the loop counter.
Each LED turns on for 100 ms, then off, and the light moves to the next pin. It runs up from pin 2 to pin 9, then back down from pin 9 to pin 2. Then it repeats.
Only one LED is on at a time, so the Uno's supply is never strained.
Parts list
18 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 8 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 8 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
Kits that can build it
These 12 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Arduino Starter Kit
- Freenove Ultimate Starter Kit
- ELEGOO UNO R3 Basic Starter Kit
- SunFounder Starter Kit for Arduino Uno R3
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- LAFVIN Super Starter Kit
- keyestudio Super Starter Kit
- Vilros Arduino Uno 3 Ultimate Starter Kit
- Adafruit MetroX Classic Kit
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 |
|---|---|
| LED, red (1) pin C | Arduino Uno R3 pin GND |
| LED, red (2) pin C | Arduino Uno R3 pin GND |
| LED, red (3) pin C | Arduino Uno R3 pin GND |
| LED, red (4) pin C | Arduino Uno R3 pin GND |
| LED, red (5) pin C | Arduino Uno R3 pin GND |
| LED, red (6) pin C | Arduino Uno R3 pin GND |
| LED, red (7) pin C | Arduino Uno R3 pin GND |
| LED, red (8) pin C | Arduino Uno R3 pin GND |
| Resistor, 220 Ω (1) pin 1 | Arduino Uno R3 pin 9 |
| Resistor, 220 Ω (2) pin 1 | Arduino Uno R3 pin 8 |
| Resistor, 220 Ω (3) pin 1 | Arduino Uno R3 pin 7 |
| Resistor, 220 Ω (4) pin 1 | Arduino Uno R3 pin 6 |
| Resistor, 220 Ω (5) pin 1 | Arduino Uno R3 pin 5 |
| Resistor, 220 Ω (6) pin 1 | Arduino Uno R3 pin 4 |
| Resistor, 220 Ω (7) pin 1 | Arduino Uno R3 pin 3 |
| Resistor, 220 Ω (8) pin 1 | Arduino Uno R3 pin 2 |
| 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 |
| LED, red (7) pin A | Resistor, 220 Ω (7) pin 2 |
| LED, red (8) pin A | Resistor, 220 Ω (8) pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Flowing lights: eight LEDs on pins 2 to 9, lit one after another.
//
// pins 2 to 9 -> one 220 ohm resistor and one LED each -> GND
//
// Because the LEDs sit on pins with consecutive numbers, one for loop can walk
// along them: the loop variable is the pin. Up the row, then back down.
const int FIRST = 2;
const int LAST = 9;
const int STEP_MS = 100;
void setup() {
for (int pin = FIRST; pin <= LAST; pin++) pinMode(pin, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: flowing lights on pins 2 to 9");
}
void loop() {
for (int pin = FIRST; pin <= LAST; pin++) {
digitalWrite(pin, HIGH);
delay(STEP_MS);
digitalWrite(pin, LOW);
}
for (int pin = LAST; pin >= FIRST; pin--) {
digitalWrite(pin, HIGH);
delay(STEP_MS);
digitalWrite(pin, LOW);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
A for loop runs from FIRST (2) to LAST (9) and calls pinMode on each pin to make it an output. That is eight pins set in one line. It then opens serial at 9600 and prints a start message.
void setup() {
for (int pin = FIRST; pin <= LAST; pin++) pinMode(pin, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: flowing lights on pins 2 to 9");
}loop sketch.ino
Two for loops run back to back. The first goes up from FIRST to LAST, and the second goes down from LAST to FIRST. In each step the pin goes HIGH, waits STEP_MS (100 ms), then goes LOW. Change STEP_MS to change the speed.
void loop() {
for (int pin = FIRST; pin <= LAST; pin++) {
digitalWrite(pin, HIGH);
delay(STEP_MS);
digitalWrite(pin, LOW);
}
for (int pin = LAST; pin >= FIRST; pin--) {
digitalWrite(pin, HIGH);
delay(STEP_MS);
digitalWrite(pin, LOW);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- One LED never lights This project
Check that LED's wiring: pin to resistor, resistor to the anode, and the cathode to ground. LEDs only work one way around. - The lights flow in the wrong order This project
The order follows the pin numbers. Make sure your LEDs are wired in the same order on pins 2 to 9. - 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. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.