Arduino Blink Without Delay() Explained

In this arduino tutorial, we shall discuss how to blink an LED without using the delay() function. Arduino program uses the delay() function to freeze the condition of a sketch running in an arduino at any moment it is evoked. If an arduino project contains a code that blinks an LED, say we have:

Simple Delay Code
Figure 1: Simple Delay Code

This means that the variable ledPin will go to a high state and remain on that state for 2 seconds, then go to a LOW state and remain on that LOW state for 2 seconds. For these periods that ledPin stay on a HIGH state and LOW state, the activities that are happening in the arduino during these times freeze to whatever they are doing and no other code in the sketch will be executed during those times. So, let’s say that a part of the sketch in question is a code that can receive a button pressed anytime and do something in response. If this button is coincidentally pressed when any of the delays is in action, because the delay has frozen all new code execution, the arduino will not take note of the pressed button.

Blink Without Delay Using Millis() Function

To solve this problem, we have to find a way to blink the LED without using the delay() function. To this we will utilize the “millis()” function.

millis() is a function that keeps track of how long arduino has been ON, or from the moment the reset button of the arduino is pressed, and produce the result in milliseconds.  With this function and some other conditional functions, we can keep track of a certain time frame and blink the LED with this time frame. For example, we can choose to say, if the time from when the arduino has been ON is 1 second i.e. 1000 milliseconds ledPin should come ON, etc. if we are able to realize this blink without delay, we can include button press code in our sketch without fear of design malfunction. To implement this design, we utilize the circuit below. The circuit is a simple arduino circuit with one LED connected to pin 2 of the arduino using a resistor.

Design Components

  1. Arduino Uno  (1)
  2. 330Ω                (1)
  3. LED                  (1)
Arduino Circuit for the Design
Figure 2: Arduino Circuit for the Design
Screenshot of the Sketch
Figure 3: Screenshot of the Sketch
const int ledPin =  2;
  int ledState = LOW;           
    unsigned long previousMillis = 0;       
      const long interval = 1000; 
        void setup()
          {pinMode(ledPin, OUTPUT);}
           void loop() 
           {
             unsigned long currentMillis = millis();
              if (currentMillis - previousMillis >= interval) {
             previousMillis = currentMillis;
           if (ledState == LOW) {
        ledState = HIGH; }
      else {
    ledState = LOW;}
digitalWrite(ledPin, ledState);}
    }

Table 1: Explanation of the codes in the sketch above

Code LineCode explanation
1A variable called ledPin is declared and assigned to pin 2 of the arduino
2A variable called ledState is declared to hold the state of the LED connected to pin 2 of the arduino and is initialized to a LOW state
3A variable called previousmillis is declared to hold the previous time in milliseconds since the arduino has been ON or the reset button pressed and it’s initialized to 0.
4A variable called interval is declared to hold the time the blink will last, it is initialized to 1000.
9A variable called currentMillis is declared to compute and hold the time in milliseconds since the arduino has been ON or the reset button pressed.
10This code checks if the currentMillis minus previousMillis is equal to or greater than the interval (1000), if the condition is true, code line 11 to 13  are executed
11This code is executed if code line 10 is true. This code assigns the value of previousMillis to currentMillis. previousMillis now stores whatever value currentMillis has instead of its initial value which was zero.
12This code checks if the variable ledState equal to LOW, if yes, code 13 is executed.
13The code line toggles the content of the variable ledState from Low to HIGH.
14This code checks if code line 12 is not true, if it is not true, code line 15 is executed
15 This code line tells the variable ledState to retain its content which is LOW.
16This code line tells the arduino to output a voltage state that is same thing as the content of the variable ledState at pin 2 of the arduino which is contained in the variable called ledPin.

With the loop function, the process keeps repeating with the LED blinking after every one second.

Stay in touch for more arduino tutorials.

2 Comments on “Arduino Blink Without Delay() Explained”

Leave a Reply

Your email address will not be published. Required fields are marked *