Arduino Knight Rider LED Chaser with Speed Control - Complete Wokwi Tutorial

Project Overview

This Arduino project creates a Knight Rider-style LED chaser effect with variable speed control using a potentiometer. Eight LEDs light up sequentially, bouncing back and forth like the iconic KITT scanner from the Knight Rider TV series.

Components Required

Diagram.json:
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-breadboard", "id": "bb1", "top": -156.6, "left": -198.8, "attrs": {} },
    { "type": "wokwi-arduino-mega", "id": "mega", "top": 58.2, "left": -541.2, "attrs": {} },
    { "type": "wokwi-led", "id": "led1", "top": -42, "left": -169, "attrs": { "color": "red" } },
    { "type": "wokwi-led", "id": "led2", "top": -42, "left": -140.2, "attrs": { "color": "red" } },
    { "type": "wokwi-led", "id": "led3", "top": -42, "left": -111.4, "attrs": { "color": "red" } },
    { "type": "wokwi-led", "id": "led4", "top": -42, "left": -82.6, "attrs": { "color": "red" } },
    { "type": "wokwi-led", "id": "led5", "top": -42, "left": -53.8, "attrs": { "color": "red" } },
    { "type": "wokwi-led", "id": "led6", "top": -42, "left": -25, "attrs": { "color": "red" } },
    { "type": "wokwi-led", "id": "led7", "top": -42, "left": 3.8, "attrs": { "color": "red" } },
    { "type": "wokwi-led", "id": "led8", "top": -42, "left": 32.6, "attrs": { "color": "red" } },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": -72,
      "left": -173.35,
      "rotate": 90,
      "attrs": { "value": "220" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": -72,
      "left": -144.55,
      "rotate": 90,
      "attrs": { "value": "220" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r3",
      "top": -72,
      "left": -115.75,
      "rotate": 90,
      "attrs": { "value": "220" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r4",
      "top": -72,
      "left": -86.95,
      "rotate": 90,
      "attrs": { "value": "220" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r5",
      "top": -72,
      "left": -58.15,
      "rotate": 90,
      "attrs": { "value": "220" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r6",
      "top": -72,
      "left": -29.35,
      "rotate": 90,
      "attrs": { "value": "220" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r7",
      "top": -72,
      "left": -0.55,
      "rotate": 90,
      "attrs": { "value": "220" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r8",
      "top": -72,
      "left": 28.25,
      "rotate": 90,
      "attrs": { "value": "220" }
    },
    {
      "type": "wokwi-slide-potentiometer",
      "id": "pot1",
      "top": 283.4,
      "left": -308.2,
      "attrs": { "travelLength": "30" }
    }
  ],
  "connections": [
    [ "r1:1", "bb1:tp.2", "green", [ "h0" ] ],
    [ "r2:1", "bb1:tp.5", "green", [ "h0" ] ],
    [ "r3:1", "bb1:tp.7", "green", [ "h0" ] ],
    [ "r4:1", "bb1:tp.10", "green", [ "h0" ] ],
    [ "r5:1", "bb1:tp.12", "green", [ "h0" ] ],
    [ "r6:1", "bb1:tp.15", "green", [ "h0" ] ],
    [ "r7:1", "bb1:tp.17", "green", [ "h0" ] ],
    [ "r8:1", "bb1:tp.20", "green", [ "h0" ] ],
    [ "r1:1", "bb1:tn.2", "green", [ "h0" ] ],
    [ "led1:C", "mega:22", "green", [ "v0" ] ],
    [ "led2:C", "mega:24", "green", [ "v0" ] ],
    [ "led3:C", "mega:26", "green", [ "v0" ] ],
    [ "led4:C", "mega:28", "green", [ "v0" ] ],
    [ "led5:C", "mega:30", "green", [ "v0" ] ],
    [ "led6:C", "mega:32", "green", [ "v0" ] ],
    [ "led7:C", "mega:34", "green", [ "v0" ] ],
    [ "led8:C", "mega:36", "green", [ "v0" ] ],
    [ "mega:5V", "bb1:tp.1", "red", [ "v35.7", "h-180.1", "v-422.4", "h326.4", "v-0.3" ] ],
    [ "led1:A", "bb1:4b.j", "", [ "$bb" ] ],
    [ "led1:C", "bb1:3b.j", "", [ "$bb" ] ],
    [ "led2:A", "bb1:7b.j", "", [ "$bb" ] ],
    [ "led2:C", "bb1:6b.j", "", [ "$bb" ] ],
    [ "led3:A", "bb1:10b.j", "", [ "$bb" ] ],
    [ "led3:C", "bb1:9b.j", "", [ "$bb" ] ],
    [ "led4:A", "bb1:13b.j", "", [ "$bb" ] ],
    [ "led4:C", "bb1:12b.j", "", [ "$bb" ] ],
    [ "led5:A", "bb1:16b.j", "", [ "$bb" ] ],
    [ "led5:C", "bb1:15b.j", "", [ "$bb" ] ],
    [ "led6:A", "bb1:19b.j", "", [ "$bb" ] ],
    [ "led6:C", "bb1:18b.j", "", [ "$bb" ] ],
    [ "led7:A", "bb1:22b.j", "", [ "$bb" ] ],
    [ "led7:C", "bb1:21b.j", "", [ "$bb" ] ],
    [ "led8:A", "bb1:25b.j", "", [ "$bb" ] ],
    [ "led8:C", "bb1:24b.j", "", [ "$bb" ] ],
    [ "r1:1", "bb1:4t.b", "", [ "$bb" ] ],
    [ "r1:2", "bb1:4b.f", "", [ "$bb" ] ],
    [ "r2:1", "bb1:7t.b", "", [ "$bb" ] ],
    [ "r2:2", "bb1:7b.f", "", [ "$bb" ] ],
    [ "r3:1", "bb1:10t.b", "", [ "$bb" ] ],
    [ "r3:2", "bb1:10b.f", "", [ "$bb" ] ],
    [ "r4:1", "bb1:13t.b", "", [ "$bb" ] ],
    [ "r4:2", "bb1:13b.f", "", [ "$bb" ] ],
    [ "r5:1", "bb1:16t.b", "", [ "$bb" ] ],
    [ "r5:2", "bb1:16b.f", "", [ "$bb" ] ],
    [ "r6:1", "bb1:19t.b", "", [ "$bb" ] ],
    [ "r6:2", "bb1:19b.f", "", [ "$bb" ] ],
    [ "r7:1", "bb1:22t.b", "", [ "$bb" ] ],
    [ "r7:2", "bb1:22b.f", "", [ "$bb" ] ],
    [ "r8:1", "bb1:25t.b", "", [ "$bb" ] ],
    [ "r8:2", "bb1:25b.f", "", [ "$bb" ] ],
    [ "pot1:VCC", "mega:5V", "red", [ "h0" ] ],
    [ "pot1:SIG", "mega:A0", "green", [ "h0" ] ],
    [ "pot1:GND", "mega:GND.2", "black", [ "v-48", "h-246.55" ] ]
  ],
  "dependencies": {}
}
  1. Arduino MEGA 2560 (or Arduino Uno)
  2. 8 x LEDs (Red or any color)
  3. 8 x 220Ω Resistors (for current limiting)
  4. 1 x Potentiometer (10kΩ) (for speed control)
  5. Breadboard
  6. Jumper wires

Circuit Connection Guide

LED Connections:

  • LED 1 → Pin 22 (with 220Ω resistor to GND)
  • LED 2 → Pin 24 (with 220Ω resistor to GND)
  • LED 3 → Pin 26 (with 220Ω resistor to GND)
  • LED 4 → Pin 28 (with 220Ω resistor to GND)
  • LED 5 → Pin 30 (with 220Ω resistor to GND)
  • LED 6 → Pin 32 (with 220Ω resistor to GND)
  • LED 7 → Pin 34 (with 220Ω resistor to GND)
  • LED 8 → Pin 36 (with 220Ω resistor to GND)

Potentiometer Connection:

  • Left Pin → 5V (Arduino)
  • Middle Pin (Wiper) → A0 (Analog Input)
  • Right Pin → GND

 

Code Operation Breakdown

Step-by-Step Execution:

  1. LED Activation: Current LED (at position pos) lights up
  2. Position Update: Position increments by +1 (forward) or -1 (backward)
  3. Boundary Detection:
    • When reaching LED 8 (position 7) → direction reverses
    • When reaching LED 1 (position 0) → direction reverses
  4. Next LED Preparation: The next LED turns OFF to create chaser effect
  5. Speed Control: Potentiometer reading (0-1023) determines delay time
    • Minimum rotation → Fast animation (~0ms delay)
    • Maximum rotation → Slow animation (~1023ms delay)

Wokwi Simulator Setup

Creating the Project in Wokwi:

  1. Go to Wokwi.com
  2. Click "New Project" → Select "Arduino Mega"
  3. Add components:
    • 8x LED (red)
    • 8x Resistor (220Ω)
    • 1x Potentiometer
  4. Connect components as shown in the circuit diagram
  5. Paste the code into the code editor
  6. Click "Start Simulation"

Expected Behavior:

  • LEDs light up sequentially from left to right
  • Animation reverses at both ends (bouncing effect)
  • Turn potentiometer to adjust animation speed
  • Smooth Knight Rider scanning pattern
Code:

arduino
// ============================================
// Knight Rider LED Chaser with Speed Control
// ============================================

// Variable Declaration
int led[] = {22, 24, 26, 28, 30, 32, 34, 36};  // Array of LED pin numbers
int number_of_leds = sizeof(led)/sizeof(int);   // Calculate total number of LEDs (8)
int position = 0;                               // Current LED position in array
int direction = 1;                              // Movement direction: 1 = forward, -1 = backward
int potentiometer = A0;                         // Potentiometer connected to analog pin A0
int potentiometer_value = 0;                    // Variable to store potentiometer reading
int speed = 0;                                  // Variable to control animation speed

void setup() {
  // Initialize all LED pins and test them
  for(int i = 0; i < number_of_leds; i++) {
    pinMode(led[i], OUTPUT);      // Set each LED pin as OUTPUT
    digitalWrite(led[i], HIGH);   // Turn ON all LEDs for functionality test
  }
  delay(500);                     // Keep all LEDs ON for 500ms to verify connections
  
  // Turn OFF all LEDs after test
  for(int i = 0; i < number_of_leds; i++) {
    digitalWrite(led[i], LOW);
  }
}

// Main Program Loop
void loop() {
  
  // Turn ON the LED at current position
  digitalWrite(led[position], HIGH);
  
  // Update position based on current direction
  position += direction;  // Move forward (+1) or backward (-1)
  
  // Check if we've reached the boundaries and reverse direction
  if(position == number_of_leds - 1 || position == 0) {
    direction *= -1;  // Reverse direction (1 becomes -1, or -1 becomes 1)
  }
  
  // Turn OFF the LED at the new position (creates chaser effect)
  digitalWrite(led[position], LOW);
  
  // Read potentiometer value (0 to 1023)
  potentiometer_value = analogRead(potentiometer);
  
  // Use potentiometer value as delay time to control speed
  delay(potentiometer_value);  // Higher value = slower animation
}

Comments