Mission 04 · Stage 1

LED Chaser

loop index drives a moving pattern

Light one segment at a time on a bar graph using a loop index.

LED Chaser circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

LED bar graph

A1

Arduino

pin 3

LED bar graph

A2

Arduino

pin 4

LED bar graph

A3

Arduino

pin 5

LED bar graph

A4

Arduino

pin 6

LED bar graph

A5

Arduino

pin 7

LED bar graph

A6

Arduino

pin 8

LED bar graph

A7

Arduino

pin 9

LED bar graph

A8

Arduino

pin 10

LED bar graph

A9

Arduino

pin 11

LED bar graph

A10

LED bar graph

C10

Arduino

GND

See it

One lit segment at a time!

A loop index picks which LED is ON — the dot runs down the bar.

Loading bars and volume meters chase like this.

The story

The problem

You want a moving pattern, not just one blinking LED.

Think of it like

Like spotlighting one person in a line, then the next.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Ten segments you can light up

LED bar graph

Display

Loading part…

How it works

1

Clear the bar

An inner loop sets every segment LOW before lighting the next one.

digitalWrite(FIRST_PIN + j, LOW);
2

Light segment i

The outer loop variable i picks which bar segment is ON — that is the chase!

digitalWrite(FIRST_PIN + i, HIGH);
3

Pause and next

A short delay lets you see the dot move; then i increases and the chase continues.

delay(100);

Then loop back to step 1

Build the circuit

Follow these steps in order. Match the wires to the colors shown.

  1. 1

    Place Arduino

    Place the Arduino (uno) on the breadboard.

    Arduino placed!

    Loading part…
  2. 2

    Place LED bar graph

    Place the LED bar graph (bar1) on the breadboard.

    Loading part…
  3. 3

    Connect Arduino pin 2 to LED bar graph (bar1) A1

    Connect Arduino pin 2 to LED bar graph (bar1) A1.

  4. 4

    Connect Arduino pin 3 to LED bar graph (bar1) A2

    Connect Arduino pin 3 to LED bar graph (bar1) A2.

  5. 5

    Connect Arduino pin 4 to LED bar graph (bar1) A3

    Connect Arduino pin 4 to LED bar graph (bar1) A3.

  6. 6

    Connect Arduino pin 5 to LED bar graph (bar1) A4

    Connect Arduino pin 5 to LED bar graph (bar1) A4.

  7. 7

    Connect Arduino pin 6 to LED bar graph (bar1) A5

    Connect Arduino pin 6 to LED bar graph (bar1) A5.

  8. 8

    Connect Arduino pin 7 to LED bar graph (bar1) A6

    Connect Arduino pin 7 to LED bar graph (bar1) A6.

  9. 9

    Connect Arduino pin 8 to LED bar graph (bar1) A7

    Connect Arduino pin 8 to LED bar graph (bar1) A7.

  10. 10

    Connect Arduino pin 9 to LED bar graph (bar1) A8

    Connect Arduino pin 9 to LED bar graph (bar1) A8.

  11. 11

    Connect Arduino pin 10 to LED bar graph (bar1) A9

    Connect Arduino pin 10 to LED bar graph (bar1) A9.

  12. 12

    Connect Arduino pin 11 to LED bar graph (bar1) A10

    Connect Arduino pin 11 to LED bar graph (bar1) A10.

  13. 13

    Connect LED bar graph (bar1) C10 to Arduino GND

    Connect LED bar graph (bar1) C10 to Arduino GND.

Try it

  • Run and follow the lit segment from left to right.
  • Only one bar LED should be on at a time.

Peek at code

Pin range

const int FIRST_PIN = 2;
const int NUM_LEDS = 10;

FIRST_PIN is the first bar segment; NUM_LEDS is how many segments the loop uses.

Setup all outputs

void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(FIRST_PIN + i, OUTPUT);
  }
}

A for-loop calls pinMode on pins 2 through 11 so each segment can be driven.

Chase loop

void loop() {
  for (int i = 0; i < NUM_LEDS; i++) {
    for (int j = 0; j < NUM_LEDS; j++) {
      digitalWrite(FIRST_PIN + j, LOW);
    }
    digitalWrite(FIRST_PIN + i, HIGH);
    delay(100);
  }
}

Outer loop picks i; inner loop clears all LEDs; then one segment turns on. That is how the dot runs along the bar.

Show full sketch (led-chaser.ino)
const int FIRST_PIN = 2;
const int NUM_LEDS = 10;
void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(FIRST_PIN + i, OUTPUT);
  }
}
void loop() {
  for (int i = 0; i < NUM_LEDS; i++) {
    for (int j = 0; j < NUM_LEDS; j++) {
      digitalWrite(FIRST_PIN + j, LOW);
    }
    digitalWrite(FIRST_PIN + i, HIGH);
    delay(100);
  }
}

Quick quiz

Q1. Where does repeating work belong?

  • A. loop()
  • B. setup()
  • C. pinMode only
Why: Correct—loop() runs again and again.

Q2. What does the loop variable i control in this sketch?

  • A. Which LED is lit
  • B. How bright the bar is
  • C. The random seed
Why: Correct — i is the index that selects one output pin.

Code lab — try on your own

  1. Make the chase faster — change delay(100) to delay(50).

    Hint: There is only one delay() inside loop().

  2. Add a comment on the line that lights one segment (digitalWrite with + i).

    Hint: Use // at the end of that line.

Code walkthrough

A line-by-line tour of the sketch — the same steps as in Robo Gurukul Studio.

Program overview

Technical

Sketches have globals, then setup() once, then loop() forever.

In this project

Light one segment at a time on a bar graph using a loop index.

Why here

Read from top to bottom. Hover words or lines for help!

const int FIRST_PIN = 2;
const int NUM_LEDS = 10;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for LED Chaser.

Why here

One-time setup belongs here—not in loop().

void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(FIRST_PIN + i, OUTPUT);
  }
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in LED Chaser.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  for (int i = 0; i < NUM_LEDS; i++) {
    for (int j = 0; j < NUM_LEDS; j++) {
      digitalWrite(FIRST_PIN + j, LOW);
    }
    digitalWrite(FIRST_PIN + i, HIGH);
    delay(100);
  }
}

Try this: Change numbers in loop(), then compile and run the simulator.

pinMode

Technical

Tells a pin if it listens or drives something.

In this project

Gets the LED Chaser circuit ready in the simulator.

Why here

Goes in setup() because we only set pins once at the start.

    pinMode(FIRST_PIN + i, OUTPUT);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in LED Chaser.

Why here

Goes in loop() so it can keep changing while the program runs.

      digitalWrite(FIRST_PIN + j, LOW);

delay

Technical

Waits for some time. Nothing else runs during the wait.

In this project

Controls speed so you can see LED Chaser in the simulator.

Why here

Right after an action that should stay the same for a moment.

    delay(100);