The micros() function helps you measure time very precisely in microseconds. This is useful when you want to know exactly how long something takes or control timing closely.
micros() for microsecond precision in Arduino
Start learning this pattern below
Jump into concepts and practice - no test required
unsigned long micros();
micros() returns the number of microseconds since the Arduino board began running the current program.
The value will overflow (go back to zero) after about 70 minutes.
time.unsigned long time = micros();
unsigned long start = micros(); // do something unsigned long duration = micros() - start;
This program measures how many microseconds pass during a 10 millisecond delay and prints the result. It repeats every second.
void setup() {
Serial.begin(9600);
}
void loop() {
unsigned long startTime = micros();
delay(10); // wait 10 milliseconds
unsigned long endTime = micros();
unsigned long elapsed = endTime - startTime;
Serial.print("Elapsed time in microseconds: ");
Serial.println(elapsed);
delay(1000); // wait 1 second before repeating
}The micros() function counts microseconds but is not perfectly precise for very short times under 4 microseconds.
Because micros() returns an unsigned long, it can hold large numbers but will reset after about 70 minutes.
micros() gives you time in microseconds since the program started.
Use it to measure short time intervals or control precise timing.
Remember it resets after about 70 minutes, so handle long-running programs carefully.
Practice
micros() return?Solution
Step 1: Understand the purpose of
Themicros()micros()function returns the time in microseconds since the Arduino program began running.Step 2: Compare options with the function's behavior
Only The number of microseconds since the program started correctly states it returns microseconds since start. Others mention milliseconds or seconds, which are incorrect.Final Answer:
The number of microseconds since the program started -> Option AQuick Check:
micros()= microseconds since start [OK]
- Confusing micros() with millis()
- Thinking it returns seconds
- Assuming it resets every second
Solution
Step 1: Identify the data type returned by
Themicros()micros()function returns an unsigned long integer representing microseconds.Step 2: Match the correct variable type to store the value
Onlyunsigned longcan hold the large values frommicros()without overflow or sign issues.Final Answer:
unsigned long time = micros(); -> Option AQuick Check:
Use unsigned long for micros() values [OK]
- Using int which is too small
- Using float which loses precision
- Using signed long which can cause negative values
unsigned long start = micros(); // some delay here unsigned long end = micros(); unsigned long diff = end - start; Serial.println(diff);Assuming the delay is about 500 microseconds.
Solution
Step 1: Understand the timing measurement
The code measures the time difference in microseconds between two calls tomicros().Step 2: Interpret the delay and difference calculation
If the delay is about 500 microseconds, the differencediffwill be close to 500, printed as a positive number.Final Answer:
A number close to 500 -> Option DQuick Check:
diff = end - start ≈ 500 [OK]
- Expecting milliseconds instead of microseconds
- Thinking difference can be negative
- Confusing delay units
unsigned long start = micros(); // some code unsigned long end = micros(); int elapsed = end - start; Serial.println(elapsed);
Solution
Step 1: Check variable types for time difference
The difference between twomicros()values can be very large, exceeding the range ofint.Step 2: Understand overflow risk
Usingint(usually 16-bit) can cause overflow and incorrect negative values. It should beunsigned long.Final Answer:
Using int for elapsed can cause overflow -> Option BQuick Check:
Use unsigned long for elapsed time to avoid overflow [OK]
- Using int instead of unsigned long
- Thinking micros() returns signed values
- Assuming Serial.println can't print integers
micros(). Which approach correctly handles the timing even if the program runs longer than 70 minutes (when micros() overflows)?Solution
Step 1: Understand micros() overflow behavior
micros()overflows roughly every 70 minutes, wrapping back to zero.Step 2: Use unsigned long subtraction to handle overflow
Unsigned subtraction correctly calculates elapsed time even if overflow happens, somicros() - startworks safely.Final Answer:
Store start time, then calculate elapsed asmicros() - startusing unsigned long subtraction -> Option CQuick Check:
Unsigned subtraction handles micros() overflow correctly [OK]
- Thinking micros() never overflows
- Using millis() which has lower precision
- Resetting Arduino unnecessarily
