Learning Goals 5 min
By the end of this lesson you will be able to:
- Declare a
const intat the top of a sketch and use it in place of a literal pin number whereverpinMode()ordigitalWrite()appear. - Explain in one sentence why a named constant is safer than typing
13in three different places. - Move your external LED from pin 13 to pin 9 by changing exactly one line of code and one wire — and have it still blink.
Warm-Up 10 min
Last lesson you typed 13 three times into your Blink sketch — in pinMode, in digitalWrite(13, HIGH), and in digitalWrite(13, LOW). Three copies of the same number. Today we make that number a name.
Quick-fire puzzle
Aaina has the Blink sketch open and decides she wants to move her external LED from pin 13 to pin 9. Without peeking ahead, predict:
- How many
13s does she need to change to9? - If she only changes two of them and forgets the third, what is the most likely symptom on her LED?
- What would make this whole job easier if she had to do it ten times in a row?
Reveal the answer
- Three. One in
pinMode, two in thedigitalWritecalls. - Either the LED never lights up (the missed line was
pinMode, so the pin is never set as OUTPUT) or it gets stuck on or stuck off (the missed line was one of the twodigitalWrites, so one half of the blink cycle now hits the wrong pin). All three are confusing bugs that are easy to make and hard to spot. - If she could write the pin number in one place at the top of the sketch and have the rest of the code automatically use it, the whole change would be a single edit. That's exactly what a variable is for.
New Concept 20 min
The big idea — give your numbers a name
A variable is a name that stands in for a value. Instead of typing 13 three times, you write LED_PIN three times — and decide once, at the top of the sketch, what number LED_PIN stands for. Change that single line and every use of LED_PIN changes too. This is called the single source of truth: the truth about which pin we're using lives in one place.
Two kinds of integer variable
Arduino sketches use two related types for whole numbers:
| Type | Syntax | What it's for |
|---|---|---|
const int | const int LED_PIN = 13; | A name for a value that never changes while the sketch is running. Use this for pin numbers, fixed delays, and any "magic number" you want to label. The compiler will refuse to let you reassign it later — a built-in safety net. |
int | int counter = 0; | A name for a value that can change while the sketch is running. Use this for counters, scores, accumulating measurements. We won't need a plain int until later in Level 1, but it's good to know the difference today. |
Naming conventions
- Constants get
ALL_CAPSwith underscores:LED_PIN,BLINK_MS,SENSOR_THRESHOLD. The capitals shout "I never change!" - Regular variables get
camelCase(first word lowercase, every later word capitalised):buttonState,readingCount,currentColour. Lowercase first letter for a "thing that can change". - Both must start with a letter, never a digit. Never use a hyphen — it's read as "minus".
Where the constant lives in the sketch
Declarations go above setup() — in what's called the global space. Both setup() and loop() can then see them.
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);
}The behaviour of this sketch is identical to the literal-13 version from L01-08. The hardware doesn't know the difference. Naming the pin is for your benefit — and for the benefit of every future student who reads your code.
Reuse the L01-07 wiring
Today's circuit is exactly the LED-on-D13 setup you built in L01-07. Keep it wired up. The yellow signal jumper goes from Arduino D13 to breadboard A6; the 220 Ω resistor lies in row D from D6 to D11; the red LED stands in B10/B11; the GND return runs through the breadboard's − rail back to the Arduino's GND pin.
Why it matters
Professional sketches sometimes use a dozen pins, half a dozen delay values, and a handful of thresholds. Naming every one of them at the top of the file means a colleague (or future-you) can tweak the project six months from now by editing one section, without having to read every line of the sketch.
Worked Example 20 min
Goal: convert your L01-08 Blink sketch to use a named constant, then prove the named version behaves exactly the same.
Step 1 — open your L01-08 sketch
In the Arduino IDE, open the Blink sketch you saved last lesson. It should have three copies of the number 13 inside it.
Step 2 — add the constant line
At the very top of the file, above void setup(), type:
const int LED_PIN = 13;Then leave a blank line before void setup().
Step 3 — replace every literal 13
Replace each of the three 13s with LED_PIN. Your sketch should now look like this:
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);
}Step 4 — upload and observe
Click Upload. Watch your external LED. It blinks exactly as before. The wiring didn't change. The behaviour didn't change. The Arduino can't tell that anything changed. You can — your code is now easier to read and easier to change.
Step 5 — reflect
Look at your two versions side by side (the literal-13 one from L01-08 and the named-constant one from today). Both work. Both compile. Both blink the same. So why did we bother?
- Readability:
digitalWrite(LED_PIN, HIGH);reads like a sentence.digitalWrite(13, HIGH);doesn't tell you what pin 13 is. - Changeability: Move the LED to pin 9? Edit one line. The named version protects you from forgetting one of the three replacements.
- Reusability: Tomorrow you'll wire three LEDs (L01-10). You'll want
RED_PIN,YELLOW_PIN,GREEN_PINat the top. Today is the warm-up.
Try It Yourself 20 min
Three small experiments on the named-constant Blink sketch from the Worked Example.
Goal: Move your LED from pin 13 to pin 9 by changing exactly one line of code and one wire.
- Unplug the USB cable.
- Move the yellow signal jumper: take it out of Arduino
D13, plug it into ArduinoD9instead. Everything else on the breadboard stays put. - In the IDE, change one line:
// Change this one line:
const int LED_PIN = 9;
// Everything below stays exactly the same.
void setup() {
pinMode(LED_PIN, OUTPUT);
}Question: Does the external LED still blink? Does the onboard L LED on the Arduino still blink in step with it — or does it now stay dark? Why? ____
Goal: Add a second constant — BLINK_MS — to name the delay value too. Now you can change the blink speed by editing one line at the top of the file.
const int LED_PIN = 13;
const int BLINK_MS = 500;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(BLINK_MS);
digitalWrite(LED_PIN, LOW);
delay(BLINK_MS);
}Questions:
- Without re-uploading, predict the LED's behaviour with
BLINK_MS = 500. ____ - What single edit at the top would turn the LED into a fast strobe? ____
- What single edit would turn it into a slow 3-second-on, 3-second-off pulse? ____
Goal: Find out for yourself what the const in const int really means. Try to change LED_PIN from inside loop():
const int LED_PIN = 13;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
LED_PIN = 9; // ← this line should fail to compile
digitalWrite(LED_PIN, HIGH);
}Click Upload and read the red error message in the IDE.
- Question: What does the error message say (the exact words don't matter — capture the gist)? ____
- Question: Explain in your own words why
constprotects you. ____ (Hint: imagine a bug somewhere in a 500-line sketch accidentally changesLED_PINhalfway through. What would happen?conststops that bug at compile time.)
Mini-Challenge 15 min
The "tunable beacon"
Build a Blink sketch with three constants at the top — one for the pin, one for how long the LED stays on, and one for how long it stays off. A classmate or your teacher should then be able to change the LED's entire behaviour by editing only those three lines, without ever touching setup() or loop().
Your task:
- Pick names for your three constants. Use ALL_CAPS. For example:
LED_PIN,ON_MS,OFF_MS. - Write the full sketch — your setup and loop should reference only constants and the Arduino built-ins (
HIGH,LOW,OUTPUT). No literal numbers insideloop(). - Upload and confirm it works.
- Swap laptops with a partner. Their job is to change the LED's behaviour to a slow heartbeat (
50 mson,950 msoff) by editing only the top of your sketch.
It works if:
- Your partner makes the heartbeat happen by editing exactly two lines at the top.
- Your
loop()body contains zero raw numbers — only constant names. - The sketch compiles with no red squiggles.
Reveal one valid sketch
const int LED_PIN = 13;
const int ON_MS = 100;
const int OFF_MS = 900;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(ON_MS);
digitalWrite(LED_PIN, LOW);
delay(OFF_MS);
}To turn this into a slow heartbeat, your partner edits the top three lines to ON_MS = 50 and OFF_MS = 950 — and the LED rhythm changes without anyone touching the logic.
Recap 5 min
A variable is a name that stands in for a value. Use const int for values that don't change while the sketch runs (pin numbers, fixed delays). Use plain int for values that need to change (counters, scores). Declare them above setup(). Name constants in ALL_CAPS and regular variables in camelCase.
- Variable
- A name in your code that stands in for a value. Like a label on a jar that lets you talk about "what's in the jar" without describing the contents every time.
- const int
- A name for a whole-number value that never changes while the sketch is running. The compiler refuses to let you reassign it — a built-in safety net.
- int
- A name for a whole-number value that can change. Used for counters, scores and other things that need to update.
- Global scope
- The space at the top of the sketch, above
setup(). Variables declared here are visible to bothsetup()andloop(). - Single source of truth
- The habit of writing each important value in one place in your code. Change that one place and the whole sketch updates correctly.
Homework 5 min
Name every number. Take the "name flasher" sketch you wrote for L01-08's homework (the one that flashes the LED once per letter of your first name) and rewrite it so that every number at the top is a named constant. By the end, the body of loop() should contain no literal numbers — only names like LED_PIN, ON_MS and OFF_MS.
Add a comment above each constant explaining what it stands for, like this:
// LED_PIN — which Arduino pin the external LED is wired to
const int LED_PIN = 13;Bring back next class:
- The saved
.inofile on a USB stick (call itname-flasher-named). - A short reflection on paper (2–3 sentences): which constants did you give names to, and was there one that was harder to name than the others?
- One example of a "magic number" you spotted that you decided not to name — and your reason why. (Hint: not every number needs a name. Numbers used once in the right context can stay as literals.)