Bird
Raised Fist0
Arduinoprogramming~20 mins

delay() function behavior 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
🎖️
Delay Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output timing behavior of delay()?
Consider this Arduino code snippet. What will be the timing behavior of the LED blinking?
Arduino
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}
AThe LED turns on for 1 second, then off for 1 second, repeatedly.
BThe LED blinks very fast without visible delay.
CThe LED stays off permanently without turning on.
DThe LED stays on permanently without turning off.
Attempts:
2 left
💡 Hint
Think about what delay(1000) means in milliseconds.
Predict Output
intermediate
2:00remaining
What happens if delay(0) is used?
What is the effect of calling delay(0) in an Arduino sketch?
Arduino
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(0);
  digitalWrite(LED_BUILTIN, LOW);
  delay(0);
}
AThe LED toggles on and off as fast as the processor can run the loop.
BThe LED stays on permanently.
CThe LED stays off permanently.
DThe code causes a compile error because delay(0) is invalid.
Attempts:
2 left
💡 Hint
Think about what a zero millisecond delay means.
Predict Output
advanced
2:00remaining
What is the output of this code with nested delays?
Analyze the timing of the LED blinking in this code:
Arduino
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}
ALED on for 500 ms, off for 500 ms, repeatedly.
BLED on for 500 ms, off for 1.5 seconds, repeatedly.
CLED on for 1 second, off for 1 second, repeatedly.
DLED on for 1.5 seconds, off for 1 second, repeatedly.
Attempts:
2 left
💡 Hint
Add the delays when the LED is on.
Predict Output
advanced
2:00remaining
What error occurs if delay() is called with a negative value?
What happens if you write delay(-100) in an Arduino sketch?
Arduino
void loop() {
  delay(-100);
}
AThe code causes a compile-time error due to invalid argument type.
BThe code compiles and causes delay() to wait a very long time.
CThe code compiles but causes a runtime error and resets the board.
DThe code compiles but delay() treats negative as zero and no delay occurs.
Attempts:
2 left
💡 Hint
Check how delay() handles unsigned integers internally.
🧠 Conceptual
expert
3:00remaining
Why does delay() block other code execution?
Explain why using delay() in Arduino sketches can cause problems with multitasking or reading sensors frequently.
ABecause delay() uses interrupts that interfere with sensor readings.
BBecause delay() only pauses the LED pin but not other code.
CBecause delay() speeds up the CPU causing timing issues.
DBecause delay() stops the CPU from running any other code during the wait time.
Attempts:
2 left
💡 Hint
Think about what happens to the program flow during delay().

Practice

(1/5)
1. What does the delay(1000); function do in an Arduino program?
easy
A. Pauses the program for 1000 milliseconds (1 second)
B. Stops the program permanently
C. Speeds up the program by 1000 times
D. Restarts the Arduino board

Solution

  1. Step 1: Understand the delay() parameter

    The number inside delay() is the time in milliseconds to pause the program.
  2. Step 2: Interpret delay(1000)

    1000 milliseconds equals 1 second, so the program pauses for 1 second.
  3. Final Answer:

    Pauses the program for 1000 milliseconds (1 second) -> Option A
  4. Quick Check:

    delay(1000) = 1 second pause [OK]
Hint: delay(x) pauses for x milliseconds, 1000ms = 1 second [OK]
Common Mistakes:
  • Thinking delay stops the program forever
  • Confusing milliseconds with seconds
  • Assuming delay speeds up the program
2. Which of the following is the correct syntax to pause an Arduino program for half a second?
easy
A. delay(0.5);
B. delay = 500;
C. delay(500s);
D. delay(500);

Solution

  1. Step 1: Check correct function usage

    delay() takes an integer number of milliseconds inside parentheses.
  2. Step 2: Validate each option

    A uses delay(0.5); which is a float and incorrect. B assigns delay which is invalid. C uses delay(500); which is correct for 500 milliseconds. D uses '500s' which is invalid syntax.
  3. Final Answer:

    delay(500); -> Option D
  4. Quick Check:

    delay(500) is correct syntax [OK]
