Arduino Delay Commands: millis(), delay(), delayMicroseconds()
Arduino Delay Commands: millis(), delay(), delayMicroseconds()
When writing Arduino code, you sometimes need the program to wait before continuing. Maybe you’re waiting for a sensor to stabilize or for data to finish transmitting.
Arduino provides 3 main delay commands, each suited for different situations. Let’s go through them one by one.
1. millis() — Read Elapsed Time
millis() returns the number of milliseconds since the program started running. It does not pause the program — it just reads the current elapsed time.
The key limitation is that the counter overflows back to 0 after about 49 days and 9 hours.
Syntax
unsigned long time = millis();
- The variable must be type unsigned long because the time value is too large for a standard int
Example Code
unsigned long time;
void setup() {
Serial.begin(9600);
}
void loop() {
time = millis();
Serial.print("time: ");
Serial.println(time);
delay(1000);
}
Upload this to your Arduino board and open the Serial Monitor. You’ll see the elapsed time increment every second, and the program keeps running continuously.
2. delay() — Pause for a Specified Duration
delay() pauses execution until the specified time has passed, then continues with the next line of code. The unit is milliseconds (ms).
- Maximum value: 4,294,967,295 ms (about 49.7 days)
- 1 ms = 1/1000 second
Syntax
delay(Var); // Var = delay time in milliseconds
Important: Use UL for Long Delays
If you need a delay longer than 32,767 ms, you must append UL to the number. This is because default numeric literals in Arduino C are integer type, which cannot hold values above that threshold.
delay(60000UL); // 60 second delay
Example Code
unsigned long time;
void setup() {
Serial.begin(9600);
}
void loop() {
time = millis();
Serial.print("time: ");
Serial.println(time);
delay(60000UL); // 60000 ms = 60 seconds
}
When uploaded, Serial Monitor will print a new time value every 60 seconds because delay() blocks all other code during the wait.
3. delayMicroseconds() — Very Short Delays
delayMicroseconds() pauses execution in microseconds (µs), which equals 1/1,000,000 of a second.
- Best for short non-blocking timing tasks
- Maximum value: 16,383 µs
Syntax
delayMicroseconds(us); // us = delay time in microseconds
Example Code
unsigned long time;
void setup() {
Serial.begin(9600);
}
void loop() {
time = millis();
Serial.print("time: ");
Serial.println(time);
delayMicroseconds(1000); // 1000 µs delay
}
With delayMicroseconds(1000), the delay is only 1/1000 of a second. The values on Serial Monitor update so fast they appear nearly instantaneous.
Quick Comparison
| Command | Unit | Blocks Code | Maximum Value |
|---|---|---|---|
| millis() | ms | No | 49 days 9 hours |
| delay() | ms | Yes | 4,294,967,295 ms |
| delayMicroseconds() | µs | Yes | 16,383 µs |
Summary
- Use millis() when you want the program to keep running and just check how long it’s been
- Use delay() for pausing in seconds or minutes, adding UL for anything over 32767 ms
- Use delayMicroseconds() for microsecond-level short delays
Pick the right command for the job and your code will behave correctly for real-world use.
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย