Learn Arduino

Lesson 8 of 16 · 15 min

Arduino Buzzer: Play Notes with tone()

A passive piezo buzzer plays any frequency you feed it with tone(pin, hertz), while an active buzzer has its own oscillator and only beeps at one fixed pitch when powered.

This lesson also runs on an interactive 3D board: wire it, press Run and watch the circuit respond.

Open the 3D lesson

A piezo buzzer is a ceramic disc that clicks every time the voltage across it flips. Flip it 440 times a second and you hear the A above middle C. tone(pin, 440) does exactly that in hardware, leaving your sketch free to do other things.

The first thing to check is whether yours is active or passive. An active buzzer beeps at a fixed pitch the moment it gets 5 V; a passive one needs a signal. If tone() changes nothing and the buzzer hums at one note, you have an active one.

What you need

Everything below is in a standard Arduino starter kit.
PartQtyNotes
Arduino Uno R31Any digital pin drives a buzzer directly
Passive piezo buzzer112 mm, two legs, marked + and −
Breadboard + 2 jumpers1Optional: the legs fit the headers

How to wire it

Buzzer + → pin 8, buzzer − → GND. A piezo draws only a few milliamps, so no transistor or resistor is needed. For a louder magnetic buzzer, drive it through an NPN transistor.

Pin-by-pin wiring for this lesson.
PinConnects toWhat it does
D8Buzzer (+)Toggled at the note’s frequency.
GNDBuzzer (−)Return path.
Wiring map for Arduino Buzzer: D8 to Buzzer (+), GND to Buzzer (−).
Every connection this lesson needs, drawn from the pin table above.

Playing notes on an Arduino buzzer

tone(pin, hertz) generates a square wave in hardware, which is why the pitch stays steady while the sketch does other things. The catch is the timer behind it: on an Uno, tone() borrows Timer2, so analogWrite() on pins 3 and 11 stops working while a note is playing.

A melody is two arrays in the same order — frequencies and beat lengths — plus a loop that plays one note, waits, then moves on. Using the third argument of tone() to set the note length and then delaying slightly longer than that is what makes repeated notes sound like separate notes instead of one long drone.

Active buzzers, volume and drive current

An active buzzer has an oscillator inside and beeps at a fixed pitch the moment it gets power. Feed it tone() and nothing changes — the single most common “my melody does not work” report. A passive piezo is a bare disc and needs the signal, so check the label or test it across a battery.

A piezo draws a milliamp or two and a pin drives it directly. A magnetic buzzer pulls 30–40 mA, which is too much for comfort: it needs an NPN transistor with a base resistor, plus a diode across the coil if the part is inductive. Volume is not really adjustable on a piezo — a series resistor only makes it quieter and duller.

Frequency matters as much as volume. A 12 mm piezo is loudest somewhere around 2–4 kHz and nearly silent at 100 Hz, so a bass line will sound thin however hard you drive it. If a melody seems to vanish, play it an octave higher before you suspect the wiring.

The sketch

sound.ino
const int BUZZER = 8;

// note frequencies in hertz
const int C4 = 262, D4 = 294, E4 = 330, F4 = 349;
const int G4 = 392, A4 = 440, B4 = 494, C5 = 523;

const int melody[] = { C4, C4, G4, G4, A4, A4, G4 };
const int beats[]  = { 1,  1,  1,  1,  1,  1,  2  };
const int NOTE_MS = 300;  // one beat

void setup() {}

void loop() {
  for (int i = 0; i < 7; i++) {
    tone(BUZZER, melody[i], beats[i] * NOTE_MS);
    delay(beats[i] * NOTE_MS + 50);  // gap so notes separate
  }
  noTone(BUZZER);
  delay(1200);  // rest, then play it again
}

What the code does

  1. tone(pin, hz) starts a square wave; the third argument, in milliseconds, stops it automatically.
  2. 262 Hz is middle C, 294 D, 330 E, 349 F, 392 G, 440 A, 494 B, 523 the C above.
  3. delay(ms + 50) leaves a gap after every note so repeated notes are heard as separate.
  4. noTone(pin) stops the output immediately when you need silence in a hurry.

Specifications

Typical values for the parts used here.
ItemValue
BuzzerPassive piezo, 12 mm
Range≈ 100 Hz – 10 kHz
Middle C262 Hz
A above440 Hz
TipAn active buzzer only beeps at one pitch

Common mistakes

Sources

The figures in this lesson come from these documents. Every value in the tables above can be checked against them.

Frequently asked questions

What is the difference between an active and a passive buzzer?

An active buzzer contains its own oscillator and beeps at a fixed frequency as soon as it is powered. A passive buzzer is a bare piezo element and needs an AC signal — tone() — to make any sound.

Which pin should I use for a buzzer?

Any digital pin. Avoid 3 and 11 if you also need PWM on them, because tone() borrows the timer that drives those pins. Pin 8 or 9 is a good default.

Why does my buzzer only click?

You are toggling it too slowly, or the note length is shorter than a few milliseconds. A click is one edge; a note needs hundreds of edges per second, which tone() generates in hardware.

Can I play a melody without blocking the sketch?

Yes. Instead of delay() between notes, record millis() when a note starts, check the elapsed time each loop, and call tone() for the next note when it is due.

Want to see this circuit before you own the parts? The Arduino simulator draws this exact pin table on a 3D Uno R3: click any jumper to see what it carries, press Run and read the serial monitor.

Open the Arduino simulator

Published 12 November 2026 · last updated 19 November 2026 · written and tested by Nick Hu.