Illustration: six red LEDs in a row in the dark, the middle two lit.Lights and displays
Illustration (made with AI): roughly what the finished project looks like.

Running Lights with Six LEDs

Six LEDs light up one after another, so a dot of light runs left and right. A neat way to learn the for loop.

Difficulty
Beginner
Time
20 min
Age
8+
Parts
4, 4 on the site

Adapted from “For Loop Iteration (aka The Knight Rider)” by Arduino. Licence: CC BY-SA 4.0. This translation is shared under the same licence. The parts list and the tips are RoboHub's.

Variations

Choose a variation and the parts list updates.

Instructions

  1. A row of LEDs

    Place six LEDs in a row on the breadboard. Connect each one's long leg through a 330 Ω resistor to pins 2 to 7, and the short legs to the ground rail.

    D2 is the board's digital pin 2, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • LED 1 + → 330 ΩArduino D2
    • LED 1 − → Arduino GND
    • LED 2 + → 330 ΩArduino D3
    • LED 2 − → Arduino GND
    • LED 3 + → 330 ΩArduino D4
    • LED 3 − → Arduino GND
    • LED 4 + → 330 ΩArduino D5
    • LED 4 − → Arduino GND
    • LED 5 + → 330 ΩArduino D6
    • LED 5 − → Arduino GND
    • LED 6 + → 330 ΩArduino D7
    • LED 6 − → Arduino GND
  2. Upload the code

    The first for loop goes through pins 2 to 7, the second from 7 back to 2.

    Arduino C++
    int timer = 100;                       // the higher the number, the slower the light runs
    
    void setup() {
      for (int pin = 2; pin < 8; pin++) {
        pinMode(pin, OUTPUT);
      }
    }
    
    void loop() {
      for (int pin = 2; pin < 8; pin++) {      // from the lowest pin to the highest
        digitalWrite(pin, HIGH);
        delay(timer);
        digitalWrite(pin, LOW);
      }
      for (int pin = 7; pin >= 2; pin--) {     // and back again
        digitalWrite(pin, HIGH);
        delay(timer);
        digitalWrite(pin, LOW);
      }
    }
  3. Change the speed

    timer is in milliseconds: 30 is very fast, 300 is slow.

    Illustration: six red LEDs in a row, the middle two lit.

    Illustration, made with AI

  • Lights and displays
    Beginner30 min8+

    A Colour-Changing Lamp with an RGB LED

    An RGB LED holds three small LEDs: red, green and blue. Mix their brightness and you get any colour, while the lamp slowly changes from one colour to the next.

    5/5 parts on the site
  • Lights and displays
    Beginner15 min8+

    Your First Program: Blink an LED

    Everyone starts here: the LED built into the board turns on and off once a second. You'll learn how to install the Arduino IDE, write code and upload it to the board.

    1/1 parts on the site
  • Lights and displays
    Beginner30 min8+

    A Traffic Light with Three LEDs

    Build a small model of a real traffic light: green, flashing green, yellow, red and finally red with yellow, just like on the street.

    4/4 parts on the site