Lesson 15 - Using delay() for Basic Timing
Learn how delay() controls simple timing in Arduino sketches and when it is useful for beginner projects.
Progress indicator
Lesson 15 of 15
Learning Objectives
- Understand what delay() does in Arduino timing.
- Read and write delay() syntax correctly.
- Convert milliseconds into seconds for timing decisions.
- Use delay() for simple LED blink patterns.
- Recognize blocking behavior and basic limitations of delay().
- Know when to keep delay() and when to switch to millis().
Concept Explanation
What is delay()
delay() pauses your program for a specific amount of time. During this pause, the program waits before running the next line.
It is one of the easiest ways for beginners to control timing in Arduino and ESP32 sketches.
delay() is often your first timing tool because it is predictable and easy to read in code, especially in LED blink exercises.
delay() Syntax
delay(milliseconds);The value inside delay() is in milliseconds (ms).
You can also use variables: int waitMs = 250; then delay(waitMs);
How delay() Works
When the program reaches delay(), it stops executing the next lines until that delay time is over.
After waiting, execution continues from the next line.
Internally, your code is paused during this wait. The loop does not move to the next statement until the delay ends.
Milliseconds vs Seconds
1000 ms = 1 second
500 ms = 0.5 second
Convert seconds to milliseconds by multiplying by 1000.
Example: 2.5 seconds = 2500 ms.
Blocking Behavior of delay()
While waiting in delay(), your code cannot do other tasks.
This is called blocking behavior and is important to understand early.
If a button is pressed during a long delay, your code may react late because the read logic has to wait.
Simple LED Blink Timing
The most common beginner timing pattern is: LED ON - wait - LED OFF - wait - repeat.
Changing delay values changes blink speed immediately.
When to Use delay()
- First LED blink experiments
- Very simple one-task sketches
- Basic timing demonstrations for beginners
- Quick hardware tests and proof-of-concept code
Real-Life Example
A warning LED that blinks every second is a classic delay()-based pattern. It is simple and clear when no other time-critical tasks are needed.
Example Code
This example blinks an LED with two different delay values.
const int ledPin = 2;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(1000); // wait 1 second
digitalWrite(ledPin, LOW);
delay(500); // wait 0.5 second
}What this example teaches
- How to set up a basic output timing loop
- How different delays create different ON/OFF durations
- How loop() repeats timing forever
- How timing readability improves with comments
Example Code Explanation
ledPinstores the output pin used for the LED.pinMode(ledPin, OUTPUT)prepares that pin as an output.digitalWrite(ledPin, HIGH)turns the LED ON.delay(1000)keeps it ON for 1 second.digitalWrite(ledPin, LOW)turns the LED OFF.delay(500)keeps it OFF for 0.5 second before loop repeats.
Beginner reading tip
Track one cycle only: ON phase + OFF phase. Then remember that loop()repeats that cycle forever.
Limitations of delay()
- It blocks the program, so other logic must wait.
- Button reads and sensor updates can feel slow if delays are long.
- Complex multi-task behavior is hard with heavy delay use.
- Long delays can make UI behavior feel frozen to users.
Common Mistakes with delay()
- Using seconds directly instead of milliseconds (for example writing
delay(1)expecting 1 second). - Adding long delays and then wondering why input response is slow.
- Forgetting that
loop()repeats forever, so delays repeat too. - Stacking many delays and losing track of total cycle time.
Best Practices for delay()
- Start with clear values like
delay(1000)anddelay(500). - Use comments to label delay durations in seconds.
- Keep delays short when your program also reads buttons or sensors.
- Move to
millis()timing when your project needs multiple actions at once. - Calculate and document total loop duration (ON delay + OFF delay).
Practice Task
- Change the ON delay to
200and OFF delay to800. Describe how blink behavior changes. - Make a symmetric blink using equal delays and explain total cycle time.
- Add one more LED phase and compute the full loop timing in milliseconds.
- Explain one reason why this delay()-based approach can be a limitation in larger projects.
Try it now
Open the simulator and test how changing delay values affects blink timing.