0
0
AutocadProgramBeginner · 2 min read

Arduino Program for LED Chaser with Example Code

Use a for loop to turn on LEDs one by one with digitalWrite(pin, HIGH) and then off with digitalWrite(pin, LOW), adding delay() between steps for the LED chaser effect.
📋

Examples

Input5 LEDs connected to pins 2 to 6
OutputLEDs light up one by one from pin 2 to 6 repeatedly with 200ms delay
Input3 LEDs connected to pins 8 to 10
OutputLEDs light up in sequence from pin 8 to 10 repeatedly with 300ms delay
Input1 LED connected to pin 13
OutputLED on pin 13 blinks on and off repeatedly with 500ms delay
🧠

How to Think About It

To create an LED chaser, think of turning on one LED at a time in a sequence, then turning it off before moving to the next LED. Use a loop to go through each LED pin, turn it on, wait a bit, then turn it off, and repeat this cycle forever.
📐

Algorithm

1
Set all LED pins as outputs
2
Start an infinite loop
3
For each LED pin in order:
4
Turn the LED on
5
Wait for a short delay
6
Turn the LED off
7
Repeat from the first LED
💻

Code

arduino
const int ledPins[] = {2, 3, 4, 5, 6};
const int numLeds = 5;

void setup() {
  for (int i = 0; i < numLeds; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
  Serial.begin(9600);
}

void loop() {
  for (int i = 0; i < numLeds; i++) {
    digitalWrite(ledPins[i], HIGH);
    Serial.print("LED ");
    Serial.print(i);
    Serial.println(" ON");
    delay(200);
    digitalWrite(ledPins[i], LOW);
  }
}
Output
LED 0 ON LED 1 ON LED 2 ON LED 3 ON LED 4 ON (repeats)
🔍

Dry Run

Let's trace the LED chaser with 5 LEDs connected to pins 2 to 6.

1

Setup pins

Pins 2,3,4,5,6 set as OUTPUT

2

Loop start

Start looping over LEDs from index 0 to 4

3

Turn on LED 0

digitalWrite(pin 2, HIGH), print 'LED 0 ON', delay 200ms

4

Turn off LED 0

digitalWrite(pin 2, LOW)

5

Repeat for LEDs 1 to 4

Same steps for pins 3,4,5,6 with indexes 1 to 4

LED IndexPinStateSerial Output
02ONLED 0 ON
02OFF
13ONLED 1 ON
13OFF
24ONLED 2 ON
24OFF
35ONLED 3 ON
35OFF
46ONLED 4 ON
46OFF
💡

Why This Works

Step 1: Set pins as output

We use pinMode to tell Arduino these pins will control LEDs.

Step 2: Turn LEDs on and off in sequence

The for loop goes through each LED pin, turning it on with digitalWrite(pin, HIGH), then off with digitalWrite(pin, LOW).

Step 3: Add delay for visible effect

The delay(200) pauses the program so the LED stays lit long enough to see the chasing effect.

🔄

Alternative Approaches

Using an array and shifting bits
arduino
const int ledPins[] = {2, 3, 4, 5, 6};
const int numLeds = 5;

void setup() {
  for (int i = 0; i < numLeds; i++) pinMode(ledPins[i], OUTPUT);
}

void loop() {
  for (int i = 0; i < numLeds; i++) {
    for (int j = 0; j < numLeds; j++) {
      digitalWrite(ledPins[j], j == i ? HIGH : LOW);
    }
    delay(200);
  }
}
This method turns off all LEDs except the current one in each loop, which can be clearer but uses nested loops.
Using a single LED blinking
arduino
const int ledPin = 13;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
  delay(500);
}
Simplest approach for one LED blinking, no chasing effect.

Complexity: O(n) time, O(n) space

Time Complexity

The program loops through each LED once per cycle, so time grows linearly with the number of LEDs.

Space Complexity

Uses an array to store LED pins, so space grows linearly with the number of LEDs.

Which Approach is Fastest?

Both main and alternative methods run in O(n) time; the main method is simpler and easier to read.

ApproachTimeSpaceBest For
Simple for loopO(n)O(n)Easy to understand and implement
Nested loop with conditionalO(n^2)O(n)Clear LED on/off control but less efficient
Single LED blinkO(1)O(1)Only one LED, no chasing effect
💡
Use arrays to manage multiple LED pins easily and loop through them for the chaser effect.
⚠️
Forgetting to set LED pins as OUTPUT with pinMode causes LEDs not to light up.