Arduino - Blink Multiple LED

by DIYables in Circuits > Arduino

14 Views, 0 Favorites, 0 Comments

Arduino - Blink Multiple LED

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://arduinogetstarted.com/tutorials/arduino-blink-multiple-led

This build blinks three LEDs at the same time on an Arduino, each one on and off at its own rate, without using delay(). The same wiring and code pattern extends to two, four, or more LEDs. It uses the ezLED library so the sketch does not have to manage timestamps manually.

Supplies

Wiring

Connect three LEDs to Arduino pins 7, 8, and 9. Using the LED Module (which has a built-in resistor) simplifies the wiring since you just connect it to the pin, 5V, and GND. If you use plain LEDs on a breadboard instead, wire each one in series with a 220Ω resistor to ground.

How It Works

Blinking several LEDs at once rules out delay(), since delay() blocks the rest of the code and would stop the other LEDs from blinking independently. The sketch instead relies on millis() to track timing for each LED separately. The ezLED library wraps this timestamp handling internally, so each LED object just needs to be told its on-time and off-time and then serviced in the loop.

Code

The sketch includes the ezLED library and creates one ezLED object per LED pin (7, 8, 9). In setup(), each object's blink() call sets its own on-time and off-time in milliseconds, for example 500/500, 200/800, and 800/200, so each LED blinks at a different rate starting immediately. In loop(), each object's loop() function must be called every pass so the library can update that LED's state; other code can run in loop() alongside it without blocking the blinking. A second version of the sketch puts the LED objects in an array to make the code shorter and easier to extend to more LEDs.

arduino-blink-multiple-led.ino - the sketch from the tutorial, unchanged.

/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-blink-multiple-led
*/

#include <ezLED.h> // ezLED library by ArduinoGetStarted.com

#define PIN_LED_1 7 // The Arduino pin connected to LED 1
#define PIN_LED_2 8 // The Arduino pin connected to LED 2
#define PIN_LED_3 9 // The Arduino pin connected to LED 3

ezLED led1(PIN_LED_1); // create ezLED object that attach to pin PIN_LED_1
ezLED led2(PIN_LED_2); // create ezLED object that attach to pin PIN_LED_2
ezLED led3(PIN_LED_3); // create ezLED object that attach to pin PIN_LED_3

void setup() {
Serial.begin(9600);

led1.blink(500, 500); // 500ms ON, 500ms OFF, blink immediately
led2.blink(200, 800); // 200ms ON, 800ms OFF, blink immediately
led3.blink(800, 200); // 800ms ON, 200ms OFF, blink immediately
}

void loop() {
led1.loop(); // MUST call the led1.loop() function in loop()
led2.loop(); // MUST call the led2.loop() function in loop()
led3.loop(); // MUST call the led3.loop() function in loop()

// DO SOMETHING HERE
}

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://arduinogetstarted.com/tutorials/arduino-blink-multiple-led