Learning Goals 5 min
By the end of this lesson you will be able to:
- Type a Blink sketch from scratch — no copying — using
pinMode()insetup()anddigitalWrite()withHIGHandLOWinsideloop(). - Predict what happens to the external LED if you swap
HIGH↔LOW, or change adelay()value, before uploading. - Make your external LED flash in a rhythm different from the original Blink sketch — for example, a brief flicker every two seconds.
Warm-Up 10 min
Last lesson we wired an external LED to pin 13. We re-used the Blink sketch from L01-02 and watched it flash. Today we stop borrowing other people's code and write our own.
Quick-fire puzzle
Aaina is staring at the Blink sketch in the Arduino IDE. The two delay(1000) lines catch her eye. Without uploading anything, predict:
- If she changes both delays from
1000to100, what will the LED look like? - If she changes both delays from
1000to5000, what will the LED look like? - If she changes only the first delay to
100and leaves the second at1000, what happens?
Reveal the answer
- The LED flashes ten times faster — so fast that your eye starts to see it as a flicker rather than separate flashes.
- The LED takes five seconds on, five seconds off — a slow, lazy pulse. You can count to five in between.
- You get a short flash + long pause pattern. The LED is ON for 100 ms then OFF for 1 second — like a beacon on a lighthouse.
Two numbers in the code = two completely different physical behaviours. That is the magic of digital output, and the thing we will master today.
New Concept 20 min
The big idea — a digital pin is a switch
Each digital pin on the Arduino is a tiny switch that you control from code. When the switch is closed, the pin is at 5 V — we call this HIGH. When it is open, the pin is at 0 V — we call this LOW. A wired-up LED on that pin glows when the pin is HIGH and goes dark when it is LOW.
To use a pin as a switch you need three commands. Today's lesson is all about them.
The three functions you'll meet today
| Function | What it does | Where it lives |
|---|---|---|
pinMode(pin, mode) | Tells the chip whether the pin is an OUTPUT (sends signals out, e.g. to an LED) or an INPUT (reads signals in, e.g. from a button). | Inside setup() — runs once. |
digitalWrite(pin, value) | Puts the pin to HIGH (5 V) or LOW (0 V). The actual on/off switching. | Inside loop() — runs over and over. |
delay(ms) | Pauses the sketch for ms milliseconds. delay(1000) = wait 1 second. delay(500) = wait half a second. | Inside loop() — used between the HIGH and LOW. |
Reuse the circuit from L01-07
You don't need to rewire anything today. The exact circuit from L01-07 — external LED on pin 13 via a 220 Ω resistor, GND return through the breadboard's − rail — is the canvas for every sketch in this lesson. Keep it powered on.
| Pin / Hole | Connects to | Wire colour |
|---|---|---|
Arduino D13 | Breadboard A6 | Yellow |
Breadboard D6 – D11 | 220 Ω resistor | (component) |
Breadboard B11 / B10 | LED anode / cathode | (component) |
Breadboard C10 | − rail (via short jumper) | Black |
| − rail | Arduino GND | Black |
The full Blink sketch — line by line
Open the Arduino IDE. Go to File → New to start a blank sketch. Type the following yourself — do not copy-paste. The act of typing it burns the structure into your fingers.
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}Read it slowly:
setup()runs once when the Arduino powers on. We use it to declare that pin 13 is an OUTPUT.loop()runs forever, top to bottom, then starts again. Inside it we set pin 13 HIGH, wait 1 second, set it LOW, wait 1 second, repeat.- The result: pin 13 turns on for 1 s, then off for 1 s, forever. Both the onboard
LLED and your external LED blink in step.
Why it matters
Every output device you will ever drive — LEDs, buzzers, motors, relays, even your Wi-Fi router's status lights — is switched on and off using exactly this digitalWrite(pin, HIGH/LOW) pattern. Master it today and every later lesson rests on it.
Worked Example 20 min
Goal: type the Blink sketch from scratch, upload it, then change one number and watch the change happen in front of you.
Step 1 — type the sketch from scratch
Already done in the New Concept section. Save it as my-first-blink.
Step 2 — upload
- Plug in the USB cable.
- In the IDE:
Tools → Board → Arduino UNO, thenTools → Port → (your COM port). - Click the round → arrow button (Upload). Wait for "
Done uploading" in the status bar. - Your external LED on pin 13 should now blink once per second, in step with the onboard
LLED. If it doesn't, check polarity and the resistor (see L01-07).
Step 3 — change one number
Change only the first delay(1000) to delay(100). Leave the second at 1000.
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(1000);
}Step 4 — upload and observe
Re-upload. The LED now flashes briefly (100 ms) and stays off for a full second. It looks like a slow heartbeat. The pin's behaviour changed because the timing in the code changed, not because the wiring changed.
Step 5 — predict, then test
Before you change anything else, write down on paper what you expect to happen if you change the second delay to 2000. Then change it and check. Was your prediction right? Predicting is a skill — by L01-20 you'll be doing it automatically.
What changed since last lesson? The wiring is identical to L01-07. What's new is that you wrote the sketch — so you can make the LED do anything you want by adjusting two numbers.
Try It Yourself 20 min
Three small variations. For each one, predict the behaviour on paper before you upload, then check.
Goal: A "stutter blink" — the LED is on for 250 ms, off for 250 ms. Four flashes per second.
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
delay(250);
}Question: Compared to the original Blink, what does the LED look like? ____
Goal: A "lighthouse beacon" — the LED gives a brief, sharp flash once every 3 seconds. The bulb is OFF most of the time, ON for just a moment.
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(80);
digitalWrite(13, LOW);
delay(2920);
}Questions:
- Why is the second delay
2920and not3000? ____ - If you wanted the beacon to flash every 5 seconds instead, what two numbers would you change? ____
Goal: A "mostly on" pattern — the LED is ON for 5 seconds, then OFF for 1 second, repeating. Looks like a steady light that briefly winks out.
// Fill in the two delays so the LED is on for 5 s then off for 1 s.
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(____);
digitalWrite(13, LOW);
delay(____);
}Bonus question: What real-world product behaves like this — mostly on, with a brief blink every few seconds? ____ (Hint: think about anti-theft devices or remote-control toys in standby.)
Mini-Challenge 15 min
Design your own blink pattern
Pick one of these real-world rhythms and write a sketch — under 10 lines of code — that makes the LED imitate it. The hardware is the L01-07 circuit. The only tools you have today are pinMode, digitalWrite, HIGH, LOW and delay.
- Phone notification — two brief flashes, then a long pause. Like the LED on your phone when a WhatsApp message arrives.
- Smoke detector low-battery — a single sharp flash every 30 seconds.
- KTM crossing barrier — slow, urgent, on for 700 ms, off for 700 ms.
- Stalled engine warning — long on, short off, long on, short off. Like a slow Morse "T" repeating.
It works if:
- A partner who didn't see your code can guess which of the four patterns you chose, just from watching the LED for 30 seconds.
- Your sketch compiles with no red squiggles.
- The sketch is under 10 lines of code (excluding blank lines).
Reveal one possible answer — the phone notification
Two quick flashes, then a long pause. The challenge here is that you cannot quite get "two flashes" in under 10 lines without repeating the digitalWrite/delay pattern. Here is the cleanest version:
// Phone notification: brief flash, pause, brief flash, long pause.
void setup() { pinMode(13, OUTPUT); }
void loop() {
digitalWrite(13, HIGH); delay(120);
digitalWrite(13, LOW); delay(180);
digitalWrite(13, HIGH); delay(120);
digitalWrite(13, LOW); delay(3000);
}(Two statements on one line is normally not allowed — but for the rare "tight" case where the cap is in the way, you can pair a digitalWrite with its delay. From L01-11 "The for Loop" onwards we'll have a better tool for repeating patterns and the one-statement-per-line rule comes back in full force.)
Recap 5 min
A digital pin is a switch. pinMode() declares the pin is an output, digitalWrite() flips it between 5 V (HIGH) and 0 V (LOW), and delay() pauses the sketch between flips. Three functions and two numbers are all it takes to control any LED, buzzer or relay from code.
- pinMode(pin, mode)
- Sets a pin as
OUTPUT(sends signal) orINPUT(reads signal). Lives insetup(). - digitalWrite(pin, value)
- Puts the pin to
HIGH(5 V) orLOW(0 V). Lives inloop(). - HIGH / LOW
- The two states of a digital pin. Type them in capitals — they are special names the Arduino library knows.
- delay(ms)
- Pauses the sketch for
msmilliseconds. 1000 ms = 1 second. 500 ms = half a second. - Digital output
- Using a pin to send a 5 V or 0 V signal out of the chip. The whole point of today's lesson.
Homework 5 min
The "name flasher". Write a sketch that flashes the LED a number of times equal to the length of your first name in letters, then waits 3 seconds, then repeats.
- Aiman (5 letters) → 5 quick flashes, pause, repeat.
- Wei Jie (6 letters, ignoring the space) → 6 quick flashes, pause, repeat.
- Priya (5 letters) → 5 quick flashes, pause, repeat.
Use only pinMode, digitalWrite, HIGH, LOW and delay. Your sketch will be longer than 10 lines — that's fine for homework. From L01-11 we will learn the for loop, which makes patterns like this much shorter.
Bring back next class:
- The saved
.inofile on a USB stick. - A short phone video (10–15 seconds) of the LED doing your name pattern.
- A one-line answer on paper: how many times does your
loop()body need to repeat thedigitalWrite(HIGH)…digitalWrite(LOW)pair?