Sub topic 9
Opening and reading the Blink sketch
This chapter assumes you have installed the Arduino IDE, can open its editor window, and know that a sketch is a program written for an Arduino board. You do not need to have uploaded anything yet. Here you open the Blink example, read it line by line, and learn what each call does on the hardware. The next chapter covers verifying and uploading it.
Why start with Blink
Blink is the standard first sketch. It switches one LED on and off with a pause between each change. It uses no extra parts, because most boards, including the Uno, have an LED soldered to the board. The sketch uses only the two required functions and three library calls, so every line can be explained.
Blink is also a test of your whole toolchain. If the LED blinks after upload, the IDE, the board package, the board selection, the port, and the USB cable all work.
Opening the example
The IDE ships with a set of example sketches. To open Blink:
- Choose File > Examples > 01.Basics > Blink.
- A new editor window opens with the sketch loaded.
The IDE opens examples as read-only copies in a temporary location. You can edit the text, but when you save, the IDE asks for a new name and folder. This keeps the original example intact. Save your copy under a name such as MyBlink. The IDE creates a folder with that name and puts MyBlink.ino inside it.
The folder and file names must match. Every sketch lives in a folder that has the same name as its main .ino file. If you rename the file without renaming the folder, the IDE complains or offers to fix it.
The code
The example file begins with a long comment block that describes the sketch and its license. The working code, with the comments removed, looks like this:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}This is complete as shown. You can paste it into an empty sketch and it will compile for any board whose core defines LED_BUILTIN.
Reading it from the top
setup(): runs once
setup() runs one time, when the board powers on or resets. Its job is to configure things that stay the same for the rest of the program. Blink has one such thing, the mode of the LED pin.
The keyword void before the name means the function returns no value. The empty parentheses mean it takes no arguments. The Arduino core calls this function for you, so you never call it yourself.
pinMode(): choosing a direction
pinMode(LED_BUILTIN, OUTPUT);A digital pin on a microcontroller (the small chip that runs your code) can work in two directions. As an input, it reads the voltage applied to it. As an output, it drives a voltage. pinMode(pin, mode) selects the direction. The two arguments are:
LED_BUILTIN: a constant that the board's core defines. It holds the pin number of the on-board LED.OUTPUT: a constant meaning the pin will drive a voltage.
Pins default to input at power-up, so without this call the LED would not light reliably.
LED_BUILTIN: why a constant
On an Uno, the on-board LED is wired to digital pin 13. On other boards it is a different pin, and on some boards the LED is wired so that it turns on at a low voltage. Using LED_BUILTIN lets the same sketch work across boards. The IDE chooses the correct value from the board you selected, which is another reason the board selection step matters.
| Board | Pin behind LED_BUILTIN |
|---|---|
| Arduino Uno | 13 |
| Arduino Mega 2560 | 13 |
| Arduino Nano | 13 |
Some other boards use different pins. Check your board's documentation if you are unsure.
loop(): runs forever
After setup() finishes, the core calls loop(). When loop() returns, the core calls it again, without end, until power is removed. This is why the sketch keeps blinking and does not stop after one cycle.
Within one pass, the four statements run in order:
digitalWrite(LED_BUILTIN, HIGH);drives the pin to its high voltage (5 V on an Uno, 3.3 V on boards with 3.3 V logic). The LED lights.delay(1000);pauses the program for 1000 milliseconds, which is one second.digitalWrite(LED_BUILTIN, LOW);drives the pin to 0 V. The LED goes dark.delay(1000);pauses for another second.
The cycle is two seconds long: one second on, one second off.
What the calls do on the hardware
digitalWrite() writes to a register, a small memory location inside the microcontroller that controls the pin. The change takes effect in microseconds. The LED stays in its new state because the register holds the value until the next write.
delay() is a blocking call. While it runs, the processor does nothing else in your sketch. For Blink this is fine. In a larger program, a long delay() makes the board unresponsive to buttons or sensors, and you would later replace it with timing based on millis(). For now, treat delay() as the simplest way to wait.
The argument to delay() is an unsigned long number of milliseconds. Changing 1000 to 250 makes the LED blink four times as fast.
Reading the comments
The example starts with a block comment between /* and */. Everything inside it is ignored by the compiler. A line comment starts with // and runs to the end of the line. The original example also contains line comments next to each statement, such as // turn the LED on (HIGH is the voltage level). These are notes for human readers and have no effect on the compiled program.
Good comments explain why code exists, not what it does. A comment that says // set pin to output next to pinMode(...) repeats the code. A comment that says // the LED is active-low on this board records something the code cannot show.
Experiments to try before uploading
You can change the sketch and predict the result before you upload anything. Predicting first builds a working model of the code.
- Change both delays to
200. The LED should blink faster, about two and a half times per second. - Make the on time
100and the off time900. The LED gives a short flash once per second. - Swap
HIGHandLOW. On an Uno, the LED is dark for the first second and lit for the second. The pattern is inverted, but the cycle length is unchanged. - Remove the
pinModeline. On many boards the LED will glow dimly or not at all, because the pin is still an input anddigitalWriteon an input only toggles an internal pull-up resistor. Treat the result as unreliable rather than as a feature.
Common errors when reading and editing the sketch
| Symptom | Likely cause |
|---|---|
| `'setup' was not declared` or `undefined reference to 'setup'` | The function is misspelled or missing. Both `setup` and `loop` are required. |
| `expected ';' before '}' token` | A statement is missing its closing semicolon. |
| `'led_builtin' was not declared in this scope` | C++ is case-sensitive. The constant is `LED_BUILTIN` in capitals. |
| `'High' was not declared in this scope` | `HIGH` and `LOW` must be written in capitals. |
| The IDE asks to move the sketch into a folder | The file name and its folder name do not match. |
The error messages appear in the black console area below the editor when you verify the sketch. The next chapter shows how to read them.
Summary of the model
- The core calls
setup()once, then callsloop()over and over. pinMode()sets a pin's direction, and it belongs insetup()because it only needs to happen once.digitalWrite()sets an output pin high or low, anddelay()holds that state for a number of milliseconds.LED_BUILTINkeeps the sketch portable across boards.
Exercise
A learner changes the sketch so that loop() contains digitalWrite(LEDBUILTIN, HIGH); delay(500); digitalWrite(LEDBUILTIN, LOW); delay(1500); on an Uno. How long is one full cycle, and for what fraction of it is the LED lit?
Show answer
One cycle is 500 ms + 1500 ms = 2000 ms, which is 2 seconds. The LED is lit for 500 ms of that, so one quarter (25%) of the cycle.
Exercise
Why is pinMode(LED_BUILTIN, OUTPUT) placed in setup() and not in loop()? Would the sketch still work if it were moved into loop()?
Show answer
The pin direction only needs to be set once, and setup() runs once. If pinMode were in loop(), the sketch would still work, because setting the same mode repeatedly is harmless, but it wastes a small amount of time on every pass and obscures the structure of the program.