Sub topic 9
Opening and Reading the Blink Sketch
Assume you have installed the Arduino IDE and connected a supported board, such as an Arduino Uno. You should also know that a sketch is an Arduino program. This chapter focuses on reading the built-in Blink example before uploading it.
Open the Blink example
In the Arduino IDE, choose File > Examples > 01.Basics > Blink. The IDE opens the example in a new sketch window. The example usually contains this code:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}The sketch has two required functions:
setup()runs once when the board starts or resets.loop()runs repeatedly aftersetup()finishes.
A function is a named group of instructions. The parentheses after setup and loop show that these are functions. Braces mark the beginning and end of each function. A semicolon ends most individual instructions.
Understand the setup function
This line appears inside setup():
pinMode(LED_BUILTIN, OUTPUT);pinMode configures an electrical pin. A pin is a connection that the microcontroller can use to read or produce an electrical signal.
LED_BUILTIN is a constant supplied by the board's software definition. It identifies the pin connected to the board's built-in LED. Using this name is safer than writing a number because different boards may connect the LED to different pins.
OUTPUT tells the microcontroller that the sketch will control the pin. Without this configuration, the LED may not respond as expected.
The setup function runs once, so the pin does not need to be configured again on every loop.
Understand the loop function
The first instruction in loop() is:
digitalWrite(LED_BUILTIN, HIGH);digitalWrite sets a digital output to a binary state. HIGH turns the output on, and LOW turns it off. On most Arduino boards, setting the built-in LED's pin to HIGH lights the LED.
Next, the sketch waits:
delay(1000);delay pauses the program for a number of milliseconds. One thousand milliseconds equals one second. During this pause, the board is not executing the next instruction in this sketch.
The final two instructions turn the LED off and wait again:
digitalWrite(LED_BUILTIN, LOW);
delay(1000);When the second delay ends, the closing brace of loop() is reached. Arduino then starts loop() again. The result is one second on, followed by one second off.
Common reading errors
| Code detail | Common mistake | Correct interpretation |
|---|---|---|
| `LED_BUILTIN` | Replacing it with an unrelated pin number | It names the board's built-in LED pin |
| `HIGH` and `LOW` | Treating them as text strings | They are predefined state values |
| `delay(1000)` | Reading `1000` as seconds | The value is milliseconds, so it means one second |
| `setup()` | Expecting it to repeat | It runs once after startup or reset |
| `loop()` | Expecting it to run only once | Arduino calls it continuously |
A board can run the sketch even if the LED is not visibly blinking. Some boards use an active-low LED, where the electrical meaning of HIGH and LOW appears reversed. The example's behavior is defined for the selected board package, so first check that the correct board is selected before changing the code.
Exercise
How long is the built-in LED on during one complete Blink cycle, and which instructions control that time?
Show answer
The LED is on for about one second. digitalWrite(LED_BUILTIN, HIGH) turns it on, and the following delay(1000) keeps that state for one second.