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.

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

1×Arduino Nano ESP32
1×USB Cable Type-C
1×LED
1×220 ohm resistor
1×Push Button
1×(Optional) Panel-mount Push Button
1×Breadboard
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of LED and Button

We have specific tutorials about LED and button. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to ESP32, Arduino Nano ESP32 code... Learn more about them at the following links:

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and LED button

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

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-led-blink-without-delay */ #define LED_PIN D5 // The Arduino Nano ESP32 pin D5 connected to LED #define BUTTON_PIN D2 // The Arduino Nano ESP32 pin D2 connected to button #define BLINK_INTERVAL 1000 // interval at which to blink LED (milliseconds) int led_state = LOW; // led_state used to set the LED int prev_button_state = LOW; // will store last time button was updated void setup() { Serial.begin(9600); // set the digital pin as output: pinMode(LED_PIN, OUTPUT); // set the digital pin as an input: pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // if the LED is off turn it on and vice-versa: led_state = (led_state == LOW) ? HIGH : LOW; // set the LED with the led_state of the variable: digitalWrite(LED_PIN, led_state); delay(BLINK_INTERVAL); // If button is pressed during this time, Arduino CANNOT detect int button_state = digitalRead(BUTTON_PIN); if (button_state != prev_button_state) { // print out the state of the button: Serial.println(button_state); // save the last state of button prev_button_state = button_state; } // DO OTHER WORKS HERE }

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
How to upload Arduino Nano ESP32 code on Arduino IDE
  • Open Serial Monitor on Arduino IDE
How to 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
COM6
Send
1 0
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • 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

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-led-blink-without-delay */ #define LED_PIN D5 // The Arduino Nano ESP32 pin D5 connected to LED #define BUTTON_PIN D2 // The Arduino Nano ESP32 pin D2 connected to button #define BLINK_INTERVAL 1000 // interval at which to blink LED (milliseconds) int led_state = LOW; // led_state used to set the LED int prev_button_state = LOW; // will store last time button was updated unsigned long prev_millis = 0; // will store last time LED was updated void setup() { Serial.begin(9600); // set the digital pin as output: pinMode(LED_PIN, OUTPUT); // set the digital pin as an input: pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // check to see if it's time to blink the LED; that is, if the difference // between the current time and last time you blinked the LED is bigger than // The interval at which you want to blink the LED. unsigned long current_millis = millis(); if (current_millis - prev_millis >= BLINK_INTERVAL) { // if the LED is off turn it on and vice-versa: led_state = (led_state == LOW) ? HIGH : LOW; // set the LED with the led_state of the variable: digitalWrite(LED_PIN, led_state); // save the last time you blinked the LED prev_millis = current_millis; } // check button state's change int button_state = digitalRead(BUTTON_PIN); if (button_state != prev_button_state) { // print out the state of the button: Serial.println(button_state); // save the last state of button prev_button_state = button_state; } // DO OTHER WORKS HERE }

Detailed Instructions

COM6
Send
1 0 1 0 1 0 1 0
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • 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.

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-led-blink-without-delay */ #define LED_PIN_1 D6 // The Arduino Nano ESP32 pin D6 connected to LED 1 #define LED_PIN_2 D5 // The Arduino Nano ESP32 pin D5 connected to LED 2 #define BUTTON_PIN D2 // The Arduino Nano ESP32 pin D2 connected to button #define BLINK_INTERVAL_1 1000 // interval at which to blink LED 1 (milliseconds) #define BLINK_INTERVAL_2 500 // interval at which to blink LED 2 (milliseconds) int led_state_1 = LOW; // led_state used to set the LED 1 int led_state_2 = LOW; // led_state used to set the LED 2 int prev_button_state = LOW; // will store last time button was updated unsigned long prev_millis_1 = 0; // will store last time LED 1 was updated unsigned long prev_millis_2 = 0; // will store last time LED 2 was updated void setup() { Serial.begin(9600); // set the digital pin as output: pinMode(LED_PIN_1, OUTPUT); pinMode(LED_PIN_2, OUTPUT); // set the digital pin as an input: pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { unsigned long current_millis = millis(); // check to see if it's time to blink the LED 1 if (current_millis - prev_millis_1 >= BLINK_INTERVAL_1) { // if the LED is off turn it on and vice-versa: led_state_1 = (led_state_1 == LOW) ? HIGH : LOW; // set the LED with the led_state of the variable: digitalWrite(LED_PIN_1, led_state_1); // save the last time you blinked the LED prev_millis_1 = current_millis; } // check to see if it's time to blink the LED 2 if (current_millis - prev_millis_2 >= BLINK_INTERVAL_2) { // if the LED is off turn it on and vice-versa: led_state_2 = (led_state_2 == LOW) ? HIGH : LOW; // set the LED with the led_state of the variable: digitalWrite(LED_PIN_2, led_state_2); // save the last time you blinked the LED prev_millis_2 = current_millis; } // check button state's change int button_state = digitalRead(BUTTON_PIN); if (button_state != prev_button_state) { // print out the state of the button: Serial.println(button_state); // save the last state of button prev_button_state = button_state; } // DO OTHER WORKS HERE }

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!