Adding sound to a project — an alert tone, a simple melody, a confirmation beep — is one line of code away with a passive buzzer and Arduino's built-in tone() function.
Beginner
⌛ 15 min
🔧 What You'll Need
- Arduino Uno
- 1x passive buzzer
- Breadboard
- 2x jumper wires
Wiring the Circuit
Connect the buzzer's positive leg (usually marked with a + or a longer pin) to digital pin 8, and the negative leg to GND. No resistor is needed for a passive buzzer used with tone().
The Code
const int buzzerPin = 8;
// A simple ascending alert tone
int notes[] = {262, 294, 330, 349, 392}; // C4 to G4
void setup() {
pinMode(buzzerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < 5; i++) {
tone(buzzerPin, notes[i]);
delay(200);
}
noTone(buzzerPin);
delay(1500);
}
tone(pin, frequency) generates a square wave at the given frequency (in Hz) on the specified pin for as long as it's left running — the buzzer converts that electrical oscillation into sound. The notes[] array holds five musical note frequencies, and the loop plays through them in sequence before calling noTone() to silence the buzzer and pausing before repeating.
💡 Tip: Passive buzzers (used here) can play different pitches viatone(). Active buzzers only produce one fixed tone and just needdigitalWrite(HIGH)— check which type you have before wiring, since the code and behavior differ.
Troubleshooting
- No sound at all: Confirm you have a passive buzzer if using
tone()— an active buzzer needsdigitalWrite()instead and won't respond to frequency changes. - Buzzer makes a constant tone, ignoring the code: This is the signature of an active buzzer being driven with
tone()— it can't vary pitch, so it just buzzes at whatever it's built for. - Sound is very quiet: Normal for small buzzers at low voltage — this is expected behavior, not a fault.
Sound is a simple but effective way to give a project feedback without needing a screen. Combined with the sensors from earlier tutorials, this is exactly how you'd build a basic alert system — which is where this series is headed next.
Menu