Tutorial 4: Arduino Knight Rider

Created on: 31 July 2012

In this tutorial, eight LEDs are interfaced to the Arduino Uno board. This is not complicated – it is just like interfacing a single LED to the Arduino as done in tutorial 3, but eight times over. A program is then loaded to the Arduino that will turn the eight LEDs into a "Knight rider" display.

The following video shows what you will achieve:

Can't see the video? View on YouTube →

Prerequisites

You will need to have completed tutorial 3 and its prerequisites before attempting this tutorial.

Components

Besides a breadboard, Arduino Uno and USB cable, you will need:

Qty Part Designator Notes Type
8 470 ohm resistor (yellow - violet - brown) R1 1/4W, 5% or better Resistors
8 5mm red LED D1 Other coloured and sized LEDs could also be used, e.g. 3mm green LED Semiconductors
9 Wire links Wire links that will reach from the breadboard to the Arduino Wire

Circuit Diagram

The circuit diagram is shown below. It is simply eight LEDs interfaced to the Arduino pins 2 to 9.

Arduino Knight Rider circuit diagram

Building the Circuit

Place the LEDs next to each other in the breadboard so that the anode (longer lead) is at the left and the cathode is on the right.

Insert the LEDs into the breadboard

Insert the 470 ohm resistors connecting one lead to the LED's cathode and the other to the top rail.

Insert the resistors into the breadboard

Join the anodes of the LEDs to pins 2 to 9 of the Arduino from left to right using single-core wire. Join the top rail to one of the GND pins of the Arduino.

The finished Arduino Knight Rider breadboard circuit

When finished building the circuit, connect the Arduino to the PC via the USB cable.

Loading the Program

The Knight Rider program listing is shown below. Copy it and pasted it into the Arduino IDE.

/*
  Knight Rider
  
  Knight rider display on 8 LEDs
*/

void setup() {
    // set up pins 2 to 9 as outputs
    for (int i = 2; i < 10; i++) {
        pinMode(i, OUTPUT);
    }
}

// function to switch all LEDs off
void allLEDsOff(void)
{
    for (int i = 2; i < 10; i++) {
        digitalWrite(i, LOW);
    }
}

void loop() {
    // move on LED to the right
    for (int i = 2; i < 9; i++) {
        allLEDsOff();
        digitalWrite(i, HIGH);
        delay(200);
    }
    // move on LED to the left
     for (int i = 9; i > 2; i--) {
        allLEDsOff();
        digitalWrite(i, HIGH);
        delay(200);
    }
}

Load the program to the Arduino and if the circuit was built correctly, your knight rider circuit will start operating.

← Go back to Tutorial 3: Starting with ArduinoGo to Tutorial 5: 555 LED Flasher →

Enjoy this article, project or tutorial?

You can help by donating to Starting Electronics to fund our web hosting costs and more great content.

Use the button below to donate by PayPal or Credit Card.

Comment on this article, ask questions or submit corrections below.

Use the subscribe button above to receive email notification when new comments are submitted on this article.