ESP32 - LED - Blink Without Delay

Let's imagine that ESP32 has to do two tasks: blinking LED and checking the button state, which can be pressed anytime. If we use the delay() function (described in a previous tutorial), ESP32 may miss some of the pressing events. In other words, ESP32 cannot fully do the second task.

In this tutorial, we will learn how ESP32 blinks LED and checks the button's state without missing any pressing event.

Thi method is not just only for blinking LED and checking the button's state. Generally, this method lets ESP32 do several tasks at the same time without blocking each other.

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×LED
1×220 ohm resistor
1×Button
1×Breadboard
4×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About LED and Button

If you do not have knowledge of LED and button (pinout, how they work, how to connect them to ESP32, how to program for them...), learn about them in the following tutorials:

Wiring Diagram

ESP32 LED button Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

ESP32 Code - With Delay

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-led-blink-without-delay */ // constants won't change: const int LED_PIN = 18; // GIOP18 pin connected to LED const int BUTTON_PIN = 16; // GIOP16 pin connected to button const long BLINK_INTERVAL = 1000; // interval at which to blink LED (milliseconds) // Variables will change: int ledState = LOW; // ledState used to set the LED int previousButtonState = 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); } void loop() { // if the LED is off turn it on and vice-versa: ledState == (ledState == LOW) ? HIGH : LOW; // set the LED with the ledState of the variable: digitalWrite(LED_PIN, ledState); delay(BLINK_INTERVAL); // If button is pressed during this time, Arduino CANNOT detect int currentButtonState = digitalRead(BUTTON_PIN); if(currentButtonState != previousButtonState) { // print out the state of the button: Serial.println(currentButtonState); // save the last state of button previousButtonState = currentButtonState; } // DO OTHER WORKS HERE }

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE, select the right ESP32 board and COM port
  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • How to upload 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, some pressing times were missed. That is because, during delay time, ESP32 CANNOT do anything. Therefore, it is unable to detect the pressing event.

ESP32 Code - Without Delay

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-led-blink-without-delay */ // constants won't change: const int LED_PIN = 18; // GIOP18 pin connected to LED const int BUTTON_PIN = 16; // GIOP16 pin connected to button const long BLINK_INTERVAL = 1000; // interval at which to blink LED (milliseconds) // Variables will change: int ledState = LOW; // ledState used to set the LED int previousButtonState = LOW; // will store last time button was updated unsigned long previousMillis = 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); } 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 currentMillis = millis(); if (currentMillis - previousMillis >= BLINK_INTERVAL) { // if the LED is off turn it on and vice-versa: ledState == (ledState == LOW) ? HIGH : LOW; // set the LED with the ledState of the variable: digitalWrite(LED_PIN, ledState); // save the last time you blinked the LED previousMillis = currentMillis; } // check button state's change int currentButtonState = digitalRead(BUTTON_PIN); if(currentButtonState != previousButtonState) { // print out the state of the button: Serial.println(currentButtonState); // save the last state of button previousButtonState = currentButtonState; } // DO OTHER WORKS HERE }

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for 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
  • COM6
    Send
    1 0 1 0 1 0 1 0
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  
  • All pressing events were detected.

Code Explanation

Read the line-by-line explanation in comment lines of code!

Adding More Tasks

The below code blinks two LEDs with different intervals and checks the state of the button.

ESP32 LED two button Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-led-blink-without-delay */ // constants won't change: const int LED_PIN_1 = 18; // GIOP18 pin connected to LED const int LED_PIN_2 = 21; // GIOP21 pin connected to LED const int BUTTON_PIN = 16; // GIOP16 pin connected to button const long BLINK_INTERVAL_1 = 1000; // interval at which to blink LED 1 (milliseconds) const long BLINK_INTERVAL_2 = 500; // interval at which to blink LED 2 (milliseconds) // Variables will change: int ledState_1 = LOW; // ledState used to set the LED 1 int ledState_2 = LOW; // ledState used to set the LED 2 int previousButtonState = LOW; // will store last time button was updated unsigned long previousMillis_1 = 0; // will store last time LED 1 was updated unsigned long previousMillis_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); } void loop() { unsigned long currentMillis = millis(); // check to see if it's time to blink the LED 1 if (currentMillis - previousMillis_1 >= BLINK_INTERVAL_1) { // if the LED is off turn it on and vice-versa: ledState_1 == (ledState_1 == LOW) ? HIGH : LOW; // set the LED with the ledState of the variable: digitalWrite(LED_PIN_1, ledState_1); // save the last time you blinked the LED previousMillis_1 = currentMillis; } // check to see if it's time to blink the LED 2 if (currentMillis - previousMillis_2 >= BLINK_INTERVAL_2) { // if the LED is off turn it on and vice-versa: ledState_2 == (ledState_2 == LOW) ? HIGH : LOW; // set the LED with the ledState of the variable: digitalWrite(LED_PIN_2, ledState_2); // save the last time you blinked the LED previousMillis_1 = currentMillis; } // check button state's change int currentButtonState = digitalRead(BUTTON_PIN); if(currentButtonState != previousButtonState) { // print out the state of the button: Serial.println(currentButtonState); // save the last state of button previousButtonState = currentButtonState; } // DO OTHER WORKS HERE }

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.

Extendability

This method can be used to let ESP32 do several tasks at the same time without blocking each other. For example, sending a request to the Internet and waiting for the response, while waiting for the response, blink some LED indicators and check the cancel button.

LED on Commercial Products

Small LEDs usually are used to indicate the status of devices. For examples:

Big LEDs usually are used for lighting. They can be combined into groups. For examples:

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere