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
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | Buzzer | Pin 2 (+) to an output, pin 1 (−) to GND. tone(pin, hz) plays that pitch through your speakers. |
Wiring
| # | From | To |
|---|---|---|
| 1 | Arduino Uno pin 8 | Buzzer 2 |
| 2 | Arduino Uno GND | Buzzer 1 |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add a buzzer from the parts palette.
- Wire 1. Connect Arduino Uno pin 8 to Buzzer 2.
- Wire 2. Connect Arduino Uno GND to Buzzer 1.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- 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
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator