LED Blinking
Interfacing LED Bar with ESP32
Interfacing LED Bar with ESP32 demonstrates how an LED bar can be connected to an ESP32 board and controlled using digital output pins.
An LED (Light Emitting Diode) is a semiconductor device that emits light when it is forward biased.
An LED has two terminals:
- Anode (+) – positive terminal
- Cathode (−) – negative terminal
A current-limiting resistor is used while interfacing LEDs with the ESP32 to limit the current flowing through the LED.
Introduction to LED
LED stands for Light Emitting Diode.
An LED is a p-n junction diode that emits light when it is forward biased.
The typical characteristics considered for the LED in this practical are:
| Parameter | Value |
|---|---|
| Required voltage | Approximately 1.7 V |
| Required current | 5–20 mA |
A current-limiting resistor is connected between the LED and the ESP32 to limit the current.
LED Bar
An LED bar consists of multiple LEDs arranged together in a single package.
In this practical, the LED bar contains 4 LEDs.
The LEDs are controlled by applying suitable digital signals to their respective terminals.
The LED bar can have two commonly used configurations:
- Common Anode
- Common Cathode
Common Anode
In a Common Anode LED bar, the anode terminals of all the LEDs are connected together and connected to the positive supply VCC.
The individual LEDs are controlled through their cathode terminals.
Common Anode → VCC
By controlling the cathode terminals, the individual LEDs can be switched ON or OFF.
Common Cathode
In a Common Cathode LED bar, the cathode terminals of all the LEDs are connected together and connected to Ground (0 V).
The individual LEDs are controlled through their anode terminals.
Common Cathode → Ground (0 V)
By controlling the anode terminals, the individual LEDs can be switched ON or OFF.
Connections
The LED bar is interfaced with the ESP32 using four digital GPIO pins.
| Device | Pin | ESP32 | Function |
|---|---|---|---|
| LED Bar | LED 1 | GPIO 12 | LED control |
| LED Bar | LED 2 | GPIO 13 | LED control |
| LED Bar | LED 3 | GPIO 14 | LED control |
| LED Bar | LED 4 | GPIO 15 | LED control |
| LED Bar | VCC / Common Anode | 3.3V / Supply | Power |
| LED Bar | GND / Common Cathode | GND | Ground |
Current Limiting Resistor
A current-limiting resistor is used with the LED to prevent excessive current from flowing through it.
The resistor is connected between the LED and the ESP32 output.
The resistor helps protect the LED and the ESP32 GPIO from excessive current.
Safety Precautions
- First, make all the connections according to the circuit diagram.
- Check the LED bar configuration before connecting the supply.
- Use a suitable current-limiting resistor with the LEDs.
- Check the polarity of the LED bar.
- Ensure that the connections are correct before applying power.
- Then connect the ESP32 board to the PC or laptop.
- Do not connect the LED directly to an ESP32 GPIO without suitable current limiting.
Interfacing Diagram Using Breadboard
The LED bar is connected to the ESP32 through the four GPIO pins used in the program.
The LED control pins are:
| LED | ESP32 GPIO |
|---|---|
| LED 1 | GPIO 12 |
| LED 2 | GPIO 13 |
| LED 3 | GPIO 14 |
| LED 4 | GPIO 15 |
The LED bar's common terminal is connected according to its configuration.
For a Common Anode LED bar, the common anode is connected to the positive supply.
For a Common Cathode LED bar, the common cathode is connected to ground.
| Configuration | Common Terminal | LED Control |
|---|---|---|
| Common Anode | VCC | Cathode terminals |
| Common Cathode | GND | Anode terminals |
Algorithm
The following algorithm is used to interface and control the LED bar:
- Start the Arduino IDE.
- Declare integer variables for the four LED bar pins.
- In the void setup(), configure all the LED pins as OUTPUT.
- In the void loop(), make all the LED pins HIGH.
- Give a delay of one second.
- Make all the LED pins LOW.
- Give a delay of one second.
- Repeat the process continuously.
The overall operation is:
ESP32 → GPIO Pins → LED Bar → LEDs ON/OFF
Working
The ESP32 controls the LED bar using four digital GPIO pins.
During operation, the program alternately changes the state of all four LED control pins.
First, all four LED pins are made HIGH.
GPIO 12, 13, 14, 15 = HIGH
After a delay of one second, all four LED pins are made LOW.
GPIO 12, 13, 14, 15 = LOW
The process repeats continuously.
Timing
The LED bar is controlled with a delay of 1000 milliseconds between the two output states.
The sequence is:
All Pins HIGH → 1000 ms delay → All Pins LOW → 1000 ms delay → Repeat
Program Codes
LED Bar ON and OFF
/* Make the LEDs on LED bar ON and OFF */
int led1Pin = 12;
int led2Pin = 13;
int led3Pin = 14;
int led4Pin = 15; // pins declaration of LED bar;
void setup()
{
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT); // Set all pins as output;
}
void loop()
{
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, HIGH);
digitalWrite(led4Pin, HIGH);
delay(1000);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, LOW);
delay(1000);
}
ESP32 Builtin LED Blinking
void setup() {
// put your setup code here, to run once:
pinMode(2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(2, HIGH);
delay(500);
digitalWrite(2, LOW);
delay(500);
}
Apart from these you can make more patterns and combinations of LED blinking by varying the timing and sequence of turning LED ON and OFF.
Observation
After connecting the ESP32 to the LED bar and running the program, observe the LED bar output.
The four LED control pins alternate between HIGH and LOW states.
The LEDs remain in the HIGH state for 1000 ms and in the LOW state for 1000 ms.
The LEDs turn ON or OFF according to the configuration of the LED bar and the logic applied to their control terminals.
Result
The ESP32 successfully interfaces with the LED bar and controls the four LEDs using digital GPIO pins.
The LED bar output alternates between the two programmed states with a 1000 ms HIGH duration and a 1000 ms LOW duration.