Returns the number of milliseconds passed since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.


time_ms = millis()

Parameter Values

  • None

Return Values

  • Number of milliseconds passed since the program started. Data type: unsigned long.

Example Code

Example 1

This example code prints on the serial port the number of milliseconds passed since the Arduino board started running the code itself.

unsigned long time_ms; void setup() { Serial.begin(9600); } void loop() { Serial.print("Time: "); time_ms = millis(); Serial.println(time_ms); // prints time since program started delay(1000); // wait a second so as not to send massive amounts of data }

The result on Serial Monitor:

Time: 0 Time: 999 Time: 1999 Time: 3000 Time: 4000 Time: 5000 Time: 6000 Time: 7001 Time: 8001 Time: 9001
Autoscroll Show timestamp
Clear output
9600 baud  

Example 2

Print a text one time per second without blocking other codes

const unsigned long TIME_INTERVAL = 1000; unsigned long previousMillis; void setup() { Serial.begin(9600); previousMillis = millis(); } void loop() { if (millis() - previousMillis >= TIME_INTERVAL) { previousMillis = millis(); Serial.println("Arduino References"); } }


  • Please note that the return value for millis() is of type unsigned long, logic errors may occur if a programmer tries to do arithmetic with smaller data types such as int. Even signed long may encounter errors as its maximum value is half that of its unsigned counterpart.
  • The return value of millis() function rolls over back to zero after roughly 50 days. If the sketch is intended to run for longer than that, It needs to make sure the rollover does not make the sketch fail. To solve it, write rollover-safe code. Let's compare the two following inequations:
    • millis() >= (previousMillis + TIME_INTERVAL)
    • (millis() - previousMillis) >= TIME_INTERVAL
  • Mathematically, they are equivalent to each other. However, in programming, they are not. That is because the size of storage is unlimited in mathematics while it is limited to 4 bytes in Arduino programming. In programming, when the rollover happens, the first inequation makes the sketch fail while the second inequation does NOT. Therefore:
    • Do NOT use if (millis() >= (previousMillis + TIME_INTERVAL)),
    • Use if(millis() - previousMillis >= TIME_INTERVAL) instead

See Also


Arduino UNO R3
Arduino Starter Kit
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.