Creating an Arduino Melody

Oct 5, 2016

When I first sought out to create a melody through an Arduino code, I thought this would be extremely difficult. I came to understand that it is pretty straightforward, especially if you have prior experience with a musical instrument. Granted, my code only generates a very short simple melody and the possibilities in which one could generate music span way further than what I have done this week. I understand that. However, I was pleasantly surprised when the sounds coming out of the speaker actually sounded like a melody I once made up on the piano.

The Arduino library of musical notes was puzzling at first. Each note is written with the word “NOTE”, an underscore, a note itself (i.e. C,B,A) and a number (1-8). If the note is a “sharp”, the letter “S” is placed in between the letter and number. I came to discover, through trial and error, that the number represents the octave in which it is being played. If the octave is higher the number is higher and likewise if the octave it lower, the number is lower. Each of these notes represents a different frequency value. Arduino has a pre-constructed library of pitches that a coder can use so they don’t have to go through the trouble of creating a name for each frequency. This is what I used.


So I started with a mid-range octave: 4 (i.e. NOTE_”letter”4). There was a variance between 3 and 4 for some notes,  but the majority were played at the 4th octave. The next step was to replicate the string of notes that I am used to playing on the piano: C, G, F, E, D, C, | C, G, F, E, D, C | C, G, F, E, D, C, D, E, F, G, F, E, D, C | C, B, A, B, C | C, B, A, B, C | C, B, A, B, C, D, E, D, C, B, A, B, C, D, E, D, C, C. These are the opening notes to one of my favorite songs that I play on the piano. A song I made up a few years ago.

The next step was to set the duration of each note. In other words, how long should each note be played. Through trial and error I came to discover that the higher the number the shorter the duration of the note and the lower the number the longer duration of the note. For instance NOTE_C32 will be a very fast sound, where as NOTE_C1 will be drawn out longer. This was important for my melody because without varying note durations, the melody is lost as it just sounds like a bunch of notes running in a marathon. The duration variance gives the melody its dynamics.


The next step was to create corresponding lights. The entire melody consisted of the notes A-G, which is seven notes. I decided to place the lights in order, via a parallel digital OUTPUT circuit,  to represent each note in the same order it would appear on a piano (i.e. when watching the video, the green LED all the way to the right is A, the yellow LED to it’s left is B, the red LED to it’s left is C, following is D, E, F and G). So after creating an Arduino code that mapped each note to an LED, the LEDs lit up according to the note being played. So if one were to watch the LEDs light pattern, they could simply translate that directly to a piano and play the exact melody (with probably a nice, more soothing tone). This would be an easy way to read music.

A side note: I didn’t create a switch to initiate the melody, so when going through all of my troubleshooting and having to play the melody repeatedly at ITP, some people actually began humming the melody without thinking. That’s my song they’re singing!!!! How cool!!!! 🙂 This made me happy.

In the future, with more time, I would like to create a switch for this circuit, and also a variable resistor that could manipulate the duty cycle and allow the user to control the volume.

(In the following aerial view video, the note durations are a bit off towards the end.)

After finally getting my hands on some “Bare Conductive” Electric Paint, I was able to begin the kind of experimentation I am very interested in: physicality in circuitry. I volunteered my sister to be my guinea pig. I told her the chances of her getting electrocuted were only slim, and so she agreed to the role. I put a dab on her pointer fingers and thumbs and then painted a small connecting line between them. She then held both ends of the wires, which are usually connected via the graphite of the drawing. IT WORKED!!!! The LEDs lit!!!!! Well… barely… but it worked! I think the amount of conductive material (paint) was too small for this circuit, because the LEDs only dimly lit up, however it was really exciting to see that the possibility of lighting an LED with the body definitely exists!

I did do some research prior to experimenting on my sister, and I found that there was actually no chance of electrocuting the user as long as the voltage was five or less volts. This came as a disappointment to me however because does that mean I can’t use any circuits that run on more than five volts? I would LOVE to one day choreograph a piece where the dancer moves about the stage and connects various circuits that lit up different stage lights or perhaps sound different musical notes. Would this be possible? Or would the dancer run the risk of being electrocuted? This is probably an overreach right now, but one day achieving something like that would be a dream.

Lastly, as per the lab completed in class, below if a video of an input and output analog circuit. I used a potentiometer as a variable resister to input a value between 0 and 180. I had to map the standard analog input range of 0-1023 to 0-180 because a Servo moves based on angles and generally within a range of 180 degrees.


One Reply to “Creating an Arduino Melody”

  1. I am super excited about all of these experiments and super happy to see you continue to build on some of the previous ones. I love the materials you are choosing to experiment with and look forward to seeing what you create every week! Definitely happy to talk more about your larger project ideas in office hours!

Leave a Reply

Your email address will not be published. Required fields are marked *