Jump to content

LED Blinking: Difference between revisions

From cognifront
CogWikiEditor (talk | contribs)
Created page with "===Interfacing LED Bar with ESP32=== {| class="wikitable" style="float:right; margin-left:20px; width:300px;" ! colspan="2" style="font-size:120%;" | LED Bar Interface |- ! Controller | ESP32 |- ! Component | LED Bar |- ! Number of LEDs | 8 |- ! Data Pins | 4 |- ! LED Configuration | Common Anode / Common Cathode |- ! LED Voltage | Approximately 1.7 V |- ! LED Current | 5–20 mA |- ! Output | LED ON/OFF |} '''Interfacing LED Bar with ESP32''' demonstrates how an LED b..."
 
CogWikiEditor (talk | contribs)
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
===Interfacing LED Bar with ESP32===
===Interfacing LED Bar with ESP32===
{| class="wikitable" style="float:right; margin-left:20px; width:300px;"
! colspan="2" style="font-size:120%;" | LED Bar Interface
|-
! Controller
| ESP32
|-
! Component
| LED Bar
|-
! Number of LEDs
| 8
|-
! Data Pins
| 4
|-
! LED Configuration
| Common Anode / Common Cathode
|-
! LED Voltage
| Approximately 1.7 V
|-
! LED Current
| 5–20 mA
|-
! Output
| LED ON/OFF
|}


'''Interfacing LED Bar with ESP32''' demonstrates how an LED bar can be connected to an ESP32 board and controlled using digital output pins.
'''Interfacing LED Bar with ESP32''' demonstrates how an LED bar can be connected to an ESP32 board and controlled using digital output pins.
Line 59: Line 31:
|}
|}


A current-limiting resistor is connected between the LED and the ESP32 board to limit the current.
A current-limiting resistor is connected between the LED and the ESP32 to limit the current.


== LED Bar ==
== LED Bar ==
Line 65: Line 37:
An LED bar consists of multiple LEDs arranged together in a single package.
An LED bar consists of multiple LEDs arranged together in a single package.


In this practical, the LED bar contains '''8 LEDs'''.
In this practical, the LED bar contains '''4 LEDs'''.


The LEDs can be controlled by applying suitable digital signals to their terminals.
The LEDs are controlled by applying suitable digital signals to their respective terminals.


The LED bar can have two commonly used configurations:
The LED bar can have two commonly used configurations:
Line 81: Line 53:


<center>
<center>
'''Common Anode → VCC (+5 V)'''
'''Common Anode → VCC'''
</center>
</center>


Line 100: Line 72:
== Connections ==
== Connections ==


The LED bar is interfaced with the ESP32 using four digital data pins.
The LED bar is interfaced with the ESP32 using four digital GPIO pins.


{| class="wikitable"
{| class="wikitable"
Line 109: Line 81:
|-
|-
| LED Bar
| LED Bar
| Data Pin 1
| LED 1
| ESP32 GPIO
| GPIO 12
| LED control
| LED control
|-
|-
| LED Bar
| LED Bar
| Data Pin 2
| LED 2
| ESP32 GPIO
| GPIO 13
| LED control
| LED control
|-
|-
| LED Bar
| LED Bar
| Data Pin 3
| LED 3
| ESP32 GPIO
| GPIO 14
| LED control
| LED control
|-
|-
| LED Bar
| LED Bar
| Data Pin 4
| LED 4
| ESP32 GPIO
| GPIO 15
| LED control
| LED control
|-
|-
| LED Bar
| LED Bar
| VCC / Common Anode
| VCC / Common Anode
| Supply
| 3.3V / Supply
| Power
| Power
|-
|-
Line 138: Line 110:
| Ground
| Ground
|}
|}
'''Note:''' The exact ESP32 GPIO numbers depend on the circuit/program used for the practical.


== Current Limiting Resistor ==
== Current Limiting Resistor ==
Line 161: Line 131:
== Interfacing Diagram Using Breadboard ==
== Interfacing Diagram Using Breadboard ==


The LED bar is connected to the ESP32 through the required data pins.
The LED bar is connected to the ESP32 through the four GPIO pins used in the program.
 
The LED control pins are:
 
{| class="wikitable"
! 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.
The LED bar's common terminal is connected according to its configuration.
Line 188: Line 177:


# Start the Arduino IDE.
# Start the Arduino IDE.
# Declare integer variables for the four data pins of the LED bar.
# Declare integer variables for the four LED bar pins.
# In the '''void setup()''', configure all the LED pins as '''OUTPUT'''.
# In the '''void setup()''', configure all the LED pins as '''OUTPUT'''.
# In the '''void loop()''', make all the LED pins '''HIGH'''.
# In the '''void loop()''', make all the LED pins '''HIGH'''.
Line 199: Line 188:


<center>
<center>
'''ESP32 → Digital Output Pins → LED Bar → LEDs ON/OFF'''
'''ESP32 → GPIO Pins → LED Bar → LEDs ON/OFF'''
</center>
</center>


== Working ==
== Working ==


The ESP32 controls the LED bar using digital output pins.
The ESP32 controls the LED bar using four digital GPIO pins.


During operation, the program alternately changes the state of the LED control pins.
During operation, the program alternately changes the state of all four LED control pins.


First, all the required LED pins are made '''HIGH'''.
First, all four LED pins are made '''HIGH'''.


<center>
<center>
'''HIGH → LED control'''
'''GPIO 12, 13, 14, 15 = HIGH'''
</center>
</center>


After a delay of one second, all the LED pins are made '''LOW'''.
After a delay of one second, all four LED pins are made '''LOW'''.


<center>
<center>
'''LOW → LED control'''
'''GPIO 12, 13, 14, 15 = LOW'''
</center>
</center>


The process repeats continuously, causing the LED bar to change between its two output states at one-second intervals.
The process repeats continuously.


== Timing ==
== Timing ==


The LED bar is controlled with a delay of '''one second''' between the two output states.
The LED bar is controlled with a delay of '''1000 milliseconds''' between the two output states.


The sequence is:
The sequence is:


<center>
<center>
'''All Pins HIGH → 1 second delay → All Pins LOW → 1 second delay → Repeat'''
'''All Pins HIGH → 1000 ms delay → All Pins LOW → 1000 ms delay → Repeat'''
</center>
</center>
== Program Codes ==
=== LED Bar ON and OFF ===
<pre>
/* 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);
}
</pre>
=== ESP32 Builtin LED Blinking ===
<pre>
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);
}
</pre>
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 ==
== Observation ==
Line 236: Line 279:
After connecting the ESP32 to the LED bar and running the program, observe the LED bar output.
After connecting the ESP32 to the LED bar and running the program, observe the LED bar output.


The LED control pins alternate between '''HIGH''' and '''LOW''' states.
The four LED control pins alternate between '''HIGH''' and '''LOW''' states.


The output changes after every '''one second'''.
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 its control terminals.
The LEDs turn ON or OFF according to the configuration of the LED bar and the logic applied to their control terminals.


== Result ==
== Result ==


The ESP32 successfully interfaces with the LED bar and controls the LED output using digital GPIO pins.
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 delay of one second.
The LED bar output alternates between the two programmed states with a '''1000 ms''' HIGH duration and a '''1000 ms''' LOW duration.

Latest revision as of 02:58, 4 September 2026

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:

  1. Common Anode
  2. 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:

  1. Start the Arduino IDE.
  2. Declare integer variables for the four LED bar pins.
  3. In the void setup(), configure all the LED pins as OUTPUT.
  4. In the void loop(), make all the LED pins HIGH.
  5. Give a delay of one second.
  6. Make all the LED pins LOW.
  7. Give a delay of one second.
  8. 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.