Songs for Arduino

I have recently become very interested in music. Consequently, I started to learn how to read scores. So, I thought that adapting some songs for Arduino would be a good exercise. I wrote a few Arduino sketches, with some details on mind:

  • Sketches should be compatible with all or almost all Arduino boards;
  • No need for installing libraries;
  • Sketches should be easy to understand and modify.

That said, I have only used the tone() function, which is part of the Arduino “language”. The tone() function is capable of generating a single tone, in only one pin at a time. Libraries such as the Tone library allow you to generate more tones, but use specific timers of some microcontrollers, which causes incompatibility with many boards. This means that the sketches here are monophonic, that is, only one note can be played at a time.

Songs

At the moment, the following songs are currently available:

Movies

Games

Classic

Others

The repository with all the songs can be found on github.

Example

As an example, we will use the theme of the game Tetris (type A). The code can be found along with all the others in the repository.

Simply copy the code into the Arduino IDE and connect a buzzer to pin 11 of your Arduino board, or connect it to any pin and edit the value of the buzzer variable accordingly.

With the piezo connected to the board, simply add the code with the desired song in the IDE and upload it to the Arduino. The tempo variable can be changed to make the music play faster or slower, while the buzzer variable contains the number of the pin to which the piezo is connected. The melody vector contains each note of the song followed by their duration.

After uploading the sketch, the Arduino will play the song. The setup() code can be moved to the loop() if the music is to be repeated.

That’s it for now. I have thought about making a post about the process I use to “convert” the songs from the score to the Arduino, but that’s for later :)

Thanks for reading, see you next post o/

Robson Couto

Electrical engineer. I enjoy devoting my time to learning about computers, electronics, programming and reverse engineering. I document my projects in this blog when possible.

Leave a Reply

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