Playing popular songs with Arduino and a buzzer

  • avatar

Buzzer is used to generate sound, beep or even melody of a song. It can be found in alarm devices, computers, timers and confirmation of user input such as a mouse click or keystroke. A piezo buzzer is not like a regular speaker that you might think of. It uses a material that actually changes shape when you apply electricity to it which in turn creates noise. The faster you bend the material, the higher the pitch of the noise that is produced.

Prerequisites

To understand the inner workings of the buzzer, we suggest checking out our article on How to use a buzzer with Arduino.

Components

arduino-nano

1x Arduino Nano (or another Arduino module)

$3.18

Buy now

mini-breadboard

1x Mini-breadboard

$1.17

Buy now

buzzer

1x Buzzer

$2.12

Buy now

dupont

Dupont wires

$1.61

Buy now

github

Pitches library and melodies

Official HiBit GitHub

Wiring schema

Pairing an Arduino and a piezo buzzer is easy. The buzzer is equipped with two pins: one for signal control and another for ground. In the setup below, the tone control will be managed by Arduino's D9 pin.

arduino_buzzer.png

Importing pitches library

The code used to generate the melody uses an extra library available on our GitHub. This file contains all the pitch values for typical notes.

To import a library, open the Arduino IDE, go to Sketch > Include Library > Add .ZIP Library and select the library file downloaded from our GitHub repository.

Arduino IDE library import

Then you can simply use include statement:

#include "pitches.h"

It will include the library with predefined pitch constants so the melody generation becomes easier.

Arduino code

The main program uses two arrays, melody and durations, to define the sequence of notes and their corresponding duration, respectively. The loop() function will iterate over notes and and use assigned duration for each note. A pause between notes is introduced to distinguish them, calculated as the note's duration plus 30%. After playing a note, the noTone() function stops the buzzer to prevent any lingering sound.

#include "pitches.h"

#define BUZZER_PIN 9

int melody[] = {
// Notes goes here
};

int durations[] = {
// Notes duration goes here
};

void setup()
{
pinMode(BUZZER_PIN, OUTPUT);
}

void loop()
{
int size = sizeof(durations) / sizeof(int);

for (int note = 0; note < size; note++) {
//to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int duration = 1000 / durations[note];
tone(BUZZER_PIN, melody[note], duration);

//to distinguish the notes, set a minimum time between them.
//the note's duration + 30% seems to work well:
int pauseBetweenNotes = duration * 1.30;
delay(pauseBetweenNotes);

//stop the tone playing:
noTone(BUZZER_PIN);
}
}

Note: tone() function uses one of the built in timers on the Arduino’s micro-contoller and works independently of the delay() function. Use of the tone() will interfere with PWM output on pins 3 and 11 (on boards other than the Mega).

This program provides a flexible framework for creating simple melodies by adjusting the contents of the melody and durations arrays. Users can customize the musical composition by populating these arrays with different note frequencies and durations.

Popular songs

At the moment, the following melodies are available. The list can be updated with new ones based on user requests. So, feel free to leave a comment with melodies you would like to hear.

Songs

Movies

Games

Other

Credits

Official GitHub: https://github.com/hibit-dev/buzzer

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month

We never send SPAM nor unsolicited emails

45 Comments

avatar

Jlocke1776 Reply

Great stuff, could you add Vanilla Ice - Ice Ice Baby?

avatar

Aditya Sarkar Reply

Please upload Ezio's Family from Assassin's Creed 2

avatar

Reply

Assassin's Creed 2 - Ezio's Family has been added!

Enjoy!

avatar

Tara Singh Reply

can u add living on a prayer please please please

avatar

Reply

Livin' on a prayer has been added.

Enjoy!

avatar

dacheezeman Reply

Hello, can you please add Kerosene By crystal castles. Would be great for a project i am currently working on. Thanks!

avatar

Reply

Crystal Castles Kerosene has been added.

Enjoy!

avatar

dacheezeman Reply

Thanks so much!

avatar

Itay Reply

Doom does not work for some reason :(

avatar

Reply

The Doom melody requires more memory than others, so make sure your board can handle that amount. If not, you will usually encounter an error during compilation.

As a workaround, you can try removing some of the notes (along with their respective durations) to fit the melody on the board.

avatar

Anastasiya Reply

Hi, is it possible to add additional memory to the board?

avatar

Reply

The board has built-in memory. If additional memory is needed, an alternative board can be used.

avatar

Emre Mandıra Reply

hey, can you add Coldplay - Hymn For The Weekend, please?

avatar

Reply

Coldplay - Hymn for the weekend has been added.

Enjoy!

avatar

yoki hetoro Reply

Hi. It would be awesome if you could add the German folk song Was wollen wir trinken!

avatar

Reply

Was wollen wir trinken has been added.

Enjoy!

avatar

Олексій Сальков Reply

Hello, can you add Richard Marx - Right Here Waiting

Please.

avatar

Reply

Right Here Waiting has been added.

Enjoy!

avatar

Monika Berkova Reply

Thank you for answering requests. Awesome

avatar

Олексій Сальков Reply

Thank you very much

avatar

Natalie Majano Reply

Is there anyway you can do coogie by dijon? or talk down by dijon?

avatar

Reply

We couldn't find notes for that song that will sound good with a buzzer.

avatar

Monika Berkova Reply

Hello, could you please add Never Gonna Give You Up by Rick Astley

avatar

Reply

Never Gonna Give You Up has been added.


Enjoy!

avatar

Zelvet Reply

Could you please Add: End of Beginning by Djo?

avatar

Reply

We just added End of Beginning by Djo.

Enjoy!

avatar

Sherlyn Reply

pls do It’s You by Max feat. Keshi. I’m making a gift for my anniversary:)

avatar

Reply

It's you song by Max feat Keshi has been added.

Happy anniversary!

avatar

yabba dabba do Dhhrnr Reply

can you add original subway surfers theme

avatar

Reply

Subway Surfers theme song has been added!

avatar

lilblueyes Reply

I created a We Are the Champions theme adaptation, available on my GitHub. Check it out!

https://github.com/lilblueyes/WeAreTheChampions-Arduino-Buzzer

avatar

Ghostgamer Reply

Could you please Add: Fast & Furious: Tokyio Drift

avatar

Reply

Fast & Furious: Tokyo Drift has been added.

avatar

Reply

Kaleo's Way down we go song has been added!

avatar

Reply

Imagine Dragons Enemy has been added!

avatar

Geekgui Reply

Omg 😱! This is amazing. Thank you very much! It's soooo great!

Is it possible to add memories from Maroon 5? 🥺

avatar

Reply

Maroon 5 Memories has been added.

Enjoy!

avatar

Reply

The Simpsons theme has been added!

avatar

Raystorm7 Reply

Hi! it's possible to add this is halloween from the film nightmare before christmans?
I have found this link to get the note...but I can't read them :(
Piano Letter Notes

Thank you!

avatar

Reply

The Nightmare Before Christmas has been added.

Enjoy!

Leave a Reply

Your email address will not be published.

Replying to the message: View original

Hey visitor! Unlock access to featured articles, remove ads and much more - it's free.