ESP32Cube Logo
Sign In
ESP32 LED Chaser (Running Light) Experiment

ESP32 LED Chaser (Running Light) Experiment

esp32cube
Oct 26, 2026
Tutorial
1162 views

This article demonstrates how to build a classic LED chaser (running light) circuit with ESP32, including several implementation methods and code optimizations.

ESP32ArduinoLEDGPIO

Introduction

This tutorial shows how to create a classic LED chaser (running light) using ESP32 and Arduino. You will learn the hardware wiring, program structure, and several ways to implement the effect, including a more concise version using arrays and loops.

Hardware Design

Bill of Materials (BOM):

ItemQuantity
LED (through-hole)5
1kΩ resistor5
Jumper wiresSeveral
Breadboard1

Each LED anode connects to a separate ESP32 GPIO pin via a 1kΩ resistor; all cathodes connect to GND.

Software Design

1. Basic LED Chaser

The simplest way is to use an array to store the GPIO pins and loop through them to turn each LED on and off in sequence.

// Define GPIO pins for LEDs
const int pin_list[] = {13, 12, 14, 27, 26};
const int size = sizeof(pin_list) / sizeof(pin_list[0]);

void setup() {
  for (int i = 0; i < size; i++) {
    pinMode(pin_list[i], OUTPUT);
    digitalWrite(pin_list[i], LOW); // Ensure all LEDs are off at start
  }
}

void loop() {
  for (int i = 0; i < size; i++) {
    digitalWrite(pin_list[i], HIGH); // Turn on current LED
    delay(80);
    digitalWrite(pin_list[i], LOW);  // Turn off current LED
  }
}

Alternative (more concise):

You can use a single variable to track the current LED and update only two pins per step:

const int pin_list[] = {13, 12, 14, 27, 26};
const int size = sizeof(pin_list) / sizeof(pin_list[0]);
int current = 0;

void setup() {
  for (int i = 0; i < size; i++) {
    pinMode(pin_list[i], OUTPUT);
    digitalWrite(pin_list[i], LOW);
  }
}

void loop() {
  digitalWrite(pin_list[current], HIGH);
  digitalWrite(pin_list[(current + size - 1) % size], LOW);
  delay(120);
  current = (current + 1) % size;
}

2. Bidirectional (Ping-Pong) Chaser

To make the LEDs light up back and forth, use a direction variable:

const int pin_list[] = {13, 12, 14, 27, 26};
const int size = sizeof(pin_list) / sizeof(pin_list[0]);
int current = 0;
int dir = 1;

void setup() {
  for (int i = 0; i < size; i++) {
    pinMode(pin_list[i], OUTPUT);
    digitalWrite(pin_list[i], LOW);
  }
}

void loop() {
  digitalWrite(pin_list[current], HIGH);
  delay(100);
  digitalWrite(pin_list[current], LOW);
  current += dir;
  if (current == size - 1 || current == 0) dir = -dir;
}

3. Moving Light (Single LED Moves)

To create a moving light effect where only one LED is on at a time:

const int pin_list[] = {13, 12, 14, 27, 26};
const int size = sizeof(pin_list) / sizeof(pin_list[0]);

void setup() {
  for (int i = 0; i < size; i++) {
    pinMode(pin_list[i], OUTPUT);
    digitalWrite(pin_list[i], LOW);
  }
}

void loop() {
  for (int i = 0; i < size; i++) {
    for (int j = 0; j < size; j++) {
      digitalWrite(pin_list[j], j == i ? HIGH : LOW);
    }
    delay(180);
  }
}

4. Symmetrical (Center-Out) Chaser

This effect lights up LEDs from the center outwards and then back inwards, creating a symmetrical running light:

const int pin_list[] = {13, 12, 14, 27, 26};
const int size = sizeof(pin_list) / sizeof(pin_list[0]);

void setup() {
  for (int i = 0; i < size; i++) {
    pinMode(pin_list[i], OUTPUT);
    digitalWrite(pin_list[i], LOW);
  }
}

void loop() {
  int mid = size / 2;
  // Center out
  for (int offset = 0; offset <= mid; offset++) {
    for (int i = 0; i < size; i++) digitalWrite(pin_list[i], LOW);
    digitalWrite(pin_list[mid - offset], HIGH);
    digitalWrite(pin_list[mid + offset], HIGH);
    delay(180);
  }
  // Out to center
  for (int offset = mid - 1; offset >= 0; offset--) {
    for (int i = 0; i < size; i++) digitalWrite(pin_list[i], LOW);
    digitalWrite(pin_list[mid - offset], HIGH);
    digitalWrite(pin_list[mid + offset], HIGH);
    delay(180);
  }
}

5. Random LED Chaser

This effect randomly lights up one LED at a time, creating a dynamic and unpredictable running light:

#include <Arduino.h>
const int pin_list[] = {13, 12, 14, 27, 26};
const int size = sizeof(pin_list) / sizeof(pin_list[0]);
int last = -1;

void setup() {
  for (int i = 0; i < size; i++) {
    pinMode(pin_list[i], OUTPUT);
    digitalWrite(pin_list[i], LOW);
  }
  randomSeed(analogRead(0));
}

void loop() {
  int idx = random(size);
  // Avoid lighting the same LED twice in a row
  while (idx == last) idx = random(size);
  for (int i = 0; i < size; i++) digitalWrite(pin_list[i], LOW);
  digitalWrite(pin_list[idx], HIGH);
  last = idx;
  delay(200);
}

References

  • ESP32 LED Basics
  • How to Use ESP32 and WS2812B to Create Stunning LED Displays

Share this article

Comments

0

Please sign in to post a comment.

No comments yet.

Related Articles

ESP32 4-Wire PT100/PT1000 High-Accuracy Temperature Measurement with MAX31865

Tutorial·408 views

ESP32 DFPlayer Mini (MP3-TF-16P): Wiring, Arduino Code, Playback and Troubleshooting

Tutorial·4767 views

ESP32 Buttons in Arduino: Wiring, Debouncing, and Interrupts

Tutorial·556 views

ESP32-C3 vs ESP32-S3 vs ESP32-C6: How to Choose for Real Firmware Projects

Tutorial·3617 views

ESP32 Arduino Button Input: Practical Guide and Debouncing

Tutorial·1718 views
View more in this category→
Copyright © 2026 ESP32Cube. All rights reserved.•1.0.1•Terms·Privacy
Source codeTwitterDiscord