Hint: Use delay(milliseconds); with integer inside parentheses [OK]
Common Mistakes:
  • Using decimal numbers instead of integers
  • Assigning delay like a variable
  • Adding units like 's' inside delay()
3. What will be the output on the Serial Monitor after running this Arduino code?
void setup() {
  Serial.begin(9600);
  Serial.println("Start");
  delay(2000);
  Serial.println("End");
}
void loop() {}
medium
A. Start and End printed immediately together
B. Only Start is printed, End never prints
C. Start immediately, then End after 2 seconds
D. No output because delay stops Serial

Solution

  1. Step 1: Analyze Serial prints and delay

    "Start" prints immediately, then delay(2000) pauses 2 seconds before next line.
  2. Step 2: Understand delay effect on output

    After 2 seconds pause, "End" prints. Both lines appear but with 2 seconds gap.
  3. Final Answer:

    Start immediately, then End after 2 seconds -> Option C
  4. Quick Check:

    delay pauses program, output delayed [OK]
Hint: delay pauses code, so output after delay appears later [OK]
Common Mistakes:
  • Thinking delay stops Serial output completely
  • Assuming both prints happen instantly
  • Believing delay affects only loop(), not setup()
4. Identify the problem in this Arduino code snippet:
void setup() {
  pinMode(13, OUTPUT);
}
void loop() {
  digitalWrite(13, HIGH);
  delay(1000)
  digitalWrite(13, LOW);
  delay(1000);
}
medium
A. pinMode should be in loop()
B. Missing semicolon after delay(1000)
C. digitalWrite needs delay before it
D. delay() cannot be used in loop()

Solution

  1. Step 1: Check syntax line by line

    delay(1000) is missing a semicolon at the end, causing a syntax error.
  2. Step 2: Validate other statements

    pinMode is correctly in setup(), digitalWrite and delay usage is correct except missing semicolon.
  3. Final Answer:

    Missing semicolon after delay(1000) -> Option B
  4. Quick Check:

    Every statement must end with ; [OK]
Hint: Check for missing semicolons after delay() calls [OK]
Common Mistakes:
  • Putting pinMode inside loop unnecessarily
  • Thinking delay() can't be in loop
  • Forgetting semicolons after statements
5. You want to blink an LED connected to pin 9 exactly 3 times with 0.5 second ON and 0.5 second OFF, then stop. Which code snippet correctly uses delay() to do this?
hard
A. for(int i=0; i<3; i++) { digitalWrite(9, HIGH); delay(500); digitalWrite(9, LOW); delay(500); }
B. while(true) { digitalWrite(9, HIGH); delay(500); digitalWrite(9, LOW); delay(500); }
C. digitalWrite(9, HIGH); delay(1500); digitalWrite(9, LOW); delay(1500);
D. for(int i=0; i<=3; i++) { digitalWrite(9, HIGH); delay(1000); digitalWrite(9, LOW); delay(1000); }

Solution

  1. Step 1: Understand blinking requirements

    LED must turn ON and OFF 3 times, each ON and OFF lasting 0.5 seconds (500 ms).
  2. Step 2: Analyze each option

    A uses a for loop 3 times with 500ms delay ON and OFF, matching requirements. B loops forever, no stop. C delays 1500ms which is too long and only blinks once. D loops 4 times (i<=3) with 1000ms delays, wrong timing and count.
  3. Final Answer:

    for(int i=0; i<3; i++) { digitalWrite(9, HIGH); delay(500); digitalWrite(9, LOW); delay(500); } -> Option A
  4. Quick Check:

    Loop 3 times with 500ms delay ON/OFF [OK]
Hint: Use for loop with delay(500) ON and OFF, repeat 3 times [OK]
Common Mistakes:
  • Using infinite loops instead of fixed count
  • Wrong delay times for ON/OFF
  • Looping one extra time with <= instead of <