Want your project to beep when a sensor detects something, or play a tone when you press a button? You'll use a buzzer or a speaker. But there are two kinds of buzzer that look very much alike and are driven differently.
The active buzzer
- It has an oscillator inside: connect it to a DC voltage and it beeps continuously at a fixed tone.
- Driving it is very easy:
digitalWrite(8, HIGH); // beep
digitalWrite(8, LOW); // silent
- The drawback: only one tone.
- How to recognise it, usually: its back is closed (sealed), it's marked +, and it has one longer leg. And if you connect it directly to a battery it beeps.
The passive buzzer
- It has no oscillator: connect it to DC and it just "clicks".
- It needs a changing signal at the frequency you want, which lets you make tones and music:
tone(8, 1000); // a 1000 Hz tone
delay(500);
noTone(8);
- How to recognise it, usually: its back is open and you can see the board inside.
A simple tune
Each note has a frequency. The scale from do to the next do:
| Note | Do | Re | Mi | Fa | Sol | La | Ti | Do |
|---|---|---|---|---|---|---|---|---|
| Frequency (Hz) | 262 | 294 | 330 | 349 | 392 | 440 | 494 | 523 |
int notes[] = {262, 294, 330, 349, 392, 440, 494, 523};
void setup() {
for (int i = 0; i < 8; i++) {
tone(8, notes[i], 250); // the third argument: duration in ms
delay(300); // leave a small gap between notes
}
}
void loop() {}
When you give tone a duration, the tone stops by itself, but the program doesn't wait, which is why the delay follows it.
An alarm beep without delay
An intermittent beep (half a second on and half off) while the program keeps running, using millis:
bool alarm = true, on = false;
unsigned long tBeep = 0;
void loop() {
if (alarm && millis() - tBeep >= 500) {
tBeep = millis();
on = !on;
if (on) tone(8, 2500); else noTone(8);
}
}
And for a "rising and falling" siren, sweep the frequency between 1500 and 2500 every 20 ms instead of switching on and off.
Piezo and magnetic
- Piezo: a disc that flexes with voltage, draws very little current, and has a sharp sound.
- Magnetic: a small coil and diaphragm, drawing more current (perhaps 30 mA or more). It usually needs a transistor and a protection diode because it's a coil.
The transistor circuit
- The Arduino pin → a 1k resistor → the base of an NPN transistor (such as a 2N2222 or BC547).
- The emitter to ground.
- The buzzer between 5 V (or 12) and the collector, with a diode reversed across the buzzer if it's magnetic.
That way the buzzer takes the current it needs from the supply, and the pin provides only a small base current. And you can drive a much louder 12 V buzzer.
And there's a reverse trick: a piezo also produces a voltage when you tap it. Connect it to an analog pin with a 1 MΩ resistor in parallel, read it with analogRead, and you have a knock sensor.
The speaker
A small speaker (8 Ω, for example) has a very low resistance, so don't connect it directly to an Arduino pin: it'll draw a large current and can damage the pin. Use:
- A series resistor (about 100 Ω) for simple experiments and low volume.
- Or a transistor, or a small audio amplifier module (such as the PAM8403) for louder sound.
Why? 5 V across 8 Ω = 625 mA, and an Arduino pin safely carries 20. With 100 Ω in series the current is about 46 mA, still a little high, but fine for a short experiment. See Ohm's law.
Real audio (MP3)
tone produces only a square wave, meaning an "electronic" sound. If you want speech or real music (such as "door unlocked"), use a DFPlayer Mini module: it plays MP3 files from a microSD card, is controlled by the Arduino over Serial, and has a small amplifier that drives a 3 W speaker directly.
Volume
- The sound is loudest at the buzzer's resonant frequency (usually 2–4 kHz, stated in the datasheet).
- A higher voltage (with a transistor) = louder sound for magnetic and piezo buzzers.
- A series resistor lowers the volume if it's annoying.
- The enclosure matters: a buzzer inside a sealed box is quieter, and a small hole in front of it makes a big difference.
Tips
- The Arduino's
tone()uses a timer, which can conflict with PWM on certain pins (3 and 11 on the Uno). - On the Uno,
tone()drives only one pin at a time. - If you don't know the type: briefly connect it to 5 V. If it beeps continuously, it's active; if it clicks, it's passive.
- With an ESP32:
tone()works in recent versions of the core, or useledcWriteTone. See PWM.
FAQ
Can I play tunes with an active buzzer? Not well: you get its own tone mixed with the chopping. For any tune, use a passive one.
The buzzer sounds by itself when the Arduino starts? The pin floats for a moment at power-up, or starts HIGH. Add a 10k resistor from the pin (or the transistor's base) to ground.