Learning Goals 5 min
By the end of this lesson you will be able to:
- Wire five LEDs in a row, each with its own 220 Ω resistor, all sharing one GND through the breadboard's − rail — and address them as
leds[0]throughleds[4]by storing their pin numbers in an array. - Store an animation as a 2D array of frames: each row of the array is one snapshot of all five LEDs, and each row gets shown in turn.
- Write a single nested
for-loop "player" that walks the animation — outer loop over frames, inner loop over LEDs — so that adding a new pattern is a data change, not a code change.
Warm-Up 10 min
L01-33 took a tune and stored it as data. Today the same trick goes from sound to light. Five LEDs in a row are the visual analog of a tiny screen — and just like a screen, you tell them what to show by giving them a sequence of frames.
Quick-fire puzzle
You want to drive five LEDs through a "Knight Rider" sweep — the light moves left to right across all five, then back, then left to right again, forever. Without arrays, every frame is a fresh set of digitalWrite calls.
- How many
digitalWritecalls would one frame need (one snapshot of all five LEDs)? - If the sweep has 10 frames (left to right, then back), how many
digitalWritecalls in total — typed out as separate lines — would the whole animation take? - If you wanted to change the pattern later, how many of those lines would you need to find and edit?
Reveal the answer
- Five — one per LED, every frame.
- 50 calls, plus 10
delays. Possibly more if you have tosetLeds(LOW, …)the rest each frame. - Whichever lines describe the "wrong" behaviour. With 50 scattered calls, finding the right ones is a chore — and an easy place to introduce typos.
Arrays compress all of this dramatically. The animation becomes one rectangular block of 1s and 0s that you can read like a picture in the source code: each row is one moment in time, each column is one LED. A single nested loop walks the whole thing. Editing the pattern means changing the 1s and 0s — never the surrounding code.
New Concept — three arrays you'll meet today 15 min
The big idea — pins, frames, and a stack of frames
Three array shapes show up in every multi-LED sketch from here on. Learn the shapes; the rest is bookkeeping.
Array #1 — pin numbers
You have five LEDs, each on its own Arduino pin. Don't write the pin numbers all over the place; collect them once at the top:
const int NUM_LEDS = 5;
int leds[NUM_LEDS] = { 7, 8, 9, 10, 11 };Now leds[0] is pin 7, leds[1] is pin 8, and so on. To set the leftmost LED, write digitalWrite(leds[0], HIGH). To set the rightmost, digitalWrite(leds[4], HIGH). The benefit: position (0–4 from left to right) is now separate from which Arduino pin. Rewiring is a one-line change.
Array #2 — one frame (a snapshot of all five)
A frame is one moment of the animation — what each LED is doing right now. With five LEDs and only HIGH/LOW values, a frame is five 1s and 0s:
int frame[NUM_LEDS] = { 1, 0, 0, 0, 0 }; // only leftmost onframe[i] is the desired state of LED i. (Arduino accepts the literals 1 and 0 wherever it expects HIGH and LOW — they're literally the same numeric values. Using 1/0 here lets the animation array look like a picture.) A small helper that takes a frame and applies it:
void showFrame(int frame[]) {
for (int i = 0; i < NUM_LEDS; i = i + 1) {
digitalWrite(leds[i], frame[i]);
}
}One call to showFrame pushes all five values to the LEDs in a single line of your top-level code. That's the L01-33 idea in pure form — data goes in, a loop does the boring work.
Array #3 — a 2D array of frames (the whole animation)
One frame is a snapshot. An animation is a sequence of snapshots. Stack them as a 2D array: rows = frames, columns = LEDs.
const int NUM_FRAMES = 5;
int chase[NUM_FRAMES][NUM_LEDS] = {
{ 1, 0, 0, 0, 0 }, // frame 0: leftmost
{ 0, 1, 0, 0, 0 }, // frame 1: one right
{ 0, 0, 1, 0, 0 }, // frame 2: middle
{ 0, 0, 0, 1, 0 }, // frame 3: one more right
{ 0, 0, 0, 0, 1 } // frame 4: rightmost
};Two pairs of square brackets in the declaration. First number is the number of frames; second is LEDs per frame. The rectangle of 1s and 0s in the curly braces is the animation — you can read it like a picture. The diagonal line of 1s shows the LED moving left to right across the row.
To get the value of LED i in frame f, write chase[f][i]. The first index picks the row; the second picks the column.
The nested-loop player — outer for frames, inner for LEDs
One for walks frames; inside it, another walks LEDs. The whole player is six lines:
for (int f = 0; f < NUM_FRAMES; f = f + 1) {
for (int i = 0; i < NUM_LEDS; i = i + 1) {
digitalWrite(leds[i], chase[f][i]);
}
delay(150);
}That's it. The outer loop counts frames 0, 1, 2, 3, 4. For each, the inner loop pushes five LED values, then a 150 ms pause holds the frame on screen. Six lines and you've played a whole animation. Change the pattern? Edit the rectangle. Change the speed? Edit the delay. Change the number of LEDs? Edit two constants.
Why it matters
The "frames stacked as rows in a 2D array" shape is exactly how every screen and image format on earth stores a picture or video — including this web page, your phone's camera roll, and every game you've ever played. Today's array is 5 wide × N tall and made of 1s and 0s; a real screen's is 1920 wide × 1080 tall and made of RGB tuples; but the structure is the same. Master it on five LEDs and you've got the foundation of every pixel-based device.
Worked Example — five LEDs, three patterns 20 min
Step 1 — Wire the row
Five LEDs in a row across the breadboard, each built the way every module has been built since L01-10: the 220 Ω resistor lying in row C, the LED standing in row A, and the signal jumper from the Arduino plugging into row E. All five cathodes drop onto the − rail, which carries one shared GND wire back to the Arduino's GND pin. This is the same "one GND bus" wiring as L01-15, extended from three LEDs to five — the only thing that gives is the spacing, six columns per module instead of eight, which is what makes five of them fit on a 30-column board.
On the breadboard what you build
On paper what engineers draw
C, LED in row A, jumper in row E, and the return climbing the free column next door.Step 2 — The full sketch with three named patterns
// Multi-LED patterns — five-frame chase, ten-frame bounce, eight-frame wave
const int NUM_LEDS = 5;
int leds[NUM_LEDS] = { 7, 8, 9, 10, 11 };
const int CHASE_FRAMES = 5;
int chase[CHASE_FRAMES][NUM_LEDS] = {
{ 1, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0 },
{ 0, 0, 1, 0, 0 },
{ 0, 0, 0, 1, 0 },
{ 0, 0, 0, 0, 1 }
};
const int BOUNCE_FRAMES = 8;
int bounce[BOUNCE_FRAMES][NUM_LEDS] = {
{ 1, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0 },
{ 0, 0, 1, 0, 0 },
{ 0, 0, 0, 1, 0 },
{ 0, 0, 0, 0, 1 },
{ 0, 0, 0, 1, 0 },
{ 0, 0, 1, 0, 0 },
{ 0, 1, 0, 0, 0 }
};
const int WAVE_FRAMES = 8;
int wave[WAVE_FRAMES][NUM_LEDS] = {
{ 1, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0 },
{ 1, 1, 1, 0, 0 },
{ 1, 1, 1, 1, 0 },
{ 1, 1, 1, 1, 1 },
{ 0, 1, 1, 1, 1 },
{ 0, 0, 1, 1, 1 },
{ 0, 0, 0, 1, 1 }
};
void showFrame(int frame[]) {
for (int i = 0; i < NUM_LEDS; i = i + 1) {
digitalWrite(leds[i], frame[i]);
}
}
void playPattern(int pattern[][NUM_LEDS], int numFrames, int stepMs) {
for (int f = 0; f < numFrames; f = f + 1) {
showFrame(pattern[f]);
delay(stepMs);
}
}
void setup() {
for (int i = 0; i < NUM_LEDS; i = i + 1) {
pinMode(leds[i], OUTPUT);
}
}
void loop() {
playPattern(chase, CHASE_FRAMES, 120);
playPattern(bounce, BOUNCE_FRAMES, 120);
playPattern(wave, WAVE_FRAMES, 180);
}Step 3 — Upload and watch
- Upload. You should see the chase sweep left-to-right once (5 frames × 120 ms = 600 ms total).
- Then the bounce: same sweep right, then back left, taking about a second.
- Then the wave: a growing band fills the row, then a gap eats it from the left. This is the prettiest pattern — eight frames spelt out as a tiny picture in code.
- Then everything starts again from the top of
loop().
Notice setup() uses a loop too — five pinMode calls compressed into one. Any time you have "the same thing for every entry in leds", a small for loop is the right move.
Reading the arrays as pictures
Tilt your head and look at the wave array — the 1s form a parallelogram sweeping from upper-left to lower-right. That's exactly what the LEDs do on the breadboard. You can design patterns by drawing 1s and 0s on paper first, then typing them in. This is the same workflow a sprite-sheet artist uses for 2D games: paint pixels in a grid, paste the grid into code.
Trace one frame on paper
The player calls showFrame(bounce[5]) on frame index 5 of the bounce array. Fill in what each LED is set to.
i | leds[i] | bounce[5][i] | LED state |
|---|---|---|---|
| 0 | 7 | 0 | off |
| 1 | 8 | ____ | ____ |
| 2 | 9 | ____ | ____ |
| 3 | 10 | ____ | ____ |
| 4 | 11 | ____ | ____ |
If you can fill in this table without uploading, you've internalised both the 1D pin lookup and the 2D frame indexing in one go.
Basic 7 min
Goal: A countdown pattern that goes 5 → 4 → 3 → 2 → 1 → 0 LEDs lit, then resets. Use the worked example's wiring and player; just add a new 6-frame 2D array.
1. Think first (on paper, no typing yet): draw the six frames as a grid — five columns, one row per frame, 1 for lit and 0 for off. Each frame has one fewer LED lit than the one before. Which end goes dark first is your choice.
2. Code it. Open your Worked Example sketch. Use the chase array as a model for the shape of a pattern, and type your grid in as a new pattern called countdown with its own frame-count constant. Then play it from loop(), holding each frame for 1 second so it feels paced like a real countdown.
3. Upload and observe. Compare the LEDs with your paper grid, row by row.
It works if:
- All five LEDs start lit, one goes out each second, and then all are off.
- The pattern then starts again from five.
Questions:
- Squint at the 1s in your array — what shape do they make? Why does that shape look like the animation? ____
- Which LED did you switch off first — the left end or the right end? Swap it round. What changed in your array? ____
- What change makes the countdown go the other direction (1 LED grows to 5)? Try it. ____
- If you wanted a buzz at the very end (final frame = all off + a buzzer beep), where in your code would the new
tone(BUZZER_PIN, ...)call go? ____
Challenge 1 7 min
Goal: A generated sparkle. Instead of storing a fixed pattern, each frame randomly picks two of the five LEDs to be on. Run for 50 frames at 80 ms each. No array of frames needed.
1. Think first (on paper, no typing yet):
- Write, in plain English, what has to happen in one frame of sparkle. How many jobs is that? In what order?
- What happens to the LEDs from the last frame if you only ever switch LEDs on?
random(0, NUM_LEDS)gives a position from 0 to 4 — not a pin number. Which array turns a position into a pin?- The whole thing repeats 50 times. Which structure counts the frames?
2. Code it. In your Worked Example sketch, write a function called sparkle that is told how many frames and how long each one lasts, then call it from loop(). Build one job at a time and upload after each. Useful piece: the for loop over leds[] in showFrame(). (Two picks might land on the same LED — that's fine, you'll just get one on.)
3. Upload and observe. Watch for about four seconds. Is there ever a frame with three or more LEDs lit? There shouldn't be.
It works if: two LEDs (sometimes one) flicker on at random, a new pair every 80 ms, for about four seconds.
Questions:
- Your sketch has no 2D array. Why is that allowed — and what makes sparkle different from the chase or bounce patterns? ____ (Hint: sparkle isn't a fixed sequence — every run is different.)
- Leave out the "everything off" job and upload. What happens after a few frames, and why? ____
- Each random call returns 0–4. About what percentage of frames will both picks land on the same LED, giving you only one lit instead of two? ____ (Hint: 1 in 5.)
- Change your sketch to sparkle three LEDs per frame. What did you add? ____
Challenge 2 6 min
Goal: A binary counter. The five LEDs show the value of a number that counts from 0 to 31 in binary, with one frame per number. LED 0 is the 1s place, LED 1 is the 2s, LED 2 is the 4s, LED 3 is the 8s, LED 4 is the 16s. No 2D array — derive the LED states from the counter value each frame.
This is the first time you've seen computed animation: the pattern is mathematics, not data. The hint: for each LED i at counter value n, the LED is on if the bit at position i is set, which is (n >> i) & 1.
(The >> operator shifts a number right by i bits; & 1 keeps only the lowest bit of the result. Together they say "is bit i set in n?" You'll meet these operators properly in Level 2 — for now, treat them as the formula that makes binary work.)
Here are the first eight numbers, 0 to 7 (LEDs 3 and 4 stay dark until 8 and 16):
1. Think first (on paper, no typing yet):
- Two things change in this animation: the number being shown, and which LED you are setting. Which one changes once per frame, and which one runs through all five within a frame?
- So one loop sits inside the other. Which goes on the outside? Where does the 400 ms wait go — inside the inner loop, or after it?
- Check the formula on paper for
n = 5: which of the five LEDs should be lit?
2. Code it. Write a function called binaryCount() and call it from loop(). Start by getting the number to count with only LED 0 using the formula, upload, then extend it to all five.
3. Upload and observe. Compare the first eight numbers with the diagram, then watch it reach 31.
It works if: LED 0 blinks fastest, LED 1 half as fast, and so on — and at 31 all five are lit.
Questions:
- Which loop did you put on the outside, and why does it have to be that one? ____
- How long does the full count from 0 to 31 take with a 400 ms per-frame delay? ____
- Compare your approach to writing 32 frames as a 2D array. Which is shorter? Which is easier to understand at a glance? ____
- If you had 8 LEDs and the same counter, what's the largest number it could display? ____ (Hint:
2⁸ − 1.)
Challenge 3 — crossing trains 10 min
"Two lights pass each other"
Build a new pattern called crossing. Two lights start at the two ends, on LED 0 and LED 4. Each frame, both move one LED towards the middle. When they meet on LED 2, the next frame lights all five — a crash flash. Then they carry on outwards, back to the two ends.
Your task:
- On squared paper, draw the frames: 5 columns, one row per frame, 1 for lit and 0 for off.
- Type it as
int crossing[CROSS_FRAMES][NUM_LEDS], one curly-braced row per frame. - Add a matching
const int CROSS_FRAMESthat equals your number of rows. - Play it from
loop()with the same player, 200 ms per frame. Do not changeplayPatternorshowFrame.
Think first: before you draw, answer: when both lights are on LED 2 at the same moment, how many LEDs are lit in that frame? How many frames moving in, how many moving out?
Code it from your paper grid, then upload and observe: watch one full run and tick each frame off on your paper.
It works if:
- The lights start at both ends and move in by one LED per frame.
- They meet on the middle LED, and then all five flash for one frame.
- They finish back at the two ends, ready to repeat.
- No frame shows an LED left on from an earlier frame by mistake.
Recap 5 min
Three array shapes do all the heavy lifting today: a 1D array of pin numbers (leds) so position is separate from pin; a 1D array of HIGH/LOW values (frame) so one snapshot of the LEDs is one tidy unit; and a 2D array of frames (pattern) so an animation is a rectangle of 1s and 0s that looks like what you'll see on the LEDs. A single nested for loop walks the rectangle — outer over frames, inner over LEDs — and one helper function plays any animation. Adding new patterns means adding data; the player code never changes.
- Frame
- One snapshot of an animation. With five LEDs, a frame is five values — one per LED — describing what each one is doing right now.
- 2D array
- An array of arrays. Declared with two pairs of brackets —
int pattern[ROWS][COLS]— and indexed the same way:pattern[r][c]. The rows-and-columns shape mirrors the rectangle you draw on paper. - Nested loop
- A
forloop inside anotherforloop. The standard tool for walking a 2D array: outer loop picks the row, inner loop picks the column. - Player function
- A small function that takes a pattern (and its size and timing) and runs it on the LEDs. Once written, every new pattern is a data change, not a code change.
- Data-driven animation
- The technique of describing an animation as a block of values (1s and 0s here, RGB tuples in a game) and using one generic loop to play it back. The opposite of writing each frame as separate code.
- Generated animation
- An animation that's computed each frame rather than stored — like the sparkle (random) or binary counter (mathematics). Both styles coexist; pick whichever fits the effect.
1and0vsHIGHandLOW- Inside C++,
HIGHandLOWare literally the numbers 1 and 0. Using the digits in a 2D-array initialiser makes the data look like a tiny picture on the page — that's the only reason to prefer them over the keyword names.
Extra Mission 5 min
Part 1 — Design a five-LED signal
A row of lights can send a message: a loading bar, a bike's "turning left" arrow, or a score bar in a game. Design your own animation for five LEDs.
Your design must include:
- What the signal means and where it would be used.
- A grid on squared paper: 5 columns wide and at least 10 frames tall, filled with 1s and 0s.
- The hold time for each frame in ms.
- Whether it repeats forever or plays once.
Part 2 — Make it
Type your grid as a 2D array with a matching FRAMES constant. Play it with playPattern() from the worked example. Change only the 1s and 0s, never the player.
Bring back next class:
- The saved
.inofile. - A short video of the animation.
- A photo of your paper grid.
Heads up for next class: L01-35 "Mood Lamp + Theme Tune" is the Cluster E project — it combines today's array-of-frames animation with last lesson's array-of-notes melody to build a tiny device that lights up and plays a personalised jingle at power-on. The data/code split from L01-33 and L01-34 pays off twice over.