SimpleCircuit here with a simple musical project. This Instructable is an extension of several previous Instructables and tutorials, so a tip of the hat to “Arduino-Music” and “Demotime – Arduino Music” by Misperry, “How to easily play music with buzzer on Arduino (The Imperial March – STAR WARS)” by eserra, and “Simple 12 Note Piano” by bprew1, to name just a few. However, these projects require that melodies be hard coded in Arduino sketches. This project consists of two programs for composing and playing melodies on the Arduino without having to enter or modify any code.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Stuff Needed for This Project
Arduino board (I used an Arduino Uno for this project but almost any Arduino will do – check pin assignments for the SD card module for different boards.
SD card module
Breadboard and hookup wire
11 push button switches (optional)
Hardware configurations for the ArduinoMusic and SD_Card_Music sketches are very simple and are shown in the figures above. I used a speaker from an old home theater surround system which produced fairly decent tone reproduction, but a simple 8 ohm speaker will do. The Arduino output pin (A2) provided adequate power for my speaker. Some people recommend placing a 100 ohm shunt resistor in series between the tone output pin of the Arduino and the positive speaker (or buzzer) lead. I didn’t find this necessary.
The optional push button piano with its pin assignments is shown in the top figure. Some makers recommend placing a 10K ohm resistor in series with the ground pin of each push button to eliminate any possible feedback. I did not find feedback to be a problem, so to keep things simple, no resistors. The piano can only be used with the ArduinoMusic sketch since digital pin assignments are different in the SD_Card_Music sketch. The piano() function in the loop function of the ArduinoMusic sketch must be uncommented to use the piano.
The Arduino - SD card configuration is shown in the bottom figure. Aduino Uno pin assignments for the SD card module used for the SD_Card_Music sketch are:
Vcc = Arduino 5V
GND = GND
MOSI = digital pin 11
MISO = digital pin 12
CLK = digital pin 13
CS = digital pin 4
These pin assignments may vary for other Arduino boards so the pin assignments in the SD sketch will have to be changed accordingly.
Step 2: The Software
While the hardware configurations are really simple, the code, not so much. You can download the program sketches at https://github.com/SimpleCircuit/Arduino-Music . Click on the green download button and download and open the zip file that contains both the ArduinoMusic and SD_Card_Music sketches. Copy these to your Arduino sketch folder. Or, you can open each text file below and copy the code to a new Arduino sketch. The first option may be more reliable.
The musical characters and note frequencies are stored in arrays in program memory, of which only about 30% is used by the ArduinoMusic sketch and about 60% by the SD_Card_Music sketch, so additional note octaves could added as required. Any code can be improved upon and mine is no exception, so suggestions for improvements are always welcome. And, of course, please report any bugs that are found so I can fix them.
Step 3: The Music
The intent of this Instructable is not to reinvent the wheel, but to improve it. The code representing musical composition for the Arduino has been presented many times before with minor variations. Notes, rests, and note values (note duration) are assigned to variables that Arduino interprets and outputs as tones of proper frequency and duration. This is essentially Arduino “MIDI” music.
Check out the site, readsheetmusic.info/readingmusic.shtml, for a beginner's guide to reading music if needed. I will cover a few concepts that may help when entering music in the sketches. (Those of you with a musical background can skip this paragraph). Musical sketches written for Arduino are usually based on piano key frequencies. The keys on a piano are divided into octaves, beginning with a C note and ending with the next C note in succession (C,D,E,F,G,A,B,nextC). The piano keyboard contains 7 complete octaves, numbered 1 to 7 from left (lowest C) to right (highest C). Notes are followed by the number of the octave in which they are located, so C4 would be the beginning note in the fourth octave. C4 is also called middle C since the fourth octave is also the middle octave on the keyboard. One last point – the same note doubles in frequency every time it moves up an octave. For example, A3 has a frequency of 220 Hz, A4 has a frequency of 440 Hz, and A5 a frequency of 880 Hz.
The following characters represent the 3 octaves of notes available for this project:
c3, c#3, d3 d#3, e3, f3, f#3, g3, g#3, a3, a#3, b3, c, c#, d, d#, e, f, f#, g, g#, a, a#, b, c5, c#5, d5, d#5, e5, f5, f#5, g5, g#5, a5, a#5, b5, and c6.
These characters are entered into the programs exactly as they are written here. I’ve used lower case letters and dropped the 4 from the notes of the middle octave to simplify data entry (I hope). There are no flat notes because every flat note is the same as the sharp of the lower note next to it. So if a melody has a B-flat, use the equivalent a# instead.
Note Values (note duration)
The following note values are available in the sketches:
1, 1., 2, 2., 4, 4., 8, 8., and 16.
1 represents a whole note, 2 a half note, 4 a quarter note, 8 an eighth note, and 16 a sixteenth note.The quarter note is the default for this project because a majority of music is written in quarter time (3/4, 4/4, etc.), with a quarter note getting one beat. I set the duration of one beat to 400 milliseconds because it sounded about right to me. This can be changed by changing the value of the #define beat statement at the beginning of each sketch. A quarter note will last 400 msec, a half note 800 msec, an eighth note 200 msec. There are also dotted notes available (1., 2., 4., and 8.) A dotted note has a note value one and a half times that of the note. For example, a dotted quarter note is held for a beat and a half, or in our case, 600 msec. Note values are entered before the note whose value is to be changed.
Ex: a<>g<>a<>g<>2<>c5<>4<>a<>g<>a<>g<> (<> = ENTER)
In this example, the note sequence is entered from the serial monitor using the ArduinoMusic sketch. The first 4 notes are quarter notes (the default), entering 2 changes c5 to a half note, and 4 changes the last 4 notes back to quarter notes.
The sketches contain the following rest values:
r1, r2, r4, r4., r8, and r16.
Rests are periods of silence in a musical score, and are analogous to note values - r1 is a whole rest, r2 a half rest, and so forth. Entering r4 would result in one beat of silence; entering r16 would produce ¼ beat of silence. There is one dotted rest (r4.).
Play the Morse code file which uses a combination of note values and rests. The code spells out the time-honored programmer's phrase, "HELLO WORLD".
Step 4: The Sketches
The ArduinoMusic sketch enables you to enter and play music using Arduino’s serial monitor. Compile and upload the sketch, and open the serial monitor. The following message will print, “Set serial monitor to [Newline] and enter musical composition characters, one at a time.”. Enter the music, one character at a time and press ENTER after typing each character. The note buffer can hold up to 120 characters. Type “play” and ENTER to play the melody after all characters have been entered. You can play the music as many times as you like until the note buffer is cleared. Press the Arduino reset button or close the serial monitor to purge the note buffer.
So let's try an example. We'll enter and play the top staff (double set of 5 lines containing the notes) in the figure above. The staff contains 5 measures (time periods) of notes which are delineated by vertical lines.The first note in the music is a half note G4, so the first two characters to enter are 2 (change to half note) and g (note G4). Keep going till everything is entered.
The full character sequence looks like this:
2<>g<>4.<>g<>8<>a<>4<>b<>g<>b<>d5<>2<>g5<>4.<>b5<>8<>a5<>4<>g5<>d5<>b<>g<>2<>c5<>4<>e5<>c5<>play<> (<> = ENTER)
Play the Scotland file to hear the result.
The serial monitor is great for developing compositions or entering simple melodies on the fly, but it has its limitations. It is first and foremost a code debugging tool – it has no text editing or data storage capabilities. If a wrong note is entered, there’s no way to take it back, and there’s no way to store a melody once it’s entered using the ArduinoMusic sketch. The SD_Card_Music sketch addresses these issues.
Using this sketch is a two part process. The first step is to write some music and store it in a file on an SD card. You can use any simple text editor to do this. I use Notepad since I have a Windows based system. To write a melody, type each character and follow it with a comma (no spaces).
When finished composing, store each melody on the SD card as a plain text file with the extension, .txt. File names need to be 8 letters or less.
Now for step 2. Insert the SD card in its module and make sure the module is correctly configured with the Arduino board. Compile and upload the SD_Card_Music sketch, then open the serial monitor. If everything is hooked up correctly, the serial monitor will print the message, "SD Initialization done...". Type in the name of the file you want to play and include the .txt extension (ex. flute.txt). If the filename has been entered correctly the following will happen:
The serial monitor will display the message, "filename.txt exists", the music will play, the serial monitor will print each note as it's played, and after the music has finished the monitor will display the number of characters in the file.
Step 5: Wrapping Up
Well, that's the nuts and bolts of this Instructable. I started this project to improve my programming skills, but in the end it was just a lot of fun messing around with simple melodies and learning a little more about music composition. I hope you find it as much fun and as educational as I have.
A good site for lots of free sheet music that is perfect for this project is http://www.8notes.com . I have also included several melodies in the attached pdf file that are ready to play in ArduinoMusic or loaded into files for SD_Card_Music.
I'll add one last song (Bamboo Flute 1) to inspire you to compose a simple symphony.
SimpleCircuit saying So Long