Piezo Buzzers
Overview
Use a passive piezo buzzer to produce a beep, then play a melody using arrays of frequencies and durations.
Builds on
Before You Start
Test the buzzer as its own circuit and disconnect the reflectance sensor from pin 2.
Materials
- Arduino Uno R3, USB cable, and Arduino IDE
- Passive piezo buzzer and male-to-male jumper wires
Safety
Disconnect power before rewiring
Unplug Arduino USB and disconnect or switch off every other supply before changing wires. Check polarity and connections before restoring power.
You've got it when…
- The buzzer plays the beep and a melody with audible notes, rests, and intended durations.
- The two arrays and loop bound match; zero-frequency entries are handled as rests.
- Your notebook contains the wiring table and complete final melody code.
Collaboration & AI
Compare observations and troubleshoot with classmates. Each student must understand the wiring or design and prepare their own submission.
AI — limited permission: You may use ChatGPT to suggest monophonic note lists, as described below. Check the notes yourself; write and debug the program without AI. This is a narrow exception to the usual no-AI coding rule. AI Assessment Scale
Piezo Buzzer Overview
A piezo buzzer uses a vibrating piezoelectric material to move air and produce sound. An active buzzer has its own oscillator and makes a fixed tone when powered. A passive buzzer needs a changing signal; its frequency controls the pitch, allowing melodies and sound effects. This lab uses a passive piezo buzzer.
Wiring
-
Disconnect power. Connect the passive piezo with male-to-male jumper wires using the table and the Arduino Pinout. The
+terminal is marked on top of the component. Use this as a separate circuit: the reflectance sensor also used pin2.Piezo pin Arduino pin + 2 − GND
Play a Beep
tone() accepts a pin number, a frequency in hertz (Hz) (cycles per second), and an optional duration in milliseconds. noTone(pin) stops sound on the specified pin.
-
Check your wiring, reconnect power, and upload this sketch. Listen for a 440 Hz beep lasting half a second, followed by half a second of silence. This is the sound equivalent of blinking an LED.
const int pinBuzzer = 2; void setup() { // pinMode() is not required for tone() } void loop() { tone(pinBuzzer, 440, 500); delay(500); noTone(pinBuzzer); delay(500); }A piezo has a resonant frequency: notes near that frequency tend to be louder. Keep the buzzer away from ears.
Program a Tune
-
Start a new sketch with the pin constant and empty
setup()andloop()functions. -
Above
setup(), add an array of note frequencies in playback order. In this program,0represents a silent rest; it must not be passed totone()as a frequency. -
Add a second array containing the duration of each note or rest in milliseconds. Both arrays must have the same number of items so that every note has a matching duration.
-
Add a constant for a silent gap between notes. A gap makes repeated notes easier to hear as separate notes.
-
Inside
setup(), create a for loop that visits each pair of array entries. The indexistarts at0and ends at8, covering nine items.Match the array length
The
9must match the number of entries in each array. A larger bound reads beyond the arrays and can produce unpredictable behavior. Update it if you change the number of notes. -
Add the playback logic so that positive frequencies play notes and zero entries stay silent. Wait for each note's duration, stop the sound, then wait for the gap. Your complete program should look like this:
const int pinBuzzer = 2; const int melodyNotes[] = {659, 659, 0, 659, 0, 523, 659, 0, 784}; const int noteDurations[] = {150, 150, 150, 150, 150, 150, 150, 150, 200}; const int pauseBetweenNotes = 150; void setup() { for (int i = 0; i < 9; i++) { if (melodyNotes[i] > 0) { tone(pinBuzzer, melodyNotes[i], noteDurations[i]); } else { noTone(pinBuzzer); // A zero in the array represents a rest. } delay(noteDurations[i]); noTone(pinBuzzer); delay(pauseBetweenNotes); } } void loop() { // The melody plays once in setup(). }melodyNotes[i]selects a frequency;noteDurations[i]selects its matching duration.tone()starts sound without waiting, so the duration delay is needed before stopping it. -
Upload and verify the notes, rests, and final longer note. The melody plays once because it is in
setup(). Press the board's reset button to replay it. To repeat continuously, you could move the playback loop intoloop(). -
Explore note names and frequencies with a virtual piano or a piano with frequencies. You may ask ChatGPT for a monophonic note list for a familiar song, then check the notes yourself. Trying another tune is optional; the supplied melody satisfies the lab.
Combining this with other robot functions
On the Uno R3,
tone()interferes with PWM on pins3and11. The motor-driver lab uses pin11, so test this buzzer separately before planning a combined pin/timer layout. Arduino tone reference -
Copy the wiring table and complete final melody code into your notebook.
Turn It In
- Submit the following notebook entry:
- Notebook entry: wiring table and complete final melody code
How It's Graded
One score from 0–4 covers the required work and evidence. Verification may be shown during the lab; the required uploads are listed above.
| Score | Evidence |
|---|---|
| 4 Excellent | The beep and melody are verified; rests and durations work, array lengths and loop bounds agree, and the wiring table and complete final melody code are included. |
| 3 Above Average | The melody, final code, and wiring table are complete and working, with only minor errors or omissions in clarity or verification. |
| 2 Average | The melody, final code, and wiring table have significant gaps, errors, or missing evidence. |
| 1 Below Average | The required work is largely incomplete or nonfunctional, with little usable evidence. |
| 0 Failing | No submission or no evidence of the required work. |
Converted from the original lab.