กลับหน้าหลัก
views
Arduino Timing Basics: millis, delay, and delayMicroseconds
Last updated on

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.

Serial Monitor showing millis() values incrementing by 1000ms every second

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.

Diagram illustrating how delay() freezes code execution while waiting

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.

Comparison table of time units for millis, delay, and delayMicroseconds

Summary: Choosing the Right Timing Command

CommandUnitUse 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

จ้างทำโปรเจคเลย

ประเมินราคาอัตโนมัติ + Reference Code

ขอให้ AI ประเมินราคาโปรเจคนี้

กรอกข้อมูลให้ครบ ระบบจะสร้างรหัสอ้างอิงและประเมินราคา/ระยะเวลาคร่าว ๆ จากรายละเอียดงาน แล้วให้กด Add LINE พร้อมพิมพ์รหัสนี้เพื่อคุยต่อ

คำถามให้ AI ประเมินแม่นขึ้น

หลังส่งฟอร์ม ระบบจะโชว์ Reference Code ให้ copy แล้วกด Add LINE เพื่อคุยต่อ ข้อมูลส่วนตัวจะไม่ถูกส่งเข้า GA4

ความคิดเห็น

รีวิวจากคนใช้งานจริง

รีวิวจากลูกค้าและคนที่เคยใช้งาน

ถ้าเคยสั่งงาน เคยอ่านหน้านี้แล้วได้ประโยชน์ หรือมีข้อเสนอแนะ ฝากรีวิวไว้ได้เลย

กำลังโหลดรีวิว...