Arduino Multiple LEDs With Different Delays

Arduino Multiple LEDs With Different Delays

In this tutorial, you will learn essential information about a Light-Emitting Diode (LED) and how to control multiple LEDs with Arduino.

Using an Arduino Uno board, you will learn how to implement a project controlling multiple LEDs with different delays.

Hardware components

Arduino Uno Rev3x1Amazon
Arduino Mega (Optional)x1Amazon
LEDx3Amazon
220Ω Resistorx1Amazon
Breadboardx1Amazon
Jumper wiresx10Amazon
USB cable type A/Bx1Amazon

Software

Arduino IDEArduino IDE

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com.

What is a Light Emitting Diode (LED)?

LED Symbol
LED Symbol

A Light-emitting diode (LED) is a small semiconductor device, Which can emit light when an electric current flows through it. 

When current passes through an LED, electrons in the semiconductor recombine with an electron-hole and release energy in the form of photons.

LEDs allow the current to flow forward and block the current in the reverse direction.

LEDs are used in many different applications, such as TV Backlighting, Automotive Lighting, and Smartphone Backlighting.

How Do You Control Multiple LED Lights

An Arduino can theoretically control as many LEDs as you require for your design, only limited by the power supply current available.

However, if you want to control more LEDs than digital pins on your Arduino board, you need to use some additional hardware.

I am using 3 LEDs with an Arduino Uno board, as shown in the wiring diagram.

-> Learn more about How Easy Is It To Learn Arduino here.

Wiring Diagram

Step 1: Connect the LEDs anode (+) to a digital pin of the Arduino pin mentioned in the table below with a 220 Ohm resistor.

Connect the LEDs as per the following table.

LEDArduino Pin
REDDigital Pin 11
GREENDigital Pin 10
BLUEDigital Pin 9

Step 2: Connect all the LEDs cathode (-) to GND.

Connect all the LEDs cathode

Arduino Code

Following Arduino code is used to control the three LEDs with different delays.

#define Blink_LED_1 11
#define Blink_LED_2 10
#define Blink_LED_3 9
void setup()
{
  Serial.begin(9600);
  pinMode(Blink_LED_1, OUTPUT);
  pinMode(Blink_LED_2, OUTPUT);
  pinMode(Blink_LED_3, OUTPUT);

}

void loop()
{
  digitalWrite(Blink_LED_1, HIGH);
  digitalWrite(Blink_LED_2, LOW);
  digitalWrite(Blink_LED_3, LOW);
  Serial.println("LED_1 ON");
  delay(3000);
  digitalWrite(Blink_LED_1, LOW);
  digitalWrite(Blink_LED_2, HIGH);
  digitalWrite(Blink_LED_3, LOW);
  Serial.println("LED_2 ON");
  delay(2000);
  digitalWrite(Blink_LED_1, LOW);
  digitalWrite(Blink_LED_2, LOW);
  digitalWrite(Blink_LED_3, HIGH);
  Serial.println("LED_3 ON");
  delay(1000);
}

How The Code Works

Step 1: First, define the digital Pin for LEDs as per the wiring diagram.

#define Blink_LED_1  11
#define Blink_LED_2  10
#define Blink_LED_3  9

Step 2:

void setup()
{
  Serial.begin(9600);
  pinMode(Blink_LED_1, OUTPUT);
  pinMode(Blink_LED_2, OUTPUT);
  pinMode(Blink_LED_3, OUTPUT);
}

In the void setup() function, LEDs are set as an OUTPUT mode using pinMode(); function. Initialize the serial communication using Serial.begin() function.

Step 3: In the void loop() function, Blink the LEDs, and the result shows on the serial terminal as follows in the table.

Blink_LED_1Blink_LED_2Blink_LED_3DurationSerial Terminal Message
ONOFFOFF3 secondLED_1 ON
OFFONOFF2 secondLED_2 ON
OFFOFFON1 secondLED_3 ON

Output On The Serial Terminal

Output On The Serial Terminal

Using millis () Function To Control Multiple LEDs

Using the millis() function, you can set the required delay. The following code will help you understand how to use millis() to control multiple LEDs.

-> Read our guide about What You Can Build with Adruino.

Arduino Code

This code generates the different delays using the millis() function to control the multiple LEDs at different rates.

// Which pins are connected to which LED
#define Blink_LED_1 11
#define Blink_LED_2 10
#define Blink_LED_3 9

// Assign flags for status of LEDs
volatile int blink_LED_1_flag = 0;
volatile int blink_LED_2_flag = 0;
volatile int blink_LED_3_flag = 0;

//Assign delays intervals for different LEDs
const unsigned long Blink_LED_1_interval = 1000;
const unsigned long Blink_LED_2_interval = 2000;
const unsigned long Blink_LED_3_interval = 3000;

// Declaring the variables holding the timer values for each LED.
unsigned long Blink_LED_1_timer = 0;
unsigned long Blink_LED_2_timer = 0;
unsigned long Blink_LED_3_timer = 0;

// Setting 3 digital pins direction as output
void setup ()
{
  Serial.begin(9600);
  pinMode(Blink_LED_1, OUTPUT);
  pinMode(Blink_LED_2, OUTPUT);
  pinMode(Blink_LED_3, OUTPUT);

}
void toggle_LED1 ()
{
  if (blink_LED_1_flag == 0) {
	digitalWrite (Blink_LED_1, HIGH);
	blink_LED_1_flag = 1;
  }
  else {
	digitalWrite (Blink_LED_1, LOW);
	blink_LED_1_flag = 0;
  }
  Serial.println("LED_1 Toggle");
  Blink_LED_1_timer = millis ();       	// stores current value of millis()
}
void toggle_LED2 ()
{
  if (blink_LED_2_flag == 0) {
	digitalWrite (Blink_LED_2, HIGH);
	blink_LED_2_flag = 1;
  }
  else {
	digitalWrite (Blink_LED_2, LOW);
	blink_LED_2_flag = 0;
  }
  Serial.println("LED_2 Toggle");
  Blink_LED_2_timer = millis ();            	// stores current value of millis()
}

void toggle_LED3 ()
{
  if (blink_LED_3_flag == 0) {
	digitalWrite (Blink_LED_3, HIGH);
	blink_LED_3_flag = 1;
  }
  else {
	digitalWrite (Blink_LED_3, LOW);
	blink_LED_3_flag = 0;
  }
  Serial.println("LED_3 Toggle");
  Blink_LED_3_timer = millis ();             	// stores current value of millis()

}
void loop ()
{
  if ( (millis () - Blink_LED_1_timer) >= Blink_LED_1_interval) {

	toggle_LED1 ();

  }
  if ( (millis () - Blink_LED_2_timer) >= Blink_LED_2_interval) {

	toggle_LED2 ();
  }
  if ( (millis () - Blink_LED_3_timer) >= Blink_LED_3_interval) {

	toggle_LED3 ();
  }
}

How The Code Works

In this code, Using the millis() function LED will turn ON and turn OFF. There is no blocking condition here.

Step 1: First, define the digital pins for LEDs as per the wiring diagram.

#define Blink_LED_1  11
#define Blink_LED_2  10
#define Blink_LED_3  9

Step 2:  Next, I will use variables for different LEDs to store the current status LEDs.

// Assign flags for status of LEDs
volatile int blink_LED_1_flag = 0;
volatile int blink_LED_2_flag = 0;
volatile int blink_LED_3_flag = 0;

Step 3: Define the variable to store the value of different delays for LEDs.

const unsigned long Blink_LED_1_interval = 1000;
const unsigned long Blink_LED_2_interval = 2000;
const unsigned long Blink_LED_3_interval = 3000;

Step 4: Declaring the variables holding the timer values for each LED, initializing with zero.

unsigned long Blink_LED_1_timer=0;
unsigned long Blink_LED_2_timer=0;
unsigned long Blink_LED_3_timer=0;

Step 5: In the void setup() function, LEDs are set as an OUTPUT mode using pinMode() function.

I have also initialized the serial monitor with a 9600 baud rate.

Arduino’s millis() function returns the number of milliseconds the program has started running.

Here, Assign the millis (); values in the timer variable.

void setup ()
{
  Serial.begin(9600);
  pinMode(Blink_LED_1, OUTPUT);
  pinMode(Blink_LED_2, OUTPUT);
  pinMode(Blink_LED_3, OUTPUT);
}

Step 6: Finally, I have used the toggle LED function to check the current status of the LED with the help of the flag variable.

If the flag is zero, it will turn ON the LED; otherwise, if the flag is one, it will turn OFF the LED.

In the end, the respective LED timer is also updated to the current time.

void toggle_LED1 ()
{
  if (blink_LED_1_flag == 0) {
	digitalWrite (Blink_LED_1, HIGH);
	blink_LED_1_flag = 1;
  }
  else {
	digitalWrite (Blink_LED_1, LOW);
	blink_LED_1_flag = 0;
  }
  Serial.println("LED_1 Toggle");
  Blink_LED_1_timer = millis ();       	// stores current value of millis()
}
void toggle_LED2 ()
{
  if (blink_LED_2_flag == 0) {
	digitalWrite (Blink_LED_2, HIGH);
	blink_LED_2_flag = 1;
  }
  else {
	digitalWrite (Blink_LED_2, LOW);
	blink_LED_2_flag = 0;
  }
  Serial.println("LED_2 Toggle");
  Blink_LED_2_timer = millis ();            	// stores current value of millis()
}

void toggle_LED3 ()
{
  if (blink_LED_3_flag == 0) {
	digitalWrite (Blink_LED_3, HIGH);
	blink_LED_3_flag = 1;
  }
  else {
	digitalWrite (Blink_LED_3, LOW);
	blink_LED_3_flag = 0;
  }
  Serial.println("LED_3 Toggle");
  Blink_LED_3_timer = millis ();             	// stores current value of millis()

}

Step 7: In void loop(), The LED will toggle when the condition is true.

Here, millis(); function stores the current time.

Blink_LED_2_timer variable is saved the last time you blinked the LED2.

I am using the Blink_LED_2_interval time variable, a predefined delay for LED2.

There are no blocking conditions. Many times this code will execute in a second. LEDs will toggle as per the time interval variables.

void loop () {
  if ( (millis () - Blink_LED_1_timer) >= Blink_LED_1_interval)
    toggle_LED1 ();

  if ( (millis () - Blink_LED_2_timer) >= Blink_LED_2_interval)
    toggle_LED2 ();

  if ( (millis () - Blink_LED_3_timer) >= Blink_LED_3_interval)
    toggle_LED3 ();
}

Output On The Serial Monitor

Output On The Serial Monitor

How do you blink two LEDs at different rates?

You can blink two LEDs at different rates using the delay function or millis(). You can set different delays for both LEDs.

For example, for LED1, you can use delay(1000), and for LED2, delay(2000).

Thus, with different delays, both LEDs will blink at different rates. 

> Check out our guide to the Top 12 Best Arduino Online Courses

Conclusion

This article shows you how to control 3 LEDs with different delay functions and without delays.

I hope you found this article informative. I would love to know what project you plan on building or have already made with the Arduino.

If you have any queries or suggestions or think things are missing in this tutorial, please leave a comment below.

Note that comments are held for moderation to prevent spam.