Delay and Millis in Arduino

In this tutorial, we will learn how to use delay and millis function in Arduino programs.

Must See:

Creating Delay()

We want our sketch to pause for some period of time. This may be some number of milliseconds or time given in seconds, minutes, hours, or days.

The Arduino delay function is used in many sketches, as we have seen. delay pauses a sketch for the number of milliseconds specified as a parameter (there are 1,000 milliseconds in one second). The sketch that shows how we can use the delay to get almost any interval.

The delay function has a range from one one-thousandth of a second to around 25 days (just less than 50 days if using an unsigned long variable type).

The delay function pauses the execution of your sketch for the duration of the delay. You can use delayMicroseconds to delay short periods.

There are 1,000 microseconds in one millisecond, and 1 million microseconds in one second. delayMicroseconds will pause from one microsecond to around 16 milliseconds, but for delays longer than a few thousand microseconds you should use delay instead:          delayMicroseconds(10);  //delay for a 10 microseconds

delay and delayMicroseconds will delay for at least the amount of time given as the parameter, but they could delay a little longer if interrupts occur within the delay time.


Using millis() to Determine Duration

We want to know how much time has elapsed since an event happened; for example, how long a switch has been held down.

Arduino has a function named millis (short for milliseconds) that is used in the following sketch to print how long a button was pressed.

delay and millis in arduino

The millis function returns the number of milliseconds since the current sketch started running. This number will overflow (go back to zero) in approximately 50days.
By storing the start time for an event, you can determine the duration of the event by subtracting the start time from the current time, as shown here:

Long duration = millis() – startTime;

We can customize the functionality for your application, but in this example, an LED is flashed five times per second even while the print statement in the loop is delayed for four-second intervals:

** Connect a LED on Pin 13

You can put code in the myDelay function for an action that you want to happen repeatedly while the function waits for the specified time to elapse.


Measuring the Duration of Pulse

We want to determine the duration of a pulse with microsecond accuracy; for example, to measure the exact time between two button presses.

The pulseIn function returns the duration in microseconds for a changing signal on a digital pin. This sketch prints the time that the voltage on a digital pin is held LOW by a button press.

pulseIn can measure how long a pulse is either HIGH or LOW:

pulseIn(pin, HIGH); //return microseconds that pulse is HIGH
pulseIn(pin, LOW); //returns microseconds that pulse is LOW

the pulseIn function waits for the pulse to start (or for a timeout if there is no pulse). By default, it will stop waiting after one second, but you can change that by specifying the time to wait in microseconds as a third parameter (note that 1,000 microseconds equal 1 millisecond):

pulseIn(pin, HIGH, 5000); //wait 5 milliseconds for the pulse to start

pulseIn can measure values between around 10 microseconds to three minutes in duration, but the value of long pulses may not be very accurate.

The timeout value only matters if the pulse does not start within the given period. Once the start of a pulse is detected, the function will start timing and will not return until the pulse ends.


Some More Articles For You:

Scroll to Top