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
Wei Jie 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 |
|---|---|---|
Arduino D8 | Breadboard A12 | Yellow (signal) |
| Piezo leg 1 (left) | Breadboard B12 | (component) |
| Piezo leg 2 (right) | Breadboard B13 | (component) |
Breadboard C13 | − rail (via short jumper) | Black |
| − rail | Arduino GND | Black |
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
D8 goes HIGH-LOW-HIGH-LOW at 440 Hz, the yellow wire carries that flicker into the breadboard at A12. Column 12's tie strip passes it to the buzzer's left leg at B12. The crystal flexes back and forth 440 times per second; the air gets pushed; you hear an A. The current exits the right leg at B13, drops via the short black jumper at C13 onto the − rail, and returns to the Arduino's GND.The same circuit as a schematic
Take a moment to read the abstract view of what you just built. Only two component-like symbols sit between D8 at the top and GND at the bottom — and one of them is the piezo itself, drawn as the standard "speaker trapezoid".
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.
Try It Yourself 20 min
Three small mutations. For each one, predict on paper whether the note will sound higher or lower than the worked example, then upload to check.
Goal: Change the frequency to play a different note. Try musical C5 (523 Hz) — one octave above middle C.
const int BUZZER_PIN = 8;
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
tone(BUZZER_PIN, 523, 500);
delay(1000);
}Questions:
- Compared to the 440 Hz A from the Worked Example, does C5 sound higher or lower? ____
- How could you tell from the numbers alone, before uploading? ____
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.
const int BUZZER_PIN = 8;
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
tone(BUZZER_PIN, 100, 200); delay(220);
tone(BUZZER_PIN, 800, 200); delay(220);
tone(BUZZER_PIN, 2000, 200); delay(220);
delay(500);
}Questions:
- The three notes jump in big steps. Does it sound like a smooth siren or like three separate beeps? Why? ____
- Why is the delay
220ms and not200ms — the same as the tone's duration? (Hint: would you want the next tone to start during the previous one?) ____
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.
const int BUZZER_PIN = 8;
const int NOTE_C4 = 262;
const int NOTE_D4 = 294;
const int NOTE_E4 = 330;
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
tone(BUZZER_PIN, NOTE_C4, 300); delay(350);
tone(BUZZER_PIN, NOTE_D4, 300); delay(350);
tone(BUZZER_PIN, NOTE_E4, 300); delay(350);
delay(1000);
}Questions:
- 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? ____ (Hint: how many places does262,294or330appear?) - Compare to the 🟡 task. Which version is easier to read at a glance — the one with raw numbers (
100,800,2000) or this one with note names? Why? ____
Mini-Challenge 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.
Your task:
- At the top of the sketch, declare named constants for
NOTE_C4,NOTE_G4andNOTE_A4with their frequencies. - Write a
beep(frequencyHz, durationMs)helper that callstone()with the right pin, waits the duration, then waits a tiny extra gap (50 ms) so neighbouring notes don't blur together. - In
loop(), callbeep()seven times to play the phrase, with the last note twice the length of the first six. - End the loop with a 1-second pause before the phrase repeats.
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.
Reveal one valid sketch
const int BUZZER_PIN = 8;
const int NOTE_C4 = 262;
const int NOTE_G4 = 392;
const int NOTE_A4 = 440;
void beep(int frequencyHz, int durationMs) {
tone(BUZZER_PIN, frequencyHz, durationMs);
delay(durationMs + 50);
}
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
beep(NOTE_C4, 300);
beep(NOTE_C4, 300);
beep(NOTE_G4, 300);
beep(NOTE_G4, 300);
beep(NOTE_A4, 300);
beep(NOTE_A4, 300);
beep(NOTE_G4, 600);
delay(1000);
}The whole sketch uses every Cluster-B idea: named constants (L01-09), a helper function (L01-12), and a new built-in (tone) called consistently from one place. Notice how the loop() body reads almost like sheet music — "C, C, G, G, A, A, G". That's the gift of giving things names.
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.
Homework 5 min
Build a doorbell. Write a sketch that plays a classic two-note doorbell sound — "ding-dong" — once every 4 seconds. The "ding" is higher than the "dong".
- Pick two frequencies. A common doorbell uses about E5 (659 Hz) for the ding and C5 (523 Hz) for the dong. Or pick your own.
- Give each frequency a named constant at the top of the sketch.
- Write a
beep()helper like the one from class. - In
loop(), beep the ding (≈ 400 ms), beep the dong (≈ 600 ms), thendelay(4000)before the next ring.
Also: a reflection question on paper.
- Look at your kit's piezo. Now look at the speaker on a phone or laptop. Both make sound. What is the same about how they work, and what is different? (Hint: think about size, range of frequencies, and what each is good at.) ____
Bring back next class:
- The saved
.inofile (call itdoorbell). - A short phone video (10–15 seconds) with the audio on so we can hear the ding-dong.
- Your reflection answer on a notebook page.
Heads up for next class: in L01-15 "Musical Doorbell" we re-plug the three LEDs from L01-10 alongside today's buzzer, add a push button, and combine all of it into a project that flashes lights and plays a tune when someone presses the button. Keep your kit handy.