Tutorial 4: Arduino Knight Rider

Created on: 31 July 2012
Updated on: 14 January 2023

Build an Arduino Knight Rider LED display circuit on breadboard controlled by an Arduino Uno for beginners in electronics. 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 (or Arduino sketch) is then loaded to the Arduino that turns the eight LEDs into a "Knight rider" display, also known as an LED chaser.

The following video shows what you will achieve after building the LED Knight Rider Arduino project:

Prerequisites

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

Arduino Knight Rider 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 to D8 Other colored 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

Arduino Knight Rider Circuit Diagram

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

Arduino Knight Rider circuit diagram
Arduino Knight Rider / LED Chaser Circuit

Building the Arduino Knight Rider Circuit

To start building the Arduino Knight Rider 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
Step 1: Insert the LEDs into the Breadboard

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

Insert the resistors into the breadboard
Step 2: 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 of the breadboard to one of the GND pins of the Arduino.

The finished Arduino Knight Rider breadboard circuit
Step 3: Complete the Wiring of the Circuit

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

Loading the Arduino Knight Rider Sketch

The Knight Rider sketch listing is shown below. Either type it out, or copy it and paste it into a new window of the Arduino IDE. Finally save the sketch in 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 sketch to the Arduino and if the circuit was built correctly, your knight rider circuit will start operating.

Simplifying the Arduino Knight Rider Circuit

In most circuits each LED requires it's own current limiting resistor. However, in this Arduino Knight Rider circuit, only one LED is switched on at a time. This means that all of the LED cathodes can be joined together and then one 470Ω resistor connected from the LED cathodes to GND, instead of using eight individual resistors.

If more than one LED is to be switched on at a time in this Knight Rider circuit, then each LED would need an individual current limiting resistor as the current circuit shows. For example, changing the Arduino sketch to display two running LEDs at a time would not work if only one resistor were to be used.