delayMicroseconds() & delay() function in Arduino??

What is the delay function in Arduino, and what delay function does in code? & how to use the delay function in Arduino programming are some questions that may arise while programming an Arduino.

Hello guys! Welcome back to my blog today we are going to learn about Arduino timer functions so let’s begin.

What is delay() in Arduino programming??

As the name suggests delay means causing a delay in the program means pausing the program for some amount of defined time. To get clearer we will take an example you are drinking a soft drink directly from a bottle. You don’t drink a full drink at a time rather you take a number of small sips to finish your drink. Here the gap between two sips where you take a break is just like a delay in programming. Delay also does the same thing in Arduino. It holds the program for some amount of time and then resumes to remaining operation. delay functions are mostly used in operations where data is to send received or read after a certain interval of time. for example, you want to read data from the gas sensor every after 2 secs or send data to the server (with Ethernet Shield or Nodemcu) from sensor every after 5 sec or 5000 milliseconds, etc.

Arduino delay function

Syntax – delay(ms)

delay function takes only one argument, Which will be the amount of time we have to pause the code. The values will be in milliseconds.

  delay(1000)  -  means delay of 1 sec

  delay(5000) -   means delay of 5 sec

The argument decides how much amount of time we want to pause the code. delay function does not return any values. We can also use variables in the delay function.

 int time = 3000;

delay(time);

It works exactly as same as passing int values.

delayMicrosecond(μs)

This function is similar to the delay function except for delayMicroseconds() holds the program for microseconds and the delay() function holds the program for milliseconds.

Arduino delay function example

delay milliseconds Arduino

In the above example, we are glowing LED and turn OFF. we have introduced a delay of 1 sec (1000 milliseconds). this delay function will turn ON the LED for 1 sec and then turn OFF the LED for 1 sec. as this code is written in the void loop, the LED continues to turn ON and turn OFF.  

Code for delay() - 

int LED_pin = 4;
void setup() 
{
 pinMode(LED_pin,OUTPUT);
 Serial.begin(115200);
}

void loop() 
{
digitalWrite(LED_pin,HIGH);
 delay(1000); 
digitalWrite(LED_pin,LOW);
 delay(1000); 
}

Code for delayMicroseconds() - 

int LED_pin = 4;
void setup() 
{
 pinMode(LED_pin,OUTPUT);
 Serial.begin(115200);
}

void loop() 
{
digitalWrite(LED_pin,HIGH);
 delayMicroseconds(50); 
digitalWrite(LED_pin,LOW);
delayMicroseconds(50); 
}

Conclusion -

Today we learn about various timer functions in Arduino such as Arduino delay milliseconds and Arduino delay microseconds function in Arduino, and how to use the delay function with an example of LED.

"I hope you find this IOT blog very helpful to you. In the next upcoming lesson, we will learn more amount Arduino time functions. We will learn about millis() and micros(), till then bye see you in the next chapter of Arduino lessons."

Close Menu