LilyPad Arduino: sound
|1. If you
haven't yet, make a coaster for your LilyPad and speaker module
See this tutorial for instructions. This will prevent the alligator clips that we'll be using from sliding around on the LilyPad. Trust me, it's worth it to do this!
2. Use alligator clips to attach the speaker/buzzer module to the LilyPad
Attach the + side of the speaker module to tab 9 on the LilyPad and the - side of the speaker to - on the LilyPad like this:
3. Attach the LilyPad to your computer and start the Arduino software
4. Copy this sample code into an Arduino window
Click here for sound sample code. Copy and paste this code into an empty Arduino window.
5. Format the code
Under the Tools menu, select Auto Format. After you do this, align all of your comments (the statements in grey-brown following "//" on each line) so that they are in readable columns on the right hand side of the screen. This will help you read through the code. Here's what my Arduino window looked like after I formatted everything:
6. Load the code onto the LilyPad
Compile the code and load it onto the LilyPad. Do this by hitting the upload button in the Arduino window (that's the right pointing arrow at the top of the Arduino window) and then quickly pressing the reset switch on the LilyPad. If you're new to this process, or if you have any problems uploading, see the previous LilyPad tutorial: 4. light (LEDs).
Your LilyPad speaker should be singing out a "do re me fa so la ti do" scale. The LED should be on while the scale is playing. If you're not getting sound, double check your alligator clip connections.
8. Making sense of things: sound
So, a little bit about sound...Sound is air vibration. Different pitches are produced with different frequencies of vibration. Here's a good little introduction to sound: http://www.fearofphysics.com/Sound/dist.html. And here's a nice chart that maps vibration frequencies (in hertz or Hz) to musical notes: http://www.phy.mtu.edu/~suits/notefreqs.html.
The LilyPad buzzer module works the same way any speaker does. It contains a material or device that moves in response to electricity. It is in one position when there is no voltage across the + and - petals on the speaker (when both petals are attached to - or set LOW), and another position when there is voltage across the petals (when one petal is attached to +5V or set HIGH and the other is attached to -). To use this device to create air vibrations, we need to get it to move between those two positions quickly, hundreds to thousands of times per second. How quickly we flip between the two positions (the frequency of the movement) will determine the pitch of the sound.
9. Making sense of things: the code
Now, let's look at the code. The beep() function is the main sound producing chunk of code. Notice how the main part of the function is a loop where we set the speakerPin HIGH for a moment and then LOW for a moment. This is how we are producing sound. The pitch of the sound is determined by the "frequencyInHertz" variable and the duration of the sound is determined by the "timeInMilliseconds" variable.
The scale() function gives an example of how to use the beep() function to produce specific musical notes. I used the frequency chart from http://www.phy.mtu.edu/~suits/notefreqs.html to find the frequencies for a basic musical scale. You can use that same chart to build your own musical libaries.
10. Play with modifying the code to get different behavior