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.
Turning an LED on and off every second to make it blink.
Reading a sensor value every 5 seconds to check temperature.
Making a motor run for exactly 10 seconds before stopping.
Creating a delay between button presses to avoid mistakes.
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
This stops the program for 1 second before continuing.
Arduino
delay(1000); // pauses for1 second
This checks every loop if 2 seconds have passed, then does something without stopping the whole program.
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
}
}
OutputSuccess
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/5)
1. Why do we need timing control in Arduino programs?
easy
A. To make sure actions happen at the right time
B. To increase the speed of the Arduino processor
C. To change the color of the Arduino board
D. To connect the Arduino to the internet
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 A
Quick 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
A. digitalWrite()
B. analogRead()
C. pinMode()
D. delay()
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 D
Quick Check:
Pause program = delay() [OK]
Hint: delay() pauses program; others control pins or read values [OK]