As an Amazon Associate I earn from qualifying purchases. This article contains affiliate links.
In this tutorial, you will learn how to play music with an Arduino Uno and a passive buzzer. This project uses frequency and timing to generate a melody without a music library. You will find the wiring, sample Arduino code, and required parts below.
Here's a sample video.
Table of Contents
Parts Needed for Arduino Buzzer Music
To build this project, you need an Arduino Uno, a passive buzzer, a push button, jumper wires, and a breadboard.
Get Arduino Uno for This Project
Get the Arduino Starter Kit for This Project
Arduino Buzzer Music Code
int buzz = 7;
int button = 5;
void setup() {
pinMode(buzz,OUTPUT);
pinMode(button,INPUT_PULLUP);
}
void loop() {
if(digitalRead(button)==LOW){
unsigned char i;
double E3 = 329.628;
double C3 = 261.626;
double G3 = 391.995;
double G2 = 195.998;
double A2 = 220.000;
double B2 = 246.942;
double As2 = 233.082; // A#2
double E2 = 164.814;
double A3 = 440.000;
double F3 = 349.228;
double D3 = 293.665;
double Ds2 = 155.563; // D#2
double C2 = 130.813;
double D2 = 146.832;
double As3 = 466.164; // A#3
double Cs3 = 277.183; // C#3
double Cs4 = 554.365; // C#4
double F2 = 174.614;
double C4 = 523.251;
double D4 = 587.330;
double Fs3 = 369.994; // F#3
double B3 = 493.883;
double F4 = 698.456;
double E4 = 659.255;
double Ds3 = 311.127; // D#3
double len = 1.277;
double len2 = 0.638;
double len4 = 0.319;
double len8 = 0.160;
double len16 = 0.080;
cal(D3, len8);
cal(D3, len8);
cal(A3, len4);
cal(D3, len8);
cal(D3, len8);
cal(As3, len4);
cal(D3, len8);
cal(D3, len8);
cal(A3, len4);
cal(D3, len8);
cal(D3, len8);
cal(Cs3, len4);
cal(D3, len8);
cal(D3, len8);
cal(A3, len4);
cal(D3, len8);
cal(D3, len8);
cal(Cs4, len4);
cal(A3, len2);
cal(A2, len2);
cal(F3, len2);
cal(F2, len2);
cal(D3, len8);
cal(D3, len8);
cal(A3, len4);
cal(D3, len8);
cal(D3, len8);
cal(As3, len4);
cal(D3, len8);
cal(D3, len8);
cal(A3, len4);
cal(D3, len8);
cal(D3, len8);
cal(Cs4, len4);
cal(D4, len);
len8 = 0.150;
cal(D4, len8);
delay(len8 * 1000 + len2 * 1000);
len = 1.805;
len2 = 0.902;
len4 = 0.451;
len8 = 0.226;
len16 = 0.113;
cal(G2, len16);
cal(B2, len16);
cal(D3, len16);
cal(Fs3, len16);
cal(G3, len8);
delay(len8 * 1000);
cal(G3, len8);
delay(len8 * 1000);
delay(len8 * 1000);
cal(G3, len16);
cal(G3, len16);
cal(G3, len8);
delay(len8 * 1000);
cal(G3, len8);
delay(len8 * 1000);
cal(G3, len8);
delay(len8 * 1000);
cal(G3, 0.150);
cal(G3, 0.150);
cal(G3, 0.150);
cal(G3, 0.150);
cal(G3, 0.150);
cal(G3, 0.150);
cal(G3, len4);
cal(B3, len8);
cal(D4, len2);
cal(C3, len4);
cal(F3, len8);
cal(F4, len4);
cal(C3, len4);
cal(E4, len16);
cal(D4, len16);
cal(D4, len2);
cal(F3, len4);
cal(E3, len16);
cal(Ds3, len16);
cal((Ds3 + B2) / 2, len2);
}
}
int cal(double frec,double length){
double T = 1/frec/2*1000000;
double loopnum = length*1000000/T/2;
int loop = loopnum;
for(int i=0;i<loop;i++){
digitalWrite(buzz,HIGH);
delayMicroseconds(T);
digitalWrite(buzz,LOW);
delayMicroseconds(T);
}
}
How the Arduino Buzzer Music Code Works
Inside the for loop, the Arduino repeatedly switches the buzzer pin between
>HIGH and LOW. The signal remains HIGH for the amount of time
specified by T, and then remains LOW for the same amount of time.
Together, these two states form one complete cycle of a square wave.
The loop repeats for the number of times stored in loop.
After each complete cycle, the counter variable i increases by one.
Repeating this process generates the electrical signal that causes the passive buzzer
to produce a tone.
The first argument sets the frequency in hertz (Hz), which determines the pitch of the sound.
The second argument sets the duration of the tone in seconds.
When you press the button, the Arduino calls this function for each note and plays the complete melody through the passive buzzer.
Active Buzzer vs Passive Buzzer for Arduino Music
Use a passive buzzer if you want to play music or melodies. A passive buzzer can play different pitches by changing the frequency. An active buzzer usually makes only one fixed beep sound.
View Passive Buzzer on AmazonArduino Buzzer Wiring: Buzzer and Button Circuit
- Buzzer positive leg → Arduino digital pin 7
- Buzzer negative leg → GND
- Button one side → Arduino digital pin 5
- Button other side → GND
FAQ About Arduino Buzzer Music
Do I need a library to play music with Arduino?
No. This example does not use a music library. It plays notes manually using frequency and timing.
Should I use an active buzzer or a passive buzzer?
Use a passive buzzer if you want to play different pitches and melodies.
Why is my buzzer not playing music?
Check that you are using a passive buzzer, the wiring is correct, and the buzzer is connected to the correct Arduino pin.