Sub topic 5
Making the Blink Visible with `delay()`
Assume you know the basic purpose of an Arduino-compatible board, can read a simple sketch, and have installed the Arduino IDE. This chapter focuses on timing a digital output with delay(). You will use the built-in LED first, so no external wiring is required.
A blink has two actions:
- Set the LED output to
HIGH, which turns it on. - Set the LED output to
LOW, which turns it off.
If the program performs both actions immediately, the LED may change too quickly for your eyes to notice. The delay() function pauses the program for a specified number of milliseconds. A millisecond is one thousandth of a second.
The basic timing pattern
The following sketch turns the board's built-in LED on for one second and off for one second:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}The setup() function runs once after the board starts or resets. It configures the built-in LED pin as an output.
The loop() function runs repeatedly. During each pass, the program:
- Turns the LED on.
- Waits 1,000 milliseconds, or one second.
- Turns the LED off.
- Waits another second.
- Starts the sequence again.
The result is a complete two-second cycle: one second on and one second off.
Choosing a delay
The argument passed to delay() is an unsigned long integer measured in milliseconds. Common values include:
| Desired pause | `delay()` call |
|---|---|
| 100 milliseconds | `delay(100);` |
| Half a second | `delay(500);` |
| Two seconds | `delay(2000);` |
A shorter delay produces a faster blink. A longer delay produces a slower blink. You can use different values for the on and off periods:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(900);
}This creates a brief flash followed by a longer pause. The LED still completes a one-second cycle, but its duty cycle is different. Duty cycle is the fraction of a repeating cycle during which a signal is active. Here, the LED is on for 100 milliseconds out of 1,000, so its duty cycle is 10 percent.
A delay does not just affect the LED. It stops the processor from continuing through loop() while the delay is active.
Understanding what blocking means
Consider this example:
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
// This statement cannot run until the one-second delay ends.
int sensorValue = analogRead(A0);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}The sensor is read only after the LED has remained on for one second. It is then read once, followed by another one-second pause. If the sensor value changes during either delay, the sketch does not react immediately.
For simple demonstrations, blocking is often acceptable because it makes the timing easy to understand. For interactive projects, use a non-blocking technique based on millis(). The millis() function reports how many milliseconds have passed since the board started. Instead of stopping, the sketch checks whether the next LED change is due.
The following version keeps the same visible timing while leaving space for other work:
const uint8_t LED_PIN = LED_BUILTIN;
const unsigned long INTERVAL_MS = 1000;
unsigned long previousToggle = 0;
bool ledOn = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
unsigned long now = millis();
if (now - previousToggle >= INTERVAL_MS) {
previousToggle = now;
ledOn = !ledOn;
digitalWrite(LED_PIN, ledOn ? HIGH : LOW);
}
// Other code can run here on every pass through loop().
}The subtraction form, now - previousToggle, also works when the millis() counter eventually wraps around to zero. That makes it safer than comparing timestamps with now >= previousToggle + INTERVAL_MS.
Exercise
What happens if both delay(1000) calls in the first sketch are changed to delay(100)?
Show answer
The LED turns on for 100 milliseconds and off for 100 milliseconds, producing a faster blink with a one-fifth-second cycle.