Arduino Timing Basics: millis, delay, and delayMicroseconds
Arduino Timing Basics: millis, delay, and delayMicroseconds
Timing is essential in Arduino programming. Many projects require pausing execution or measuring elapsed time. Arduino provides three basic timing commands for these tasks.
millis() — Read Elapsed Time
The millis() function returns how many milliseconds have passed since the board started running. The key advantage is that your code keeps executing while timing is tracked, rather than freezing everything.
The counter rolls over back to zero after approximately 49 days 9 hours (49.7 days). Programs running longer than this need to handle the rollover condition.
Syntax
unsigned long elapsedTime = millis();
The return variable must be unsigned long because the time value exceeds normal int range.
millis() Example Code
unsigned long time;
void setup() {
Serial.begin(9600);
}
void loop() {
time = millis();
Serial.print("time: ");
Serial.println(time);
delay(1000); // Wait 1 second between readings
}
Upload this code and open the Serial Monitor — you’ll see the time value incrementing every 1 second while your code remains responsive.
delay() — Pause for Specified Duration
The delay() function pauses program execution for the specified time in milliseconds. It’s commonly used when waiting for sensor readings or signals to stabilize.
Syntax
delay(1000); // Pause for 1 second
Accepts values from 1 to 4,294,967,295 ms.
Setting Delay Values Above 32767
Arduino’s C language treats numbers as int by default, which maxes out at 32,767. To set longer delays, you must tell the compiler to use unsigned long by appending UL to the number.
delay(60000UL); // Pause for 60 seconds (60000ms)
delay() Example Code
unsigned long time;
void setup() {
Serial.begin(9600);
}
void loop() {
time = millis();
Serial.print("time: ");
Serial.println(time);
delay(60000UL); // Wait 60 seconds
}
Upload this code to your board — the time value updates every 60 seconds.
delayMicroseconds() — Ultra-Short Delays
For microsecond-level delays (μs), use delayMicroseconds(). One microsecond equals 1/1,000,000 of a second. This is useful for high-precision tasks like infrared signal transmission or servo motor control.
Maximum supported value is 16,383 microseconds.
Syntax
delayMicroseconds(1000); // Pause for 1000 microseconds (1 millisecond)
delayMicroseconds() Example Code
unsigned long time;
void setup() {
Serial.begin(9600);
}
void loop() {
time = millis();
Serial.print("time: ");
Serial.println(time);
delayMicroseconds(1000); // Pause for 1000 microseconds
}
The Serial Monitor values will update very rapidly because the delay is too short to observe visually.
Summary: Choosing the Right Timing Command
| Command | Unit | Use When |
|---|---|---|
millis() | Milliseconds (ms) | You need to track time without stopping code |
delay() | Milliseconds (ms) | You need to pause and wait for something |
delayMicroseconds() | Microseconds (μs) | You need very short, precise delays |
If your project needs to do multiple things simultaneously, use millis() with conditional checks instead of delay(), which stops everything during the wait.
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย