Arduino Nano ESP32 - LED - Blink Without Delay
One of the first program that beginners run is to blink an LED. The simplest way to blink an LED is to use the delay() function. This function blocks Arduino Nano ESP32 from doing other things. It will be ok if you just want to blink only a single LED. However, If you want to blink more LED or do other works in parallel, you cannot use the delay() function. We need another solution. This tutorial provides instructions on how to do multiple task without using delay function. More specifically, We will learn how to blink an LED and checks the button's state.
We will run though three below examples and compare the difference between them.
- Arduino Nano ESP32 blinks LED by using delay() function
- Arduino Nano ESP32 blinks LED by using millis() function
- Arduino Nano ESP32 blinks LED by using ezLED library
This method can be applied to let Arduino Nano ESP32 do several tasks at the same time. Blinking LED is just an example.
Hardware Preparation
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.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Let's compare the Arduino Nano ESP32 code that blinks LED with and without using delay() function
Arduino Nano ESP32 Code - With Delay
Detailed Instructions
To get started with Arduino Nano ESP32, follow these steps:
- If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
- Wire the components according to the provided diagram.
- Connect the Arduino Nano ESP32 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the Arduino Nano ESP32 board and its corresponding COM port.* Copy the above code and paste it to Arduino IDE.
- Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
- Open Serial Monitor on Arduino IDE
- Press the button 4 times
- See the LED: The LED toggles between ON/OFF periodically every second
- See the output in Serial Monitor
- On Serial Monitor, you will not see enough four times that the state changes to 0 (4 presses). That is because, during delay time, Arduino Nano ESP32 CANNOT detect the changing.
Arduino Nano ESP32 Code - Without Delay
Detailed Instructions
- If this is the first time you use Arduino Nano ESP32, see how to setup environment for Arduino Nano ESP32 on Arduino IDE.
- Run the above code and press the button 4 times
- See the LED: The LED toggles between ON/OFF periodically every second
- See the output in Serial Monitor
- All pressing events were detected.
Line-by-line Code Explanation
The above Arduino Nano ESP32 code contains line-by-line explanation. Please read the comments in the code!
Adding More Tasks
The below code blinks two LEDs with different intervals and checks the state of the button.