Arduino - Blink multiple LED
This tutorial teaches you how to program an Arduino to make multiple LEDs blink simultaneously without using the delay function. The tutorial provides code in two ways:
- Arduino blink multiple LEDs
- Arduino blink multiple LEDs using arrays.
We will use three LEDs as examples. You can easily modify it to adapt for two LEDs, four LEDs, or even more.
Hardware Required
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
About LED
We have a detailed LED tutorial including hardware pinout, working principle, Arduino wiring, and code instructions. Learn more here:
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Arduino Code - Blink Multiple LEDs
To blink multiple LEDs simultaneously, we can't rely on the delay() function. This is because using delay() blocks other code execution, preventing us from blinking multiple LEDs at the same time. Instead, we utilize the millis() function to manage timestamps effectively.
For beginners, managing timestamps while blinking multiple LEDs can be challenging. Fortunately, the ezLED library simplifies this process by internally handling timestamp management. This removes the burden of managing timestamps from users when utilizing the library. Additionally, employing an array of LEDs can improve code clarity and conciseness.
Quick Steps
- Do the wiring as above image.
- Connect the Arduino board to your PC via a USB cable
- Open Arduino IDE on your PC.
- Select the right Arduino board (e.g. Arduino Uno) and COM port.
- Click to the Libraries icon on the left bar of the Arduino IDE.
- Search “ezLED”, then find the led library by ArduinoGetStarted
- Click Install button to install ezLED library.
- Copy the above code and paste it to Arduino IDE.
- Compile and upload code to Arduino board by clicking Upload button on Arduino IDE
- Check out the status of LED
Arduino Code - Blink Multiple LEDs by using array
We can improve the code above by employing an array of LEDs. The following code utilizes this array to handle LED objects.
By using the ezLED library, we can blink multiple LED flexiblly as follows:
- Blink multiple LED with different freqency
- Blink multiple LED with different start times
- Blink each LED in a period of time
- Blink each LED in number of times (cycle)
See the example code in the library.
Video Tutorial
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.