Add delay in Arduino


In order to add time delays in Arduino, you can use the delay() function. It takes as an argument the value of the delay in milliseconds. An example execution is given below −

Example

void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
}
void loop() {
   // put your main code here, to run repeatedly:
   Serial.print("Hello!");
   delay(2000);
}

The above code prints "Hello!" every 2 seconds. As you may have guessed, the minimum delay you can introduce using the delay function is 1 milli-second. What if you want an even shorted delay? Arduino has a delayMicroseconds() function for that, which takes in the value of the delay in microseconds as the argument.

Example

void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
}
void loop() {
   // put your main code here, to run repeatedly:
   Serial.print("Hello!");
   delayMicroseconds(2000);
}

The above code prints "Hello!" every 2 milliseconds (2000 microseconds).

Updated on: 23-Mar-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements