Introduction: Create Songs With Arduino: Harry Potter Theme Song

Picture of Create Songs With Arduino: Harry Potter Theme Song

Hey guys! Hope you're alright.

I just received an Arduino Uno which I had ordered yesterday. I was trying some codes, when the idea of music making hit me. I found a function called tone(), and I tried to make sounds coming out the buzzer using this function. After a while, I managed to play the OST of Harry Potter! :D So let's see how it's done.

Step 1: What You Need

Picture of What You Need

You don't need anything special really. Just these three items:

  1. Arduino Uno [1]
  2. Non-Oscillating Buzzer [2]
  3. USB Cable [3]
  4. Laptop (Not shown)

[1]: You may use other types of Arduino, but I cannot guarantee if they would work as expected.

[2]: Some buzzers cannot output different frequencies.

[3]: Some Arduino models do not come with USB port, and you have to manually attach them.

If you have all these items, let's not waste the time and proceed!

Step 2: Connect Arduino & Set It Up

Head up to Arduino's website, download and install Arduino IDE. You need this to program your cute little device.

Then connect your Arduino via USB cable, and open up Arduino IDE.

Now, from the top menu, navigate and select "Tools > Board > Arduino/Genuino Uno".

Then navigate and select "Tools > Ports > /dev/cu.usbmodem xxxx (Arduino/Genuino) Uno". Note that xxxx is a number, it might be different for you.

Now you are ready to code!

Step 3: Attach the Buzzer

Picture of Attach the Buzzer

The buzzer has two pins. Connect the negative pin of your buzzer to the GND pin on your Arduino. This pin is located below Pin 13. Next, connect the positive pin of the buzzer to Pin 11. Any pin works just fine, but my code has been written for Pin 11. Now you are completely ready to code this baby up!

Step 4: Code Your Song!

The code for the program to work is attached to this post, but I'm gonna go ahead and explain the general idea.

Arduino comes with a function called tone(). This function accepts 3 arguments and is used like this:

tone(pin_number, frequency, duration);

pin_number: Number of the pin you have attached your buzzer to.

frequency: a number between 31 and 10000. Change this to create new sounds.

duration: How long the sound would keep buzzing.

The idea here is to find the equivalent frequency for each musical note. This info can be found here.

I made a C++ struct called MusicStruct, which contains all these frequencies for each note.

As for how long to linger the sound after each note is played (time signatures), I created a struct called LengthStruct, which holds a number that is multiplied to another variable called tempo (which is by default 400). For instance, tempo * Length.two means the same as an empty dot in a musical sheet. The result is then fed as the last argument for tone(), which is the duration.

half note (minim)       = tempo * Length.two
dotted half note        = tempo * Length.two-half

quarter note (crotchet) = tempo * Length.one
dotted quarter note     = tempo * Length.one-half

8th note (quaver)       = tempo * Length.half</p>

Next, I googled for a sheet music of Harry Potter's famous OST, and I found it here. Then I tried to simulate the song using my previously gathered data. The result is what you can see in the attached video.

Note: The code is extendable and modular, you can create any song you want easily!

Have fun! <3

Comments

samrao.geekdom (author)2017-07-26

Wow, reminded me of the old Nokia basic set! Created loads of ring tones in those days that sounded like this. Great memories :')

This is pretty interesting and simpler than using Pulse Width Modulation (old experiment with a speaker and an Arduino board for a car reversing code) and anyone with a basic knowledge of music could try this out.

Adding this to my to-do collection :)

Thanks a lot! The idea is simple and it can be used to create many other songs. I'm happy you liked it. :)

Swansong (author)2017-07-25

That's awesome!

Borderliner (author)Swansong2017-07-26

Thanks buddy! :)

About This Instructable

1,717views

23favorites

Bio: Computer Engineer, Programming Languages enthusiastic, Unix Freak. **Everything I provide here is distributed under Public Domain**
More by Borderliner:Create Songs With Arduino: Harry Potter Theme Song
Add instructable to: