Learning Goals 5 min
Before we touch a single new sensor, today we look back. By the end of this lesson you will be able to:
- Recall the eight Level 1 ideas you'll re-use in nearly every Level 2 lesson —
pinMode,digitalWrite,digitalRead, theforloop,if/else, custom functions,Serial, and Ohm's law. - Read a small sketch and predict its serial output or LED behaviour without uploading it.
- Identify your own weak spots — every wrong answer is paired with the L1 lesson that explains it, so revising tonight is a 10-minute job, not a 10-hour one.
Warm-Up 10 min
Level 2 spends most of its time on analog: PWM, ADC, smoothing, calibration, sensors. None of that lands if Level 1's digital basics are wobbly. So today we'll pause and check.
One-minute warm-up question
Aisyah's sketch looks like this. What does it do?
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}Reveal the answer
The classic Blink. The on-board LED on pin 13 turns on for one second, off for one second, and repeats forever. If you can describe that in your own words, your L1 digital foundations are intact — let's find out about the rest.
New Concept · Reading code with a stopwatch 20 min
The hidden skill of the next 47 lessons
Every Level 2 lesson hands you a sketch and asks "what will happen?". If you have to upload to find out, debugging takes ten times as long. If you can read the sketch and predict, you spot bugs before they bite.
The two-step trick
- Trace
setup()first. List which pins becomeINPUT, which becomeOUTPUT, and whatSerial.beginwas called with. This runs once. - Walk one full
loop()pass on paper. For each line, write down what changed: pin state, variable values, serial output. After one full pass, the pattern is obvious.
Example — trace this one in your head
const int LED = 9;
int count = 0;
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
count = count + 1;
Serial.println(count);
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
}Tracing: setup makes pin 9 an output and opens Serial at 9600 baud. First loop() pass — count goes from 0 to 1, Serial prints 1, LED on, 500 ms, LED off, 500 ms. Second pass: Serial prints 2. Pattern: one blink per second, with a counter ticking up forever.
The eight things to remember from Level 1
| Idea | L1 lesson | One-line reminder |
|---|---|---|
pinMode | L01-08 | Set in setup once, picks INPUT / OUTPUT / INPUT_PULLUP. |
digitalWrite | L01-08 | Drives an OUTPUT pin HIGH (5 V) or LOW (0 V). |
digitalRead | L01-16 | Reads an INPUT pin, returns HIGH or LOW. |
for loop | L01-11 | Start; condition; update. Counter often used as a pin number. |
if / else | L01-20 | Branch on a boolean. Combine with &&, ||, !. |
| Functions | L01-12 | Named recipe; called as many times as you like. |
Serial.println | L01-24/25 | Sends a value to the Serial Monitor at the matched baud rate. |
| Ohm's Law | L01-05/06 | V = I × R. 5 V across an LED with a 220 Ω resistor ≈ 14 mA. |
Worked Example · Two predict-the-output drills 20 min
Drill 1 — what does this print?
void setup() {
Serial.begin(9600);
for (int i = 1; i <= 5; i++) {
Serial.println(i * 2);
}
}
void loop() { }Trace it line by line. The for loop starts with i = 1, condition i <= 5 is true, body prints 2. Then i = 2 → prints 4. Then 3 → 6, 4 → 8, 5 → 10. Then i = 6, condition is false, loop ends. loop() is empty so nothing else happens.
Expected Serial output
2 4 6 8 10
Drill 2 — what does this LED do?
const int LED = 9;
const int BUTTON = 2;
void setup() {
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
}
void loop() {
if (digitalRead(BUTTON) == LOW) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}Trace. Pin 2 uses INPUT_PULLUP — the internal pull-up resistor holds the pin HIGH when nothing is pressed, and the button pulls it LOW when pressed (recall L01-17). So:
- Button not pressed →
digitalRead=HIGH→ theifis false →elsebranch runs → LED off. - Button pressed →
digitalRead=LOW→ theifis true → LED on.
One-line summary: LED is on while the button is held down, off otherwise.
Try It Yourself · Five quick-fire questions 20 min
Read each sketch and write down your answer in your notebook. Reveal one at a time only after you've committed to an answer on paper.
A 5 V source drives a red LED through a 330 Ω resistor. Assume the LED drops about 2 V across itself. What is the current through the LED, in mA?
Reveal
Voltage across the resistor = 5 − 2 = 3 V. Current = V / R = 3 / 330 ≈ 0.0091 A ≈ 9.1 mA. Comfortably under the LED's 20 mA limit, so 330 Ω is a safe choice. Re-read L01-05 / L01-06 if this didn't click.
int n = 3;
void setup() {
pinMode(13, OUTPUT);
if (n > 5) digitalWrite(13, HIGH);
else digitalWrite(13, LOW);
}
void loop() { }What state is the LED in after upload?
Reveal
n is 3, which is not greater than 5, so the else branch runs once and the LED is off. It stays off because loop() is empty.
void setup() {
Serial.begin(9600);
for (int i = 10; i >= 7; i--) {
Serial.println(i);
}
Serial.println("go!");
}
void loop() { }Reveal
10 9 8 7 go!
Counts down from 10 to 7 inclusive, then prints "go!". The condition i >= 7 stays true while i is 10, 9, 8, 7; at i = 6 it becomes false and the loop ends.
Faiz's sketch is meant to blink the LED twice per second. Why does the LED stay on solid instead?
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, HIGH);
delay(250);
}Reveal
Both digitalWrite lines say HIGH. The second one should be LOW. The LED never turns off, so it looks solidly on. Classic copy-paste bug.
void flash(int pin, int times) {
for (int i = 0; i < times; i++) {
digitalWrite(pin, HIGH);
delay(100);
digitalWrite(pin, LOW);
delay(100);
}
}
void setup() {
pinMode(13, OUTPUT);
flash(13, 3);
delay(500);
flash(13, 2);
}
void loop() { }How many times does the LED blink in total, after upload?
Reveal
flash(13, 3) blinks the LED three times. After a 500 ms pause, flash(13, 2) blinks it twice more. Total: five blinks. loop() is empty, so it stops there.
Mini-Challenge · Three short sketches to debug 15 min
Each of the sketches below has exactly one bug. Find it, fix it on paper (no need to upload), then check your answer.
Bug 1
// Should make pin 9 an output and blink it.
void setup() {
pinMode(9, INPUT);
}
void loop() {
digitalWrite(9, HIGH);
delay(500);
digitalWrite(9, LOW);
delay(500);
}Reveal fix
pinMode(9, INPUT) should be OUTPUT. With INPUT, the digitalWrite calls turn on / off the internal pull-up instead of driving the LED — the LED stays dim or off.
Bug 2
// Should print 1 then 2 then 3 then stop.
void setup() {
Serial.begin(9600);
for (int i = 1; i < 3; i++) {
Serial.println(i);
}
}
void loop() { }Reveal fix
The condition i < 3 stops the loop when i = 3, before printing 3. Either change to i <= 3 or i < 4. Off-by-one classic.
Bug 3
// Should turn the LED on only when the button is held.
const int LED = 9;
const int BTN = 2;
void setup() {
pinMode(LED, OUTPUT);
pinMode(BTN, INPUT_PULLUP);
}
void loop() {
if (digitalRead(BTN) == HIGH) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}Reveal fix
With INPUT_PULLUP, a pressed button reads LOW. So if (digitalRead(BTN) == HIGH) turns the LED on when the button is released — the opposite of what we want. Change to == LOW.
Recap 5 min
You don't need to memorise sketches; you need to be able to trace them. Walk setup() once, walk loop() once on paper, and the behaviour will be obvious. Tomorrow we leave plain on/off behind and meet PWM — the trick that lets one digital pin pretend to be analog. Everything in Level 2 builds on the L1 ideas you just refreshed.
- Diagnostic
- A quick test that tells you what you know and what you don't. Today's quiz is one — use the gaps to plan tonight's revision.
- Tracing
- Reading code line-by-line and writing down the value of each variable / pin after each line. Trains your eyes to spot bugs before upload.
- Off-by-one
- The most common loop bug.
i < 5runs four times (1,2,3,4);i <= 5runs five times. Always count. - INPUT_PULLUP polarity
- Pressed =
LOW, released =HIGH. Forgetting this flips every button rule.
Homework 5 min
Self-grade tonight. Go through your in-class answers and tally:
- 5 / 5 right → well done. Skim the L01-XX references in §1 just to refresh names of functions you didn't use in the quiz.
- 3 or 4 / 5 → re-read the L01 lesson next to each wrong question. Type the sketch into the IDE and step through it one line at a time.
- 0 to 2 / 5 → spend 30 minutes tomorrow re-doing L01-08, L01-11, L01-17 and L01-20. Level 2 will be tough without them.
Bring back next class:
- Your tally on a single notebook page — "X / 5 in class, weak on Y".
- Your fix for the bug-hunt sketch you found hardest, typed into the IDE.