Play MIDI Files on Your Arduino!
by HelpfullDoggo in Circuits > Arduino
5419 Views, 3 Favorites, 0 Comments
Play MIDI Files on Your Arduino!
Hello Today I'm going to show you how to play midi files on your Arduino!
This very easy tutorial will work with any midi files out there...
Edit: I made a tutorial you can watch it above...
Supplies
Arduino IDE: https://www.arduino.cc/en/software/
Miditones: https://github.com/LenShustek/miditones
Playtunes library and .ino: https://www.mediafire.com/file/ao06kc1x048wy2t/PlayTunesProject.zip/file
Thanks for LenShustek for midi tones and playtunes!!
Copy Required Libary
Before we can start make sure you got the PlayTunesProject.zip downloaded and MidiTones too
just download the code from the GitHub repository which you will also get a .zip
extract MidiTones into any folder you like... Now in the PlayTunesProject.zip
you will find Playtune.c and Playtune.cpp...
Go to where Arduino is installed and go to the folder that says Libaries
Create a new folder called Playtune and copy both the .c and .cpp file.
Convert MIDI to Byte Arrays
In order to make the MIDI playable by the Arduino we need to convert it into a byte array...
To do that MidiTones Comes into play...
Go into your MidiTones folder you will find miditones.exe
but this is not a graphical program we will need to run it via command line..
The fact MidiTones is only for windows this will also work on Linux if you have wine installed...
First we will need to find a midi file to use... In my case i used Twinkle Twinkle Little Star...
One thing that is important later on is the complexity of the midi file...
The more octaves used means more tone generators will be used. the maximum is six...
Once you got the midi file copy it to the MidiTones folder...
Now we can proceed with the conversion:
Windows Steps:
Open CMD and navigate using CD to the MidiTones folder (admin is not required)
or shift click and open cmd here (same for linux but instead terminal)
command: miditones.exe midifile.mid
Linux Steps:
Open terminal and instead of just writing the same command type wine before it..
As you can see on the image above you can see some information about the conversion..
One of the lines say: Only 1 tone generators were used
as i said before more complex the midi file meaning more octaves used, more tone generators used.
Prepare the Code...
Once we have converted our midi file we can now proceed with the upload process soon.
After the conversion you will find the output file in my case star.c Open it with any text editor you wish..
Select everything bellow where it says const unsigned char PROGMEM score [] = {
we don't need it cause its already in the template file..
press ctrl+c to copy it.. Open the included .ino file even though it says nano in the title
it also works with the Arduino Uno.. and then paste the data..
Speaker Connection and Upload!
There are 3 ways we can connect the a speaker to it..
On the Arduino the speaker is going to be connected via PWM (Pulse Wave Modulation)
For now we may only need pin 10 but for midi files that have more then 1 tone generator we will need to
connect + of the speaker to PWM 10, 11, and 12 which should be bridged by 500-ohm resistors and - to GND
The resistors are optional if you only have a few tone generators so in this case im not using that
instead you can just put 3 wires to pin 10, 11, and 12 and just join them.
Now this is important before you upload the code make sure to click verify and make sure your not very high on storage space.. now once that done you can upload.... Now you should hear it!
And that my friends is how you play midi files on your Arduino..