LED Chaser using ESP32 Board

Last Updated on June 21, 2024

Even though ESP32 Board capable of doing complex work, it can be used for simple projects and task for efficient and more advanced options. Here we are going to make a experiment with this embedded board and few LEDs. If you are new to ESP32 board, Read Here.., to get started with it. A LED chaser using ESP32 board is the simple and fun to make project.




Here we use 10 5mm Red color LEDs and connecting it to the ESP32 board through 220Ω Resistors. By using GPIO pins and code we can make different patterns LED chasing effects.

Connection Diagram

Schematic

Components Required

  • ESP32 development board
  • Resistors 220Ω = 10
  • 5mm LED = 10 (any color)
  • Connecting Wires
  • Breadboard
  • Micro USB Cable
  • Computer for to upload Code

Working Video

Construction & Working

Connect LEDs Anode to the ESP32 board GPIO pins in the following order {15, 2, 4, 16, 17, 5, 18, 19, 21, 3}, considering as LED1 to LED10 pins.

We connecting it like this because of hardware pins and GPIO pin structure, depends on the board and version these GPIO pins in hardware may change., So refer data sheet of ESP32 development board and then connect LEDs, remember to mention it correctly in the code.

After completion of hardware connection upload the following code to observe LED chasing effect.

Simple LED Running Code for ESP32

// Define the number of LEDs
#define NUM_LEDS 10

// Array of LED pins
int ledPins[NUM_LEDS] = {15, 2, 4, 16, 17, 5, 18, 19, 21, 3};

// Time delay between each LED change (in milliseconds)
int delayTime = 100;

void setup() {
  // Initialize each pin as an output
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], LOW); // Ensure all LEDs are off initially
  }
}

void loop() {
  // Turn on each LED in sequence
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(ledPins[i], HIGH); // Turn on the LED
    delay(delayTime);               // Wait
    digitalWrite(ledPins[i], LOW);  // Turn off the LED
  }

  // Turn on each LED in reverse sequence
  for (int i = NUM_LEDS - 1; i >= 0; i--) {
    digitalWrite(ledPins[i], HIGH); // Turn on the LED
    delay(delayTime);               // Wait
    digitalWrite(ledPins[i], LOW);  // Turn off the LED
  }
}

Different Pattern LED Running Code for ESP32

// Define the number of LEDs
#define NUM_LEDS 10

// Array of LED pins
int ledPins[NUM_LEDS] = {15, 2, 4, 16, 17, 5, 18, 19, 21, 3};

// Time delay between each LED change (in milliseconds)
int delayTime = 80;

// Number of iterations before switching patterns
int iterationsPerPattern = 5;

void setup() {
  // Initialize each pin as an output
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], LOW); // Ensure all LEDs are off initially
  }
}

void loop() {
  // Single LED chase
  for (int iter = 0; iter < iterationsPerPattern; iter++) {
    for (int i = 0; i < NUM_LEDS; i++) {
      digitalWrite(ledPins[i], HIGH);
      delay(delayTime);
      digitalWrite(ledPins[i], LOW);
    }
  }

  // Two LEDs chase
  for (int iter = 0; iter < iterationsPerPattern; iter++) {
    for (int i = 0; i < NUM_LEDS - 1; i++) {
      digitalWrite(ledPins[i], HIGH);
      digitalWrite(ledPins[i + 1], HIGH);
      delay(delayTime);
      digitalWrite(ledPins[i], LOW);
      digitalWrite(ledPins[i + 1], LOW);
    }
  }

  // Center outwards chase
  for (int iter = 0; iter < iterationsPerPattern; iter++) {
    for (int i = 0; i < NUM_LEDS / 2; i++) {
      digitalWrite(ledPins[NUM_LEDS / 2 - 1 - i], HIGH);
      digitalWrite(ledPins[NUM_LEDS / 2 + i], HIGH);
      delay(delayTime);
      digitalWrite(ledPins[NUM_LEDS / 2 - 1 - i], LOW);
      digitalWrite(ledPins[NUM_LEDS / 2 + i], LOW);
    }
  }

  // Outwards to center chase
  for (int iter = 0; iter < iterationsPerPattern; iter++) {
    for (int i = 0; i < NUM_LEDS / 2; i++) {
      digitalWrite(ledPins[i], HIGH);
      digitalWrite(ledPins[NUM_LEDS - 1 - i], HIGH);
      delay(delayTime);
      digitalWrite(ledPins[i], LOW);
      digitalWrite(ledPins[NUM_LEDS - 1 - i], LOW);
    }
  }

  // Blink all LEDs
  for (int iter = 0; iter < iterationsPerPattern; iter++) {
    for (int i = 0; i < NUM_LEDS; i++) {
      digitalWrite(ledPins[i], HIGH);
    }
    delay(delayTime);
    for (int i = 0; i < NUM_LEDS; i++) {
      digitalWrite(ledPins[i], LOW);
    }
    delay(delayTime);
  }
}




Leave a Reply

Your email address will not be published. Required fields are marked *