Skip to content

Embedded Systems

How to make an Arduino LED chaser with an array

An Arduino LED chaser lights a row of LEDs one after another, so the light seems to run along the row. Keep the pin numbers in an array, then let a for loop step through it: light one, wait, switch it off, move to the next. Four LEDs or forty, the code stays the same length.

A list of pins

int pins[] = {9, 10, 11, 12};
// pins[0] is 9, pins[1] is 10, pins[2] is 11, pins[3] is 12

Places in an array count from 0, so a four-item list runs from pins[0] to pins[3]. Wire each pin through a 220 Ω resistor to an LED, and each LED to GND.

A for loop visits each one

A for loop has three parts: where the count starts, when to keep going, and how it changes. This one runs its block for i = 0, 1, 2 and 3:

void setup() {
  for (int i = 0; i < 4; i = i + 1) {
    pinMode(pins[i], OUTPUT);       // all four pins in one line
  }
}

void loop() {
  for (int i = 0; i < 4; i = i + 1) {
    digitalWrite(pins[i], HIGH);
    delay(200);
    digitalWrite(pins[i], LOW);
  }
}

That's a chase: each step takes 200 ms, so a lap of four takes 0.8 s and the light runs round about 1.25 times a second. Halve the wait to 100 ms and it runs twice as fast.

Live experiment

A chase, then a bounce

Four LEDs on pins 9 to 12, kept in a list. A for loop counts i = 0, 1, 2, 3 and lights pins[i] in turn. Pick a chase or a bounce, 200, 100 or 50 ms a step, and tap the empty socket to add a fifth LED on pin 13.

Try it yourself: free lesson

“Many lights” is lesson 5 of 6 in Clockwork Cove, Embedded Systems World 2. Free with an account. Lessons unlock in order, so you’ll do the 11 before it first.

Bounce back like a scanner

To bounce, add a second loop that counts down. The two ends have just had their turn, so it runs from 2 to 1, not 3 to 0:

for (int i = 2; i >= 1; i = i - 1) {
  digitalWrite(pins[i], HIGH);
  delay(200);
  digitalWrite(pins[i], LOW);
}

Up then back gives 0, 1, 2, 3, 2, 1, and round again: six steps a lap, each the same length.

Add a light without rewriting

To add a fifth LED on pin 13, put 13 in the list and change i < 4 to i < 5 in each loop. Nothing else changes, because the loops visit whatever the list holds. Reading past the end, like pins[4] in a four-item list, isn't caught by the compiler: it reads whatever memory comes next, so keep the count and the list in step.

The chase uses delay(), which is fine while it's the only job. Give the board a button to read as well, and time each step with millis() instead.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Many lights” is lesson 5 of 6 in Clockwork Cove, Embedded Systems World 2. Free with an account. Lessons unlock in order, so you’ll do the 11 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3In int pins[] = {9, 10, 11, 12}, what is pins[2]?
    Show the answer

    Answer: 11. Places in a list are counted from 0, so pins[0] is 9, pins[1] is 10 and pins[2] is 11.

    • Why not “2”? 2 is the index, the place in the list, not the value stored there.
    • Why not “10”? 10 is pins[1]. Places count from 0, so pins[2] is the third number, 11.
    • Why not “12”? 12 is the last item, pins[3]. pins[2] is the one before it.
  2. Question 2 of 3for (int i = 0; i < 4; i = i + 1) runs its block…
    Show the answer

    Answer: Four times, with i going 0, 1, 2, 3. i starts at 0, the block runs, i goes up by one, and it stops as soon as i reaches 4. That's four passes, one for each place in a four-long list.

    • Why not “Four times a second”? A for loop runs as fast as its block allows. It counts passes, not seconds.
    • Why not “Forever”? i goes up by one each pass, and the test i < 4 fails once i reaches 4, so the loop ends.
    • Why not “Once”? The loop repeats its block while i < 4, adding 1 each time, so it runs four times, not once.
  3. Question 3 of 3The bounce comes back with for (int i = 2; i >= 1; i = i - 1). Which pins light on the way back?
    Show the answer

    Answer: 11, then 10. i goes 2, then 1, and stops before 0. pins[2] is 11 and pins[1] is 10. The ends, 9 and 12, have just had their turn, so they don't light twice.

    • Why not “9, then 12”? i counts down from 2 to 1, which are pins[2] and pins[1], 11 and 10. The ends had their turn on the way up.
    • Why not “12, 11, 10, then 9”? The loop starts at i = 2 and stops before 0, so pins[3] and pins[0], 12 and 9, don't light on the way back.
    • Why not “Only 9”? 9 is pins[0], and the loop stops when i drops below 1, so 9 doesn't light on the way back.
Keep practising: free lesson

“Many lights” is lesson 5 of 6 in Clockwork Cove, Embedded Systems World 2. Free with an account. Lessons unlock in order, so you’ll do the 11 before it first.

Questions people ask

Why do Arduino arrays start at 0?

C and C++, which Arduino uses, number an array's places by how far they are from the start. The first item is 0 places in, so it's pins[0], and a four-item list runs from pins[0] to pins[3]. There's no pins[4].

How do I add more LEDs to the chaser?

Add the pin to the list and raise the loop's count. Better still, let the code count the list itself with sizeof(pins) divided by sizeof(pins[0]), so adding a pin is the only change.

How many LEDs can an Arduino chase?

An Uno has 20 pins that can drive LEDs, and a chaser lights one at a time, so the current is never a problem. For more, a shift register like the 74HC595 adds 8 outputs from 3 pins, and chains for more.

Every topic