Arduino Buzzer Melody: Wiring, Code and Simulator

To build this Arduino Buzzer Melody project you need an Arduino Uno and a buzzer. tone() plays a scale on a buzzer on pin 8. You hear it through your speakers. Make the 2 connections in the wiring table, upload the Arduino C++ code, and turn your sound on. tone(8, 440) toggles pin 8 880 times a second: a 440 Hz square wave.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
1BuzzerPin 2 (+) to an output, pin 1 (−) to GND. tone(pin, hz) plays that pitch through your speakers.

Wiring

#FromTo
1Arduino Uno pin 8Buzzer 2
2Arduino Uno GNDBuzzer 1

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add a buzzer from the parts palette.
  3. Wire 1. Connect Arduino Uno pin 8 to Buzzer 2.
  4. Wire 2. Connect Arduino Uno GND to Buzzer 1.
  5. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  6. Run it. Press Run. Turn your sound on. tone(8, 440) toggles pin 8 880 times a second: a 440 Hz square wave.

Arduino C++ code

// Melody: tone() plays notes on a buzzer on pin 8. Turn your sound on.
const int BUZZER = 8;
const int notes[] = {262, 294, 330, 349, 392, 440, 494, 523};

void setup() {
  Serial.begin(9600);
}

void loop() {
  for (int i = 0; i < 8; i++) {
    Serial.print("Note: ");
    Serial.print(notes[i]);
    Serial.println(" Hz");
    tone(BUZZER, notes[i]);
    delay(300);
  }
  noTone(BUZZER);
  delay(1000);
}

What to try

Turn your sound on. tone(8, 440) toggles pin 8 880 times a second: a 440 Hz square wave.

Frequently asked questions

How do I wire the Buzzer Melody project on an Arduino Uno?

Make 2 connections: Arduino Uno pin 8 to Buzzer 2; Arduino Uno GND to Buzzer 1.

What parts do I need for this Arduino project?

An Arduino Uno and a buzzer.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

tone() plays a scale on a buzzer on pin 8. You hear it through your speakers.

More Arduino Uno projects