There are already ways to make an Arduino play music using a buzzer, and they work great. However, you have to write every song or sound effect in C, and the fine tuning can take a lot of time.
So if you have to create lots of music for a project, say a game console, the time isn't worth the effort. That's why I wrote an app that simplifies the process. I call it 'Piarduano', it's a native mac app and you can find it on GitHub.
It converts your keyboard to a piano: a certain key correspondents to a certain note, which is sent trough serial to an Arduino board. The Arduino outputs the note to a buzzer using the tone() function. So you hear in real time what you are playing. After you've recorded your song or sound effect, you can replay it, edit it, and most importantly, export it to code which you can copy and paste to you sketch. It couldn't be any easier!
Step 1: Installation
- A Mac with Yosemite (I haven't tried older versions)
- An Arduino board with serial (e.g. an Uno)
- A buzzer
- A resistor (I used a 270Ω)
- A breadboard and some wires or another way to connect the buzzer
Download the latest version of the Piarduano.app and the piarduano_receiver sketch from the 'Releases' tab in GitHub. Open the sketch in the Arduino IDE and upload it to the Arduino board. Close the Arduino IDE but leave the Arduino connected to your computer.
Now connect the buzzer and the resistor in series between Digital Pin 8 (default) and Ground.
Step 2: Playing a Song
Open the Piarduano app. Select the correct serial port (should be something like /dev/cu.usbmodemfaXXX) and click on 'Start' to start the serial communication. From now on, until the connection is closed, keyboard keys will trigger the Arduino to play a note.
By default, the app knows four rows of 12 keys:
- qwertyuiop[ ]
- `zxcvbnm,./ ENTER
The notes assigned to each key in a key row are in the order
- C Db D Eb E F Gb G Ab A Bb B
So for example the key S would trigger a Db note to be played. The first row has the highest pitch, and the last row has the lowest. Since four rows are not enough to cover 8 octaves, you can select the range of octaves in the main window (using the "Shift" selection). The rest of the functions are quite self-explainatory.
Step 3: Using Your Master Piece in a Sketch
Press 'Export to C' to get the C code (two arrays and a #define) you can copy and paste to an Arduino sketch.
Of course, this isn't enough by itself: you'll have to add the code that will play the song. An example of such code can be viewed by pressing the 'Show sample sketch' button.