// Define the GPIO pins for your four LEDs
// You can change these to any suitable digital GPIO pin on your ESP32 board.
// Avoid pins used for specific functions like TX/RX (GPIO1, GPIO3) or strapping pins during boot.
const int ledPin1 = 16; // Example: Connecting the first LED to ESP32's GPIO 16
const int ledPin2 = 17; // Example: Connecting the second LED to ESP32's GPIO 17
const int ledPin3 = 4; // Example: Connecting the third LED to ESP32's GPIO 4
const int ledPin4 = 2; // Example: Connecting the fourth LED to ESP32's GPIO 2
// Define the blink interval (in milliseconds)
// This controls how long each state lasts before switching.
// 500 milliseconds = 0.5 seconds
const long blinkInterval = 500;
// You can create an array of pins for easier management, especially with more LEDs
const int ledPins[] = {ledPin1, ledPin2, ledPin3, ledPin4};
const int numberOfLeds = sizeof(ledPins) / sizeof(ledPins[0]);
void setup() {
// Initialize the serial communication for debugging (optional but recommended)
Serial.begin(115200);
Serial.println("ESP32 Four LED Blink Program Started!");
// Set all LED pins as OUTPUTs using a loop (more efficient for many LEDs)
for (int i = 0; i < numberOfLeds; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW); // Ensure all LEDs are off initially
}
}
void loop() {
// --- Chasing Light Pattern (one LED on at a time) ---
for (int i = 0; i < numberOfLeds; i++) {
Serial.print("Pattern: LED ");
Serial.print(i + 1);
Serial.println(" ON");
// Turn off all LEDs first
for (int j = 0; j < numberOfLeds; j++) {
digitalWrite(ledPins[j], LOW);
}
// Turn on the current LED in the sequence
digitalWrite(ledPins[i], HIGH);
delay(blinkInterval);
}
// --- All ON, then All OFF Pattern ---
Serial.println("Pattern: All LEDs ON");
for (int i = 0; i < numberOfLeds; i++) {
digitalWrite(ledPins[i], HIGH); // Turn all LEDs ON
}
delay(blinkInterval * 2); // Stay on for a bit longer
Serial.println("Pattern: All LEDs OFF");
for (int i = 0; i < numberOfLeds; i++) {
digitalWrite(ledPins[i], LOW); // Turn all LEDs OFF
}
delay(blinkInterval);
// The loop() function will repeat these patterns indefinitely.
}