Learning Goals 5 min
By the end of this lesson you will be able to:
- Explain in your own words what code inside
setup()does versus what code insideloop()does, using a board running Blink as your example. - Point at any line of an Arduino sketch and name what it is: a statement, a comment, a function header, an opening brace or a closing brace.
- Modify Blink so the LED comes on once at start-up and then blinks slowly forever, upload it, and observe both behaviours on the board.
Warm-Up 10 min
Last lesson you uploaded your first sketch. Today we slow right down and ask: why is a sketch shaped the way it is?
Quick-fire puzzle
You upload the sketch below to your Arduino. What does the LED actually do?
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
void loop() {
}Reveal the answer
The LED switches on the moment you finish uploading — and then it just stays on forever. It never blinks.
That is because the only digitalWrite call lives in setup(), which runs once. The loop() function is empty, so nothing is happening over and over. The board is running — it is just running an empty loop, which looks the same as doing nothing.
New Concept 20 min
The big idea — once vs. forever
Every Arduino sketch has the same two top-level functions, and they have different jobs:
setup()is like turning on a kitchen — preheat the oven, lay out the pans. It runs once the moment the board powers up (or is reset by the reset button).loop()is like cooking — chop, stir, taste, repeat. As soon assetup()finishes, the board jumps to the top ofloop()and runs it again and again, forever, until the power is removed.
Put pin-mode declarations, library start-ups and "ready" indicators in setup(). Put behaviour you want to keep happening in loop().
Parts of a sketch — your decoder ring
The table below lists every kind of piece you will meet in this lesson's sketches. Memorising the names now makes every future lesson easier to read.
| Element | Looks like | What it is for |
|---|---|---|
| Variable declaration | int x = 5; | Names a storage box and puts a value in it. |
| Constant declaration | const int LED_PIN = 13; | Names a value that will never change. |
| Function definition | void setup() { … } | A named block of code the board can run. |
| Function body | everything between { and } | The instructions that belong to that function. |
| Statement | a line ending in ; | One single instruction for the chip. |
| Line comment | // like this | A note for humans — ignored by the chip. |
| Block comment | /* like this */ | A multi-line note for humans. |
| Semicolon | ; | Marks the end of a statement. Forgetting it is the #1 beginner error. |
| Indentation | 2 spaces before a line | Visual signal that a line lives inside a block. |
The minimal sketch
void setup() {
// runs ONCE when the board powers on or is reset
}
void loop() {
// runs OVER and OVER, forever
}Why it matters
Once you can name every part of a sketch, you can read any Arduino code on the internet — even code that uses parts we have not taught yet. The skeleton stays the same; only the inside changes.
Worked Example 20 min
Here is the Blink sketch one more time. Today the goal is not to upload it — you can already do that — but to name every part of it out loud.
const int LED_PIN = 13;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
}With your teacher, point at each numbered line and say what it is using the table above:
const int LED_PIN = 13;— a constant declaration and a statement (note the semicolon).void setup() {— a function header with an opening brace.voidmeans "returns nothing".pinMode(LED_PIN, OUTPUT);— a statement living inside the body ofsetup(). The 2-space indent shows it is "inside".}— the closing brace ofsetup().void loop() {— a function header again, this time forloop().- Four more statements inside the body of
loop(): switch on, wait, switch off, wait. }— the closing brace ofloop().
What changed since last lesson? Nothing in the sketch — but you can now name every single piece of it.
Try It Yourself 20 min
For each task, edit the sketch and upload it. After you upload, watch the LED and check it does what the task asks.
Goal: Add a line comment on the line directly above void setup() that says, in plain English, what this sketch does.
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}Goal: Move the line digitalWrite(13, HIGH); out of loop() and into setup(). Delete the digitalWrite(13, LOW); line entirely. Upload and write down (in a comment) what the LED now does.
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}Goal: Add a block comment (/* ... */) at the top of the sketch that has your name on one line and a one-sentence description on the next. Then change 13 everywhere to a named constant LED_PIN.
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}Mini-Challenge 15 min
Boot flash + slow blink
Combine today's understanding of setup() versus loop() with the upload skill from ARD-L01-02. Write a sketch that:
- flashes the LED once quickly at start-up (about 200 ms on, then off), and then
- blinks the LED slowly forever (one second on, one second off).
Hint: the quick flash belongs in setup(). The slow blink belongs in loop().
It works if a partner who watches the board from the moment you press Upload can describe two distinct behaviours — first a single quick blink, then steady slow blinking.
Recap 5 min
Every Arduino sketch has the same skeleton: setup() runs once when the board wakes up, and loop() runs over and over forever. Inside each function are statements (lines ending in ;), and around them are comments (notes for you). Curly braces group statements into a function body.
- setup()
- The function that runs once, when the board powers on or is reset.
- loop()
- The function that runs over and over, forever, until you remove power.
- Statement
- One instruction for the chip. Always ends with a semicolon
;. - Comment
- A note for humans.
//for one line,/* */for many. - Function body
- The code between
{and}that belongs to a function.
Homework 5 min
Comment commentary. Open one of the Blink variants you saved last lesson. Add a line comment above every single code line explaining in your own words what that line is doing.
- Save the file with a new name ending in
-commented. - Upload it — your comments should make no difference to the LED, because the chip ignores them.
- Read your commented sketch out loud, top to bottom.
Bring back next class: the saved .ino file on a USB stick or a screenshot of the IDE window showing your fully-commented sketch.