Learning Goals 5 min
By the end of this lesson you will be able to:
- Wire a piezo buzzer to digital pin
D8andGNDon the breadboard — your first component that needs no current-limiting resistor. - Use
tone(pin, frequencyHz, durationMs)to make the buzzer play a single note for a given length of time, andnoTone(pin)to silence it on demand. - Pick a frequency in hertz and predict whether the note will sound higher or lower than the previous one — and explain the rule in one sentence.
Warm-Up 10 min
For thirteen lessons everything you've built has communicated through light — onboard LED, external LEDs, three LEDs in a row. Today your circuit gets a voice.
Quick-fire puzzle
Leo names three everyday beeps:
- the high-pitched "tweet" of a smoke alarm
- the cheerful "ding" of a microwave timer
- the low "thunk" of a refrigerator door warning
- What is physically moving inside each of these devices to make the air carry the sound to your ear?
- Why does the smoke alarm sound higher than the fridge warning? What is the physical difference between the two sounds?
- If you could control how fast that physical thing moves, what would you have control over?
Reveal the answer
- A small piece of piezoelectric crystal (or a speaker cone) is being made to flex back and forth very rapidly by an electrical pulse. The flexing pushes the air, which travels to your eardrum as sound.
- The smoke alarm's crystal flexes faster than the fridge's — more flexes per second means more sound waves per second arriving at your ear, which you hear as a higher pitch.
- You control the pitch — the highness or lowness of the note. In code we set that with a number, and the unit is hertz (Hz).
Today we use a piezo buzzer and control the pitch by changing one number in our sketch.
New Concept 20 min
The big idea — make a pin flip on and off fast enough to be heard
From L01-08 you know that digitalWrite can set a pin to HIGH or LOW. Now imagine doing that 440 times per second — every 2.27 ms the pin flips. Connect that pin to a piezo crystal and the crystal flexes back and forth 440 times per second. Air gets pushed 440 times per second. Your ear hears musical A — the same note an orchestra tunes to.
Doing this by hand with digitalWrite and tiny delays is painful. Arduino gives you one built-in function that does the flipping for you — tone().
The piezo buzzer
The piezo buzzer in your kit is a small black disc with two legs sticking out the bottom. It has:
- Two pins, like an LED — one for the signal, one for GND.
- No required polarity for most small piezos in starter kits — they work either way round. (Larger industrial ones have a
+mark; respect it if present.) - No current-limiting resistor needed. Piezos are "capacitive" — they barely draw any current at all. Wire them directly to the pin.
Frequency and pitch
- Frequency is the number of times something repeats per second. Measured in hertz (Hz).
- 1 Hz = once per second (like a slow heartbeat).
- 1 000 Hz (1 kHz) = a thousand times per second (an alarm clock).
- Human hearing covers about 20 Hz to 20 000 Hz. Below or above that range and you can't hear it at all.
- Higher frequency = higher pitch. 1000 Hz is shrill; 200 Hz is a low rumble.
A handful of musical-note frequencies
Musical notes have standard frequencies you can call by name. The most useful for today:
| Note | Frequency (Hz) | Where you'd hear it |
|---|---|---|
| Middle C (C4) | 262 | Centre of a piano keyboard |
| D4 | 294 | Just above middle C |
| E4 | 330 | The "mi" of do-re-mi |
| F4 | 349 | "fa" |
| G4 | 392 | "so" |
| A4 | 440 | "la" — the orchestra tuning note |
| B4 | 494 | "ti" |
| C5 | 523 | The "do" one octave above middle C |
The new functions
| Function | What it does |
|---|---|
tone(pin, frequencyHz) | Start a tone at the given frequency on the given pin. Keeps playing forever until you call noTone(). |
tone(pin, frequencyHz, durationMs) | Same as above but auto-stops after durationMs milliseconds. This is the form we'll mostly use. |
noTone(pin) | Stop any tone currently playing on the pin. Useful when you didn't pass a duration. |
Important: tone() is non-blocking
tone(8, 440, 500) starts the tone and immediately returns. The chip plays in the background and silences itself 500 ms later. If you want your sketch to wait while the note plays, you must follow up with a delay().
tone(8, 440, 500); // Start A4 for 500 ms. Returns immediately.
delay(500); // Wait 500 ms so the note finishes playing.Today's wiring — just the buzzer, no LEDs
To keep today's focus on the new component, we temporarily unplug the three LEDs from L01-10. Just the piezo buzzer on pin 8.
| From | To | Wire colour |
|---|---|---|
| Piezo leg 1 (left) | Breadboard C12 | (component) |
| Piezo leg 2 (right) | Breadboard C13 | (component) |
Arduino D8 | Breadboard E12 | Yellow (signal) |
Breadboard E13 | − rail | Black (return) |
| − rail | Arduino GND | Black |
The piezo sits in row C and both jumpers plug into row E. That is deliberate: all five holes of a column are one strip, so a jumper in row E reaches the piezo leg in row C without a wire ever crossing the component. You will see the same three-row habit — part high, wires low — in every build from here on.
Why it matters
Once you can make sound, your circuits can communicate: alarms, alerts, melodies, error tones. Every doorbell, microwave, oven timer, smoke alarm, mobile phone notification — they all go back to a pin and a tone() call.
Worked Example 20 min
Goal: build the buzzer circuit, then write a sketch that beeps musical A (440 Hz) once a second.
The wiring, and the same circuit on paper
Both drawings below are this one circuit. The left one tells you which holes to use; the right one tells you what is actually connected to what. Hover the piezo in either picture and it lights up in both.
On the breadboard what you build
On paper what engineers draw
D8 flips HIGH-LOW 440 times a second, that flicker runs down the yellow wire into column 12's tie strip and through the piezo. The crystal flexes 440 times a second, the air gets pushed, and you hear an A. The current leaves the other leg at column 13, drops onto the − rail, and returns to GND. The schematic says exactly the same thing in four marks: pin, piezo, wire, ground — and no resistor, because a piezo does not need one.The sketch
const int BUZZER_PIN = 8;
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
tone(BUZZER_PIN, 440, 500);
delay(1000);
}Expected behaviour
- Beep at musical A (440 Hz) for half a second.
- Silence for half a second.
- Repeat forever.
If the buzzer is completely silent, check that one of its legs is at B12 (signal column) and the other at B13 (GND column). On small piezos polarity rarely matters, but a leg in the wrong column means no circuit.
Why is delay(1000) after a tone(…, …, 500)?
The third argument tells the chip when to stop the tone, but it does not pause the sketch. If we wrote tone(BUZZER_PIN, 440, 500); tone(BUZZER_PIN, 880, 500); with nothing in between, the second call would immediately overwrite the first and we'd never hear the A. The delay after each tone is what makes the sketch wait for the note to finish.
Basic 7 min
Three small mutations of the Worked Example sketch. For each one, predict on paper what you will hear, then upload to check.
Goal: Change the frequency to play a different note. Try musical C5 (523 Hz) — one octave above middle C.
1. Think first: tone() takes three numbers. Which one is the pitch? Is 523 bigger or smaller than the number there now — so will the note go up or down? Write your prediction down.
2. Code it. Open the Worked Example sketch and change one number.
3. Upload and observe. Listen, then compare with your prediction.
It works if: the beep is the same length as before, but a clearly different (higher) note.
Questions:
- Which number did you change? How did you know it was the frequency and not the duration? ____
- Compared to the 440 Hz A, does C5 sound higher or lower? Was your prediction right? ____
- How could you tell from the numbers alone, before uploading? ____
Challenge 1 7 min
Goal: Sweep from a very low note (100 Hz) to a very high one (2000 Hz) by writing three tone() calls in a row, with a short delay between each. A do-it-yourself siren.
1. Think first (on paper, no typing yet):
- Pick your middle note. Write the three frequencies in order.
- Each note is 200 ms. Does
tone()wait for its note to finish before the next line runs? (Re-read the Worked Example.) So what must come after eachtone()? - How will a listener hear where one sweep ends and the next begins?
2. Code it. Start from the Worked Example sketch. Keep setup(); rewrite loop() from your plan.
3. Upload and observe. Listen for three separate notes. If you only hear the last one, look at your waits.
It works if: you hear three separate notes, low → middle → high, each one finishing before the next starts.
Questions:
- First try a
delay()that is shorter than 200 after each tone. What happens to the notes? ____ - What delay did you settle on after each tone? Why is a little more than 200 better than exactly 200? ____
- The three notes jump in big steps. Does it sound like a smooth siren or like three separate beeps? Why? ____
Challenge 2 6 min
Goal: Play a 3-note do-re-mi sequence (C4 → D4 → E4) by giving each pitch a named constant — bringing the L01-09 single-source-of-truth idea to notes.
The frequencies you need: C4 = 262 Hz, D4 = 294 Hz, E4 = 330 Hz.
1. Think first (on paper, no typing yet):
- How is
BUZZER_PINgiven its name in the Worked Example? Write the same kind of line for each of the three notes. (Use ALL_CAPS names.) - Inside
loop(), where does the name go instead of the number? - Each note is 300 ms, then a longer rest before it repeats. Write the order of notes and waits in plain English.
2. Code it. Start from the Worked Example sketch.
3. Upload and observe. Sing along: do, re, mi.
It works if:
- You hear do, re, mi — three rising notes — then a rest.
- The numbers
262,294and330each appear only once in your sketch.
Questions:
- What names did you choose? Where in the sketch did you put them, and why there? ____
- If your music teacher decided that "concert pitch" was now
A = 442 Hzinstead of440 Hz, how many lines of your sketch would you need to edit? ____ - Compare to your 🟡 siren. Which is easier to read at a glance — raw numbers or note names? Why? ____
Challenge 3 15 min
Play the opening of "Twinkle Twinkle Little Star"
Use the named-note approach from the 🔴 stretch task and a beep() helper from L01-12 to play the recognisable first phrase: do do so so la la so — that's C C G G A A G in note-name form. The last G is held twice as long as the rest.
The notes you need: C4 = 262 Hz, G4 = 392 Hz, A4 = 440 Hz. Your sketch needs a helper called beep that is told a frequency and a length, and plays that note and leaves a tiny gap after it, so neighbouring notes don't blur together. After the phrase, rest for a second before it repeats.
1. Think first (on paper):
- Which three notes does the tune use? How will you name them?
beepneeds to be told two things. What are they? Which parameter order will you choose?- Inside
beep:tone()doesn't wait for the note. What mustbeepdo after starting the note, so the next note doesn't cut it off — and how long is that, including the small gap? - Write the seven notes as seven lines of plain English. Which one is longer?
2. Code it. Build it bottom-up: constants, then beep (test it with one note), then the phrase.
3. Upload and observe. Play it to a classmate without telling them the name of the tune.
It works if:
- A classmate can recognise the tune from the first three notes.
- Each note sits cleanly on its own — no slurring into the next one.
- Your
loop()body uses onlybeep()anddelay(). No rawtone()ornoTone()calls.
Recap 5 min
A piezo buzzer turns electrical pulses into vibration, and vibration into sound. tone(pin, frequencyHz, durationMs) tells the Arduino to flip a pin on and off at a given frequency for a given length of time. Higher frequency = higher pitch. noTone(pin) stops a tone that's still playing. The hardware does the fast-flipping; you only write the music.
- Piezo buzzer
- A small disc-shaped component containing a piezoelectric crystal. The crystal flexes when voltage is applied; pulsing the voltage rapidly makes it vibrate audibly. Two pins; no current-limiting resistor needed.
- Frequency
- How many times a signal repeats per second. Measured in hertz (Hz). 1000 Hz = a thousand times per second.
- Pitch
- How high or low a sound is, as perceived by the ear. Higher frequency = higher pitch.
- tone(pin, frequencyHz, durationMs)
- Play a square-wave tone at
frequencyHzonpinfordurationMsmilliseconds. Non-blocking — pair it with adelay()if you want the sketch to wait. - noTone(pin)
- Stop any tone currently playing on the pin.
- Hertz (Hz)
- The unit of frequency — once per second. Named after Heinrich Hertz, the physicist who proved electromagnetic waves exist.
Extra Mission 5 min
Part 1 — Design a device's sound set
Devices talk with beeps. A microwave beeps when it is done. A game console plays a sound when it starts. Pick a device, real or invented. On paper, design its sound set.
Your design must include:
- The device and its one job.
- At least three sounds, such as "start", "done" and "error".
- For each sound: its notes as frequencies in hertz, and each note's length in milliseconds.
- A rule that makes the sounds easy to tell apart, such as "errors are low, good news goes up".
Part 2 — Make it
Build your sound set on today's buzzer circuit (piezo on pin 8).
- Give every frequency a named constant, such as
NOTE_E5. - Write a
beep(frequencyHz, durationMs)helper like the one from class. - Write one function per sound, such as
soundDone(), built only frombeep()calls. - In
loop(), play each sound in turn with a 2-second pause between them.
Bring back next class: your sound-set design, the saved .ino file, and a short video with the sound on.
Heads up for next class: in L01-15 "Musical Doorbell" the three LEDs from L01-10 join today's buzzer in one project. Keep your kit handy.