Blink LEDs in an Order Using Arduino

To blink LEDs in an Order using Arduino, it means, blink first led then blink second and so on. To do this we have to use code we already used for blinking a single LED, but here we use it inside for loop, so that one by one LEDs will blink.

Required components for Lighting up LED in an order using Arduino

S.N.ComponentQuantity
1.Arduino Uno1
2.Breadboard1
3.LED4
4.Resistor 220 or 280 ohm4

Circuit Diagram : Blink LEDs in an Order using Arduino

Circuit diagram blink LEDs in an Order using Arduino
Circuit diagram Arduino and many Led’s

CONNECTION TABLE

S.N.ArduinoLED
1.GNDCathode (-)
2.2LED 1 - Anode (+)
3.3LED 2 - Anode (+)
4.4LED 3 - Anode (+)
5.5LED 4 - Anode (+)

Arduino Programming Code : To blink LEDs in an Order using Arduino

#define del 500
int i;

void setup() {
  for (i = 2 ; i <= 5; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  for (i = 2; i <= 5 ; i++) {
    digitalWrite(i, HIGH);
    delay(del);
    digitalWrite(i, LOW);
    delay(del);
  }
}

Read Next – DIM AND BRIGHT LED GRADUALLY : LED INTENSITY VARIATION (PWM) USING ARDUINO

Code Explanation

Step-1:

#define is a useful C component that allows the programmer to give a name to a constant value before the program is compiled. Defined constants in Arduino don’t take up any program memory space on the chip. The compiler will replace references to these constants with the defined value at compile time.

Here we define a variable del (it can be any word del/mark/etc) to 500. So that where we use del it means 500. This will gives us an advantage if we want to change the delay time we do not need to change it everywhere in the program instead we can change it at first line, isn’t cool?

Then we define an integer variable i.

#define del 500
int i;

Step-2:

In this block we use a for-loop because we want to do the same work again and again i.e. We want to set the mode of the pin number as output in an order from number two to five. Loop will runs 4 time as the loop runs in the first cycle it assign pin 2 as an output then in second cycle it assign pin 3 as an output and same in third cycle pin 4 and in forth cycle pin 5.

void setup() {
  for (i = 2 ; i <= 5; i++) {
    pinMode(i, OUTPUT);
  }
}

Step-3:

1. What we need to do, we need to nested blinking code inside for-loop with condition that is started with led connected at pin number 2 of Arduino board to blink it.

2. Then for-loop increase the value of i by 1 to blink led connected at pin number 3 of Arduino board. 3. Then for-loop increase the value of i by 1 to blink led connected at pin number 4 of Arduino board. 4. Then for-loop increase the value of i by 1 to blink led connected at pin number 5 of Arduino board.

void loop() {
  for (i = 2; i <= 5 ; i++) {
    digitalWrite(i, HIGH);
    delay(del);
    digitalWrite(i, LOW);
    delay(del);
  }
}

And the void loop() gets executed continuously and follows the same pattern.

 

Read Next – DIM AND BRIGHT LED GRADUALLY : LED INTENSITY VARIATION (PWM) USING ARDUINO

Leave a Reply

avatar
  Subscribe  
Notify of

Arduino Tutorials with Basic Electronics: CONTENT

Translate »