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
Mia'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.
Basic 7 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?
Challenge 1 7 min
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?
Challenge 2 6 min
void setup() {
Serial.begin(9600);
for (int i = 10; i >= 7; i--) {
Serial.println(i);
}
Serial.println("go!");
}
void loop() { }Noah'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);
}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?
Challenge 3 · 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);
}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() { }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);
}
}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.
Extra Mission 5 min
Part 1 — Design a desk helper
Pick one thing on a desk at home or school — a pencil pot, a lamp, a drawer, a water glass. On paper, design how an Arduino could give it one new useful job. Use only parts from Level 1.
Your design must include:
- A name and a one-sentence "one job" for the device.
- At least one input (a button or an LDR) and the pin it uses.
- At least two outputs (LEDs or a buzzer) and the pin each one uses.
- A resistor value for every LED, worked out with Ohm's law as in Q1.
- A labelled wiring sketch.
Part 2 — Make it
Build the core of your design: one input and one output on the breadboard. Write a sketch that uses if/else on the input to drive the output. Put the output pattern in its own function, like flash() in Q5.
Bring back next class: your design sheet, the uploaded sketch and a photo of the working circuit.