Timing control helps your Arduino do things at the right moment or for the right amount of time. It makes your projects work smoothly and correctly.
Why timing control is needed in Arduino
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Arduino
delay(milliseconds); // or using millis() for non-blocking timing unsigned long currentTime = millis(); if (currentTime - previousTime >= interval) { // do something previousTime = currentTime; }
delay() pauses the whole program for the given milliseconds.
millis() returns the time since the Arduino started, useful for timing without stopping the program.
Examples
Arduino
delay(1000); // pauses for 1 second
Arduino
unsigned long previousTime = 0; unsigned long interval = 2000; void loop() { unsigned long currentTime = millis(); if (currentTime - previousTime >= interval) { // action here previousTime = currentTime; } }
Sample Program
This program blinks the built-in LED on pin 13 every second using timing control with millis(). It does not stop the program while waiting.
Arduino
const int ledPin = 13; unsigned long previousTime = 0; const long interval = 1000; // 1 second void setup() { pinMode(ledPin, OUTPUT); } void loop() { unsigned long currentTime = millis(); if (currentTime - previousTime >= interval) { previousTime = currentTime; digitalWrite(ledPin, !digitalRead(ledPin)); // toggle LED } }
Important Notes
Using delay() stops everything, so your Arduino can't do other tasks during that time.
Using millis() lets your Arduino keep working and check time in the background.
Summary
Timing control helps your Arduino do things at the right time.
delay() is simple but stops the program.
millis() allows multitasking without stopping.
Practice
1. Why do we need timing control in Arduino programs?
easy
Solution
Step 1: Understand the purpose of timing control
Timing control allows the Arduino to perform tasks at specific times or intervals.Step 2: Identify the correct reason for timing control
It helps in making sure actions like blinking LEDs or reading sensors happen when needed.Final Answer:
To make sure actions happen at the right time -> Option AQuick Check:
Timing control = right time actions [OK]
Hint: Timing control means doing things at the right moment [OK]
Common Mistakes:
- Thinking timing control speeds up the processor
- Confusing timing control with internet connection
- Believing timing control changes hardware color
2. Which Arduino function is used to pause the program for a specific time?
easy
Solution
Step 1: Recall Arduino functions for timing
The delay() function pauses the program for a set number of milliseconds.Step 2: Match function to description
delay() is the only function among options that pauses execution.Final Answer:
delay() -> Option DQuick Check:
Pause program = delay() [OK]
Hint: delay() pauses program; others control pins or read values [OK]
Common Mistakes:
- Using digitalWrite() to pause program
- Confusing pinMode() with timing control
- Thinking analogRead() pauses execution
3. What will the following Arduino code do?
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}medium
Solution
Step 1: Analyze the loop code
The code turns pin 13 HIGH (LED on), waits 1000 ms (1 second), then LOW (LED off), waits 1000 ms again.Step 2: Understand the effect on LED
This causes the LED to blink on and off every second.Final Answer:
Turn LED on pin 13 on and off every second -> Option AQuick Check:
delay(1000) = 1 second blink [OK]
Hint: delay(1000) means 1 second pause, blinking LED [OK]
Common Mistakes:
- Thinking delay(1000) is 100 milliseconds
- Assuming LED stays always on or off
- Ignoring the delay between on and off
4. Identify the problem in this Arduino code for blinking an LED:
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, HIGH);
delay(1000);
}medium
Solution
Step 1: Check LED on/off commands
The code sets pin 13 HIGH twice but never sets it LOW, so LED stays on.Step 2: Identify missing part for blinking
To blink, the LED must be turned off with digitalWrite(13, LOW) between delays.Final Answer:
LED never turns off because digitalWrite(13, LOW) is missing -> Option BQuick Check:
Missing LOW command = LED stays on [OK]
Hint: Blink needs both HIGH and LOW commands [OK]
Common Mistakes:
- Thinking delay() is wrong here
- Moving pinMode() inside loop() unnecessarily
- Changing pin number without reason
5. You want to read a sensor every 500 milliseconds without stopping other tasks. Which timing method should you use?
hard
Solution
Step 1: Understand delay() effect
delay(500) pauses the whole program, stopping other tasks temporarily.Step 2: Use millis() for non-blocking timing
millis() lets you check time passed without stopping the program, so other tasks run smoothly.Final Answer:
Use millis() to check elapsed time and read sensor when 500 ms passed -> Option CQuick Check:
Non-blocking timing = millis() [OK]
Hint: millis() checks time without stopping program [OK]
Common Mistakes:
- Using delay() and freezing program
- Confusing digitalWrite() with timing control
- Resetting pinMode() repeatedly
