Polyphonic Music On An Arduino

Arduino Unos are cool, useful little dev boards with built-in functions for nearly everything. Arduino C even includes a tone() function, which can be used to generate square waves of a given frequency on a specified pin.

With the right note-to-frequency table, these tones can be made into music. There’s a drawback, however — the Uno can only handle one tone() output running at once. If you need to play E4, you have to stop playing C4 first.

There are several ways around this, ranging from using wave or mp3 shields to software-only approaches. By far the cheapest an easiest (no additional hardware components) is to arpeggiate the chords, playing the notes rapidly in succession. This tricks the ear into hearing a chord, even though all of the tones are not present at the same time.

Here are some Arduino C functions to help automate this, along with an example application that plays the first part of the Super Mario theme. The functions handle switching between notes as well as the timing. (Copy and paste the code as a complete Arduino sketch.)

Here’s where I found the sheet music for the Super Mario theme.
(I don’t recommend reading the description; it’s a word salad of English words, and looks like it was written by a stochastic Markov process.)

Yet to be done:

  • Update tempo to be modifiable, specified in beats per minute (BPM);
  • Tweak the timings for best sound and consistency;
  • Bozo-check the input to make sure note values are valid
  • …and maybe find the rest of the sheet music.

Share and enjoy! (code is CC-BY-NC)

 

This entry was posted in Arduino, C, Coding, Digital, Music. Bookmark the permalink.

Leave a Reply