Mega: 8-LED Chase is a runnable microcontroller simulation example on SensBench — open the wired circuit, inspect the code, and compile real firmware in your browser, no install needed. Last updated Aug 14, 2026.

FREE SIMULATOR EXAMPLE · BEGINNER

Mega: 8-LED Chase

This example runs a Knight-Rider style chase across eight LEDs on pins 2 through 9 of the Arduino Mega 2560: the dot sweeps forward, bounces off the end, and sweeps back, stepping every 80 ms. It exists to show off what the Mega has in abundance — spare digital I/O.

Board Arduino Mega 2560 (ATmega2560, 16 MHz)
Difficulty beginner
Category basics
Libraries None — just the Arduino core
Runs in Sensbench browser simulator — free, no account
Arduino Mega 2560 chasing eight LEDs across pins 2 to 9 in the Sensbench simulator

The exact circuit you get when you open this example — already wired, ready to run.

The sketch

// Arduino Mega — 8-LED Knight Rider Chase

const int LEDS[] = {2, 3, 4, 5, 6, 7, 8, 9};
const int N = 8;

void setup() {
  for (int i = 0; i < N; i++) {
    pinMode(LEDS[i], OUTPUT);
  }
  Serial.begin(9600);
  Serial.println("Mega LED Chase ready!");
}

void loop() {
  // Chase forward
  for (int i = 0; i < N; i++) {
    digitalWrite(LEDS[i], HIGH);
    delay(80);
    digitalWrite(LEDS[i], LOW);
  }
  // Chase backward
  for (int i = N - 2; i > 0; i--) {
    digitalWrite(LEDS[i], HIGH);
    delay(80);
    digitalWrite(LEDS[i], LOW);
  }
}

How it works

The eight pins live in a const int LEDS[] array, and setup() walks it with a for-loop calling pinMode(LEDS[i], OUTPUT) — the array-plus-loop pattern is what makes this sketch scale, and it is why the same structure stretches to 16 LEDs elsewhere in this library without new logic.

The chase itself is two loops. The first sweeps pins 2 through 9: turn one LED on, delay(80), turn it off — so exactly one LED is ever lit and the lit position moves 80 ms per step. The second loop runs backward from N - 2 down to index 1, deliberately skipping both endpoints so the ends of the strip do not double-blink when the direction reverses.

Before you build it for real

The simulator happily drives eight bare LEDs off GPIO pins; a real Mega cannot. Each LED needs its own 220 Ω series resistor — the ATmega2560 is rated 20 mA per pin and 200 mA for the whole chip, and a bare LED will pull whatever the pin gives it. This pattern lights only one LED at a time, which keeps the total budget trivial, but the moment you modify it to leave LEDs on — a trailing comet tail is the classic next step — the cumulative current becomes the thing to calculate, at 5 V logic levels.

Get the parts

Related examples

FAQ

Why does the backward loop start at N – 2 instead of N – 1?

To avoid lighting the last LED twice. The forward sweep already lit LED 8; starting the return at index 6 (pin 8… one in from the end) makes the dot bounce cleanly off the ends instead of pausing for a double-length step.

Can I make the chase faster or slower?

Change the single delay(80) value — both loops share it, so the whole animation speeds up or slows down symmetrically. Below about 30 ms the blur merges into a glow; above 200 ms it stops reading as motion.