ESP8266 - Blink multiple LED
This tutorial instructs you how to program the ESP8266 to blink multiple LEDs simultaneously, each blinking at a different frequency, without relying on the delay function. We'll use three LEDs as an example, but you can adjust the code to handle more LEDs easily.
Hardware Preparation
1 | × | ESP8266 NodeMCU | |
1 | × | Micro USB Cable | |
1 | × | LED | |
1 | × | 220 ohm resistor | |
1 | × | Breadboard | |
1 | × | Jumper Wires | |
1 | × | (Optional) DC Power Jack | |
1 | × | (Optional) Screw Terminal Expansion Board for ESP8266 |
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of LED
We have specific a tutorial about LED. The tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to ESP8266, ESP8266 code... Learn more about them at the following link:
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.
ESP8266 Code - Blink Multiple LEDs
To blink multiple LEDs, we cannot use the delay() function. Instead, we need to use the millis() function and manage the timestamps .
Managing timestamps while blinking multiple LEDs can be challenging for beginners. Thankfully, the ezLED library simplifies the process of blinking multiple LEDs by internally managing timestamps. This eliminates the need for users to handle timestamp management when utilizing this library. Additionally, you can enhance code clarity and brevity by employing an array of LEDs.
Detailed Instructions
To get started with ESP8266 on Arduino IDE, follow these steps:
- Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
- Wire the components according to the provided diagram.
- Connect the ESP8266 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the ESP8266) board and its corresponding 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 ESP8266 board by clicking Upload button on Arduino IDE
- Check out the status of LED
ESP8266 Code - Blink Multiple LEDs by using array
We can optimize the above code by using an array of LEDs. The below code uses the array to manage LED objects.
For the flexible blink, please refer to other functions of the ezLED library