Piezo speaker
Makes whatever tone you drive it with, and shows it.
- 2 pins
- In 51 projects
- In 21 lessons
The Piezo speaker in brief
What it is, and where it sits on the bench.
A passive piezo element is a ceramic disc on a brass plate with two legs and no oscillator inside; that is what separates it from the near-identical active buzzer. A steady voltage does nothing; it sings whatever square wave it is given, so one leg goes to a digital pin and the other to ground, and tone() on an Uno or ESP32 picks the note, 440 Hz for an A. Mokxi’s part is a frequency counter on the voltage across its legs, counting a rise at 1.0 volt and a release at 0.4 volts so a slow or noisy edge cannot count twice, and it reports the note in hertz. Anything faster than 40 kHz is treated as noise, and 60 milliseconds without an edge reads as silence, so a very short blip never gets a frequency. The common mistake is wiring it like a buzzer, giving it plain DC and wondering why it is quiet. The disc’s roughly 10 nF of capacitance, its resonance near 4 kHz and its loudness are not modeled. You hear the note it plays through your computer’s speakers, as a soft square wave at that frequency.
- Family
- Outputs
- Pins
- 2
2 pins
Named as the editor names them, so a wire lands where you expect it.
How to use a passive piezo with an Arduino
One leg goes to a digital pin and the other to GND, with no resistor. Then tone(pin, frequency) starts a square wave on the pin and returns at once, and noTone(pin) stops it, so the sketch decides how long each note lasts with its own delay(). Without tone(), a passive piezo on a pin held HIGH stays silent.
The circuit here plays the opening of Ode to Joy on pin 8, lights an LED on pin 13 for every note, and prints each pitch on the serial monitor. The 30 ms gap after every noTone() is what makes two notes of the same pitch sound like two notes rather than one long one, and the piezo’s readout shows the frequency it hears.
tone() compiles for the ESP32 here too, and a 3.3 V square wave crosses the piezo’s 1 V threshold with room to spare.
const int PIEZO = 8;
const unsigned int NOTES[] = {330, 330, 349, 392, 392, 349, 330, 294};
void setup() {}
void loop() {
for (int i = 0; i < 8; i++) {
tone(PIEZO, NOTES[i]);
delay(370);
noTone(PIEZO);
delay(30); // the gap between notes
}
delay(2000);
}What is true about the Piezo speaker, here
In its help article’s own words: what the simulation gets right, and where it stops.
Modeled
The part watches the voltage across its two legs and times the transitions, with hysteresis so a slow edge or a noisy net cannot count twice; the gap between one rise and the next is the note's period, and the probe reports its frequency in hertz. Driven differentially, as an H-bridge would, it reads the same way, because what it is watching is the difference between the legs. Silence for long enough reads back as 0 Hz.
Not modeled
No capacitance (a real element is roughly 10 nF and loads the pin driving it; this one is high impedance on both legs) and no loudness. The probe is a frequency, not a volume. You hear the note it plays through your computer's speakers, as a square wave at that frequency and a fixed soft level (the speaker button on the canvas toolbar mutes it).
It is a frequency counter rather than an acoustic model. The hysteresis is 1.0 V to go one way and 0.4 V to come back, which are round numbers rather than a real element's thresholds, and they are absolute volts: a 3.3 V board's square wave crosses them and a 200 mV signal never does, however loud a real disc would be. Anything faster than 40 kHz is rejected as noise rather than reported, and 60 ms with no edge reads as silence, so a note shorter than that never gets a frequency at all. There is no resonance (a real disc is loudest near 4 kHz and nearly silent an octave away), no mechanical ring-down, no harmonics and no waveform: a sine and a square at the same rate read identically.
From Piezo element (passive), in full.
See the Piezo speaker in a project
51 built-in projects use it. Each opens in the editor, already wired.
Where it turns up in a lesson
21 lessons place it in their own circuits.
Learn articles
20 lessons
- The 555 Astable: Timed by a Capacitor, Not a Crystal
- Arduino Buzzer and Piezo: Beeps, Notes, Metronome
- Arduino Science Fair Projects With a Real Hypothesis
- Reaction Time Science Fair Project With an Arduino
- Arduino while Loop: Wait, Repeat, Break Out
- Arduino enum: Named States, enum class and Switch
- Arduino Model Railroad: Turnouts, Crossings, Signals
- Arduino ILI9341 TFT Display: Wiring and Code
- Arduino Tone Melody: Play a Song on a Piezo
- Arduino TM1637 Display: Library and Countdown
- Arduino DS1302 RTC: an Alarm Clock With Code
- Arduino MLX90614: Non-Contact IR Thermometer
- Arduino MPR121: 12-Key Capacitive Touch Piano
- Free Arduino Simulator for Students, in the Browser
- Arduino Practical Exam Practice: Ten Timed Tasks
- Arduino Holiday Projects for Every Celebration
- Arduino Whack-a-Mole Game: LEDs, Buttons, Score
- Arduino Game Projects You Can Play in Your Browser
- STEM Club Ideas: Electronics and Coding Sessions
- Wokwi Alternatives for Arduino and ESP32, Compared
The boards it is shown on
Each of these 3 boards has a built-in project with the Piezo speaker in it.
The Piezo speaker on each board
The pins, the voltage and the code change from board to board. Each page has that board’s wiring and a circuit you can run.
The rest of the bench
Every one of these is drawn and simulated the same way.
Wire up the Piezo speaker
Open the editor and push it into the breadboard. It is free, and it runs on your own machine.