Bird
Raised Fist0
Arduinoprogramming~20 mins

micros() for microsecond precision in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Microsecond Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of micros() after delay

What will be the approximate output of the micros() function after a delay(1) call?

Arduino
unsigned long start = micros();
delay(1);
unsigned long end = micros();
Serial.println(end - start);
AApproximately 1000000
BApproximately 1
CApproximately 0
DApproximately 1000
Attempts:
2 left
💡 Hint

Remember, delay(1) pauses for 1 millisecond, and micros() counts microseconds.

🧠 Conceptual
intermediate
1:30remaining
Understanding micros() overflow behavior

What happens to the value returned by micros() when it reaches its maximum value?

AIt resets to zero and continues counting
BIt starts counting backwards
CIt throws an error and stops the program
DIt stops increasing and stays at the max value
Attempts:
2 left
💡 Hint

Think about how unsigned integers behave in Arduino.

🔧 Debug
advanced
2:30remaining
Why does this timing code give wrong results?

Consider this code snippet:

unsigned long start = micros();
// some code
unsigned long duration = micros() - start;
if (duration < 1000) {
  Serial.println("Fast");
} else {
  Serial.println("Slow");
}

Sometimes it prints "Slow" even when the code runs quickly. Why?

ABecause <code>micros()</code> can overflow and cause negative duration
BBecause <code>duration</code> is unsigned and subtraction handles overflow correctly
CBecause the code inside the timing block is actually slow
DBecause <code>micros()</code> returns milliseconds, not microseconds
Attempts:
2 left
💡 Hint

Think about how unsigned subtraction works in Arduino.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in micros() usage

Which option contains a syntax error when using micros() to measure elapsed time?

A
unsigned long start = micros()
// code
unsigned long elapsed = micros() - start;
B
unsigned long start = micros();
// code
unsigned long elapsed = micros() + start;
C
unsigned long start = micros();
// code
unsigned long elapsed = start - micros();
D
unsigned long start = micros();
// code
unsigned long elapsed = micros() - start;
Attempts:
2 left
💡 Hint

Look carefully for missing punctuation.

🚀 Application
expert
3:00remaining
Calculate frequency using micros()

You want to measure how many times a button is pressed per second using micros(). Which code snippet correctly calculates the frequency?

A
unsigned long lastPress = 0;
unsigned int count = 0;
void loop() {
  if (buttonPressed()) {
    count++;
    if (micros() - lastPress &gt; 1000000) {
      Serial.println(count);
      count = 0;
      lastPress = micros();
    }
  }
}
B
unsigned long lastPress = 0;
unsigned int count = 0;
void loop() {
  if (buttonPressed()) {
    unsigned long now = micros();
    if (now - lastPress &gt;= 1000000) {
      Serial.println(count);
      count = 0;
      lastPress = now;
    }
    count++;
  }
}
C
unsigned long lastPress = 0;
unsigned int count = 0;
void loop() {
  if (buttonPressed()) {
    count++;
    if (micros() - lastPress &gt;= 1000000) {
      Serial.println(count);
      count = 0;
      lastPress = micros();
    }
  }
}
D
unsigned long lastPress = 0;
unsigned int count = 0;
void loop() {
  if (buttonPressed()) {
    unsigned long now = micros();
    if (now - lastPress &gt; 1000000) {
      Serial.println(count);
      count = 0;
      lastPress = now;
    }
    count++;
  }
}
Attempts:
2 left
💡 Hint

Think about when to update the count and the lastPress time.

Practice

(1/5)
1. What does the Arduino function micros() return?
easy
A. The number of microseconds since the program started
B. The number of milliseconds since the program started
C. The current time in seconds
D. The number of seconds since the last reset

Solution

  1. Step 1: Understand the purpose of micros()

    The micros() function returns the time in microseconds since the Arduino program began running.
  2. 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.
  3. Final Answer:

    The number of microseconds since the program started -> Option A
  4. Quick Check:

    micros() = microseconds since start [OK]
Hint: Remember micros() counts microseconds from program start [OK]
Common Mistakes:
  • Confusing micros() with millis()
  • Thinking it returns seconds
  • Assuming it resets every second
2. Which of the following is the correct way to store the current microsecond count in a variable?
easy
A. unsigned long time = micros();
B. int time = micros();
C. float time = micros();
D. long time = micros();

Solution

  1. Step 1: Identify the data type returned by micros()

    The micros() function returns an unsigned long integer representing microseconds.
  2. Step 2: Match the correct variable type to store the value

    Only unsigned long can hold the large values from micros() without overflow or sign issues.
  3. Final Answer:

    unsigned long time = micros(); -> Option A
  4. Quick Check:

    Use unsigned long for micros() values [OK]
Hint: Use unsigned long to store micros() values safely [OK]
Common Mistakes:
  • Using int which is too small
  • Using float which loses precision
  • Using signed long which can cause negative values
3. What will be the output of this Arduino code snippet?
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.
medium
A. Always zero
B. A number close to 500000
C. A negative number
D. A number close to 500

Solution

  1. Step 1: Understand the timing measurement

    The code measures the time difference in microseconds between two calls to micros().
  2. Step 2: Interpret the delay and difference calculation

    If the delay is about 500 microseconds, the difference diff will be close to 500, printed as a positive number.
  3. Final Answer:

    A number close to 500 -> Option D
  4. Quick Check:

    diff = end - start ≈ 500 [OK]
Hint: Subtract micros() values to get elapsed microseconds [OK]
Common Mistakes:
  • Expecting milliseconds instead of microseconds
  • Thinking difference can be negative
  • Confusing delay units
4. Identify the error in this Arduino code snippet:
unsigned long start = micros();
// some code
unsigned long end = micros();
int elapsed = end - start;
Serial.println(elapsed);
medium
A. micros() cannot be assigned to unsigned long
B. Using int for elapsed can cause overflow
C. Serial.println cannot print integers
D. Subtracting micros() values is invalid

Solution

  1. Step 1: Check variable types for time difference

    The difference between two micros() values can be very large, exceeding the range of int.
  2. Step 2: Understand overflow risk

    Using int (usually 16-bit) can cause overflow and incorrect negative values. It should be unsigned long.
  3. Final Answer:

    Using int for elapsed can cause overflow -> Option B
  4. Quick Check:

    Use unsigned long for elapsed time to avoid overflow [OK]
Hint: Use unsigned long, not int, for time differences [OK]
Common Mistakes:
  • Using int instead of unsigned long
  • Thinking micros() returns signed values
  • Assuming Serial.println can't print integers
5. You want to measure how long a button is pressed in microseconds using micros(). Which approach correctly handles the timing even if the program runs longer than 70 minutes (when micros() overflows)?
hard
A. Ignore overflow because it never affects timing
B. Reset the Arduino every 60 minutes to avoid overflow
C. Store start time, then calculate elapsed as micros() - start using unsigned long subtraction
D. Use millis() instead because it never overflows

Solution

  1. Step 1: Understand micros() overflow behavior

    micros() overflows roughly every 70 minutes, wrapping back to zero.
  2. Step 2: Use unsigned long subtraction to handle overflow

    Unsigned subtraction correctly calculates elapsed time even if overflow happens, so micros() - start works safely.
  3. Final Answer:

    Store start time, then calculate elapsed as micros() - start using unsigned long subtraction -> Option C
  4. Quick Check:

    Unsigned subtraction handles micros() overflow correctly [OK]
Hint: Use unsigned subtraction to handle micros() overflow safely [OK]
Common Mistakes:
  • Thinking micros() never overflows
  • Using millis() which has lower precision
  • Resetting Arduino unnecessarily