Arduino Nano · Buzzer

Arduino Nano Buzzer Simulator: Wiring, Code and a Live Circuit

This is a passive piezo buzzer wired to an Arduino Nano, playing a four-note arpeggio with tone() live in your browser. The simulated piezo measures the frequency on its legs, so a wrong note is a wrong number.

Two wires: pin 8 to the buzzer’s plus side and GND to the other. No library is needed.

  • 5 V logic
  • No library: tone()
  • Signal on pin 8
  • Passive piezo
Arduino Nano · Buzzerlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.

Wiring the Buzzer to the Arduino Nano

Every connection in the circuit above, with the board’s own pin names.

Buzzer pinArduino Nano pinWhy
Pin 1pin 8tone() puts a square wave at the note’s frequency on this pin.
Pin 2GNDGround. A passive piezo has no polarity.

A small passive piezo draws a few milliamps and runs straight from the pin. On the ATmega328P, tone() takes Timer2, so PWM on pins 3 and 11 pauses while a note plays.

Pins to leave alone on the Arduino Nano

A6 and A7 are analog only: pinMode(A6, OUTPUT) does nothing and digitalRead(A7) is not there to call. D0 and D1 are the USB serial port.

On a real bench

The Nano plugs into the breadboard itself, straddling the center channel, so the Buzzer goes in a few columns along and every connection is a short jumper. The silkscreen labels its pins D2 to D13 and A0 to A7; in code D2 is simply 2. A Nano clone with a CH340 USB chip needs that driver on a real computer.

The code

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

Tones on Arduino Nano pin 8
const int BUZZER_PIN = 8;   // pin 8

// C, E, G, C: an arpeggio, a quarter second a note.
const int NOTES[] = {523, 659, 784, 1047};

void setup() {
  Serial.begin(9600);
  Serial.println("Playing");
}

void loop() {
  for (int i = 0; i < 4; i++) {
    tone(BUZZER_PIN, NOTES[i], 200);   // returns at once; the note plays on
    delay(250);
  }
  noTone(BUZZER_PIN);
  Serial.println("Again");
  delay(500);
}

tone(pin, frequency, duration) starts a square wave and returns at once; the note stops on its own after duration milliseconds. That is why the loop still needs delay(250): without it all four notes would start within microseconds of each other.

The frequencies are the notes of a C major arpeggio, from 523 Hz (C5) to 1047 Hz (C6). #include "pitches.h" gives names like NOTE_C5 for the same numbers.

Serial.begin(9600) opens the serial monitor. Serial is the USB port, as on the Uno; a clone with a CH340 chip needs that driver on a real computer, which the simulator does not.

Common mistakes with the Buzzer on the Arduino Nano

An active buzzer

An active buzzer has its own oscillator and beeps at one pitch whenever it has power, so tone() cannot change its note. For melodies you need a passive one; the two look the same, but the active kind usually has a sticker on top.

No delay between notes

tone() does not wait for the note to finish. Call it twice in a row and only the second note plays. Wait at least as long as the note, as the sketch does.

Picking A6 or A7

A6 and A7 look like the other analog pins but are inputs to the ADC and nothing else. the buzzer on either of them never sees a digital level. Any of D2 to D13 or A0 to A5 works.

Questions and answers

Can I simulate a buzzer with an Arduino Nano?

Yes. The circuit at the top of this page is an Arduino Nano with a piezo buzzer, running in your browser on a simulated ATmega328P. The simulated piezo reports the frequency it is driven at, and the editor’s scope can show the square wave on the pin. Press Run it in the editor to change the wiring or the code; it is free and needs no account.

Can I play two notes at once?

Not with tone(): it drives one pin at a time. Two buzzers on two pins with your own timing can, or a DFPlayer module for recorded sound.

How do I make it louder?

Drive the piezo from two pins in opposite phase, or through a transistor from a higher voltage. A magnetic buzzer is louder but needs the transistor.

Where do I find the note frequencies?

#include "pitches.h" in the editor gives NOTE_ names from NOTE_B0 (31 Hz) to NOTE_DS8 (4978 Hz), the same list the Arduino toneMelody example uses.

Build your own Buzzer project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.