Using Arduino Delay and Millis Commands: Arduino for Beginners

Arduino beginners will probably spend a lot of time on the Arduino Playground, exploring and experimenting with the sketches others have written. As you gain experience and begin to write your own sketches, understanding the difference between the delay() and millis() commands is essential.

Despite sharing some superficial commonalities, these two commands are quite different and useful for different kinds of applications. Here, we’ll clarify the difference and examine how to start using the Arduino millis() command effectively.

Arduino Commands: Millis vs. Delay 

Let’s start with the similarities:

1. You can use both delay() and millis() commands to regulate the timing of operations.

2. We measure both in milliseconds.

The differences, however, are what make the millis() command really shine. Simply put, the primary difference is that the delay() command regulates the timing of an activity (such as blinking an LED) by temporarily suspending nearly all of the Arduino’s functions for a specified amount of time. The millis() command, on the other hand, bases its timing on changes in a timer that starts at 0 and continues to advance, unrelated to other activities, then pauses and begins again.

In practical terms, while the delay() function interrupts the other processes – essentially putting everything on hold – the millis() command can establish timing without interrupting other functions, enabling a sort of “multitasking” effect.

For a very simple sketch, like making an LED blink without other functionality, the delay() command may be sufficient. After all, the unit doesn’t need to accomplish anything else in the time that the LED is on or off. If, however, your task involves more “moving parts” (such as blinking the LED and periodically printing out a counter), you’ll need millis() to do that.

In such a case, you can use the millis() command to set each task to occur when the counter reaches a certain difference (such as blinking every 500ms and printing out every 1500ms).

Millis () Code Example:

const byte ledPin =  13; //the number of the LED pin
byte ledState = 0; //ledState used to set the LED
unsigned long previousMillis1 = 0; //will store last time LED was updated
unsigned long interval1 = 500; //interval at which to blink (milliseconds)
unsigned long previousMillis2 = 0;
unsigned long interval2 = 1500;
unsigned int counter = 0;

void setup() {
  pinMode(ledPin, OUTPUT); //set the digital pin as output:

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis1 > interval1) {
    previousMillis1 = currentMillis; // save the last time I blinked the LED
    //if the LED is off turn it on and vice-versa:
    ledState ^= 1;
    digitalWrite(ledPin, ledState);
  currentMillis = millis();
  if (currentMillis - previousMillis2 > interval2) {
    previousMillis2 = currentMillis; // save the last time I printed on the serial

Looking at the code above, which we developed by slightly modifying this code, you can see that the command doesn’t stop the total functioning of the Arduino. Therefore, both tasks can occur on their own schedules without interruption.

How to Use Arduino Millis 

The delay() function is a great starting point, and you can use it to accomplish many simple tasks. It’s one of the first commands you’re likely to learn, but as you move forward and develop more complicated sketches, you should get familiar with the millis() command, which lets you run more complicated sketches with more tasks without the stops that delay() causes. For more detailed explanations of these commands and their differences, take a look at this post on the Arduino Forum or the tutorial by Norwegian Creations.


Related news articles

Latest News

Sorry, your filter selection returned no results.

We've updated our privacy policy. Please take a moment to review these changes. By clicking I Agree to Arrow Electronics Terms Of Use  and have read and understand the Privacy Policy and Cookie Policy.

Our website places cookies on your device to improve your experience and to improve our site. Read more about the cookies we use and how to disable them here. Cookies and tracking technologies may be used for marketing purposes.
By clicking “Accept”, you are consenting to placement of cookies on your device and to our use of tracking technologies. Click “Read More” below for more information and instructions on how to disable cookies and tracking technologies. While acceptance of cookies and tracking technologies is voluntary, disabling them may result in the website not working properly, and certain advertisements may be less relevant to you.
We respect your privacy. Read our privacy policy here