Table of Contents
To blink LEDs in stack form using arduino we first know about stack. Stack is a data structure in which data can be add or remove at only one end called the TOP
Examples of Stack are :
– Stacks of books
– Stack of tray in cafeteria
Suppose we have 8 books start keeping books one by one at the top of each other. After placing books take books one by one starting from the top, the book placed by us at the last we will get it first, continuing the process the books placed at first by us we will get it on last, This is stack (LIFO – Last in First out).
Required hardware or components for Lighting up LED in Stack form using Arduino
S.N. | Component | Quantity |
---|---|---|
1. | Arduino Uno | 1 |
2. | Breadboard | 1 |
3. | LED | 4 |
4. | Resistor 220 or 280 ohm | 4 |
CIRCUIT DIAGRAM : BLINK LEDS IN STACK FORM USING ARDUINO
CONNECTION TABLE
S.N. | Arduino | LED |
---|---|---|
1. | GND | Cathode (-) |
2. | 2 | LED 1 - Anode (+) |
3. | 3 | LED 2 - Anode (+) |
4. | 4 | LED 3 - Anode (+) |
5. | 5 | LED 4 - Anode (+) |
ARDUINO CODE : BLINK LEDS IN STACK FORM USING ARDUINO
void setup() { pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); } void loop() { digitalWrite(2, HIGH); delay(500); digitalWrite(3, HIGH); delay(500); digitalWrite(4, HIGH); delay(500); digitalWrite(5, HIGH); delay(500); digitalWrite(5, LOW); delay(500); digitalWrite(4, LOW); delay(500); digitalWrite(3, LOW); delay(500); digitalWrite(2, LOW); delay(500); }
CODE EXPLANATION
Step-1:
In setup() block we assign pin number 2, 3, 4 & 5 of Arduino as output.
void setup() { pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); }
Step-2:
In the loop() block, in the first half we first want to turn ON led connected on pin number 2 and then 3, 4 & 5 with a delay of 0.5 seconds (500 milliseconds) to each.
void loop() { digitalWrite(2, HIGH); delay(500); digitalWrite(3, HIGH); delay(500); digitalWrite(4, HIGH); delay(500); digitalWrite(5, HIGH); delay(500);
Step-3:
In the second half, we want to turn OFF the led connected on pin number 5 and then 4, 3, and at last 2 with a delay of 0.5 seconds (500 milliseconds) to each.
digitalWrite(5, LOW); delay(500); digitalWrite(4, LOW); delay(500); digitalWrite(3, LOW); delay(500); digitalWrite(2, LOW); delay(500); }
And the void loop() gets executed continuously and follows the same pattern.
READ NEXT
BLINK LED’S IN AN ORDER USING ARDUINO