Arduino Nano - LED - Blink Without Delay

Let us envision that Arduino Nano has two tasks to accomplish: blinking an LED and monitoring the state of a button which can be pressed at any time. If we use the delay() function (as discussed in a prior tutorial), Arduino Nano may overlook some of the button presses. In other words, Arduino Nano is not able to fully complete the second task.

This tutorial instructs you how to make Arduino Nano blink an LED and detect the state of a button without any pressing events being missed.

We will go through three examples and compare the differences between them:

This method is not just limited to blinking an LED and checking the button's state. Generally, it allows Arduino Nano to perform multiple tasks simultaneously without blocking with each other.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×LED
1×220 ohm resistor
1×Push Button
1×(Optional) Panel-mount Push Button
1×Breadboard
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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

If you are unfamiliar with LED and button (including pinout, functionality, and programming), the following tutorials can help:

Wiring Diagram

The wiring diagram between Arduino Nano and LED

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - With Delay

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-led-blink-without-delay */ const int LED_PIN = 5; // The number of the LED pin const int BUTTON_PIN = 2; // The number of the button pin const long 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); } 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

  • Connect your Arduino Nano to your computer using a USB cable.
  • Launch the Arduino IDE, select the correct board and port.
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and upload the code to the Arduino Nano.
How to upload code to Arduino Nano
  • Open the Serial Monitor.
  • Press the button four times.
  • Check out the LED; it will alternate between being on and off every second.
  • Check the output in the Serial Monitor.
COM6
Send
1 0
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • On Serial Monitor, some pressing times were not registered. This is due to the fact that during delay time, Arduino Nano is unable to perform any actions. Consequently, it is not able to detect the pressing event.

Arduino Nano Code - Without Delay

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-led-blink-without-delay */ const int LED_PIN = 5; // The number of the LED pin const int BUTTON_PIN = 2; // The number of the button pin const long 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_time_ms = 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 time_ms = millis(); if (time_ms - prev_time_ms >= 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_time_ms = time_ms; } // 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

  • Execute the code and press the button four times.
  • Check out the LED; it will switch between ON and OFF at one-second intervals.
  • Check the output in the Serial Monitor.
COM6
Send
1 0 1 0 1 0 1 0
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • All occurrences of urgent matters were identified.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Adding More Tasks

The Arduino Nano code below does:

  • Makes two LEDs blink with different intervals.
  • Checks the state of the button
/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-led-blink-without-delay */ const int LED_PIN_1 = 5; // The number of the LED 1 pin const int LED_PIN_2 = LED_BUILTIN; // The number of the LED 2 pin const int BUTTON_PIN = 2; // The number of the button pin 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) 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_time_ms_1 = 0; // will store last time LED 1 was updated unsigned long prev_time_ms_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 time_ms = millis(); // check to see if it's time to blink the LED 1 if (time_ms - prev_time_ms_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_time_ms_1 = time_ms; } // check to see if it's time to blink the LED 2 if (time_ms - prev_time_ms_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_time_ms_2 = time_ms; } // 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

Extendability

This method can be used to enable Arduino Nano to execute multiple tasks concurrently, without one task blocking the progress of the other. For instance, sending a request to the Internet and waiting for the response, while simultaneously flashing some LED indicators and monitoring the cancel button.