Play Home Alone song with a piezo buzzer and Arduino

  • avatar
  • 2.5K Views
  • 3 Likes
  • 7 mins read

Home Alone has since been considered one of the best Christmas films and Somewhere in My Memory has been nominated as best original song. We wanted to recreate the melody of the film's theme song with a piezo buzzer and share with you. 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

$7.43 $2.38

Buy now

buzzer

1x Buzzer

$0.99

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.

Piezo buzzer wiring with Arduino

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 for Home Alone song

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[] = {
NOTE_E4, NOTE_C5, NOTE_B4, NOTE_B4,
NOTE_C5, NOTE_B4, NOTE_F4, NOTE_G4,
NOTE_G4, NOTE_E4, NOTE_G4, NOTE_E4,
NOTE_C5, NOTE_G4,
NOTE_D4, NOTE_A4, NOTE_G4, NOTE_C4, NOTE_F4,
NOTE_E4, NOTE_D4,
NOTE_G4, NOTE_E4, NOTE_G4, NOTE_E4,
NOTE_C5, NOTE_G4,
NOTE_A4, NOTE_C5, NOTE_F4, NOTE_E4, NOTE_G4, NOTE_C4,
NOTE_E4, NOTE_D4,
REST, NOTE_E4,NOTE_C4, NOTE_F4, NOTE_D4,
REST, NOTE_FS4, NOTE_D4, NOTE_A4, NOTE_G4,
REST, NOTE_GS5, NOTE_E4, NOTE_B4, NOTE_A4,
NOTE_F4, NOTE_C5, NOTE_A4, NOTE_B4,
NOTE_G4, NOTE_E4, NOTE_G4, NOTE_E4,
NOTE_C5, NOTE_G4,
NOTE_D4, NOTE_A4, NOTE_G4, NOTE_C4, NOTE_F4,
NOTE_E4, NOTE_D4,
NOTE_G4, NOTE_E4, NOTE_G4, NOTE_E4,
NOTE_C5, NOTE_G4,
REST, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_G4, NOTE_E4,
REST, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_G4, NOTE_E4,
REST, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_G4, NOTE_E4,
NOTE_C4, NOTE_C5, NOTE_B4,
NOTE_D5, NOTE_E4, NOTE_G4, NOTE_E4,
NOTE_C5, NOTE_G4,
NOTE_C5
};

int durations[] = {
4, 8, 8, 2,
8, 8, 4, 4,
4, 4, 4, 4,
2, 2,
4, 4, 4, 8, 8,
2, 2,
4, 4, 4, 4,
2, 2,
8, 8, 4, 8, 8, 4,
2, 2,
4, 8, 8, 4, 4,
4, 8, 8, 4, 4,
4, 8, 8, 4, 4,
4, 8, 8, 2,
4, 4, 4, 4,
2, 2,
4, 4, 4, 8, 8,
2, 2,
4, 4, 4, 4,
2, 2,
4, 16, 16, 8, 4, 4,
4, 16, 16, 8, 4, 4,
4, 16, 16, 8, 4, 4,
4, 2, 4,
4, 4, 4, 4,
2, 2,
1
};

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).


Merry Christmas and a Happy New Year!

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

0 Comments

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.