= Making Music = Your kit includes a tiny [http://electronics.howstuffworks.com/speaker6.htm audio speaker] [[Image(speaker.jpg,align=right,width=250px,margin=10)]]. You can use the speaker with your Arduino to make sounds and even play music. You can connect your speaker to one of the arduino digital pins (red wire) and ground (black wire) and use the tone() function to generate sound! Because the wires on the speaker are very thin, it may be hard to get them into the holes on the arduino or breadboard. You can try using one lead from a resistor to push them in or ask a mentor or senior student for help. If needed, we can solder thicker wire leads to the ends of the speaker wires. * [http://learning.codasign.com/index.php?title=Arduino_tone%28_%29 The simplest method] * [https://www.arduino.cc/en/Tutorial/Melody Another approach] that's more complex, but more instructive * [http://playground.arduino.cc/Code/MusicalAlgoFun a music program] * [https://www.arduino.cc/en/Tutorial/PlayMelody music with volume control] **Exercises**: * Make a sound * Play a song (use an array for the notes and lookup tables for the frequencies) * Use switches to make an instrument * Examine the signal being sent to the speaker using an oscilloscope * NOTE: later, we'll build a [https://learn.adafruit.com/adafruit-arduino-lesson-10-making-sounds/pseudo-theramin Thermin] == Sound == Pure tones, such as the sound made by a [https://en.wikipedia.org/wiki/Piano_key_frequencies key on a piano], rise and fall in a ''sinusoidal'' pattern (sine wave). Humans can typically hear sounds between 20Hz (sound rises and falls 20 times-per-second) and 20kHz (20,000 times per second); as we get older, we lose the ability to hear some of the higher frequencies which is why [http://www.noisehelp.com/ultrasonic-ringtones.html mosquito ring tones] were invented. With most of the projects above, your Arduino has been making tones by turning the speaker on and off at some frequency. The sound isn't great because we are using digital outputs to control the speaker; the outputs can only turn on or off, creating a square wave, not a sinusoidal pattern. However, in the last project above you used an analog output that can vary the amplitude of its signal...this can be used to make better and different sounds as you'll see below. When you create more than one pure tone at a time, such as when you play a chord on a piano, the different [http://www.math.umn.edu/~rogness/math1155/soundwaves/ sound waves interact], add or subtract from each other and the result is a complex waveform. Noise cancelling headphones work on this principle (they generate sound waves that are 180-degrees out of phase with the sounds around you so that when they mix, they cancel each other out. Your Arduino makes it easy to generate a single tone (using the tone() function), but it can also generate sounds that consist of multiple tones (this is called ''polyphony''). Generating polyphonic music is more complex and there are several different approaches: * Use a library like [https://code.google.com/p/arduino-playtune/ Playtune], that generates several tones at once (each on a different pin) using dedicated Arduino hardware timers and then combine (add) the outputs together through series resistors to feed the speaker. This is called [https://en.wikipedia.org/wiki/Additive_synthesis additive synthesis]. * You can also use math to calculate the proper output; for example, calculating the amplitude of each tone and then adding the amplitudes together in software before sending the sum to the speaker. This is called ''direct synthesis''. Because some of the math calculations are complex and an small computers like an Arduino might not be able to perform them fast enough, we often use [http://telecnatron.com/articles/Simple-Direct-Digital-Synthesis-Tutorial/index.html tables of pre-computed values] to help generate the desired values; this is called [http://www.mobileer.com/wp/direct_vs_wavetable.pdf wavetable synthesis]. As digital hardware has gotten faster and faster, Direct Digital Synthesis is now a very popular way to generate waveforms. * The [http://makezine.com/projects/make-35/advanced-arduino-sound-synthesis/ Advanced sound and waveform generator] project presents various approaches for generating polyphonic sound using an Arduino. == MP3 Player? == WAV files contain discrete measurements of the amplitude of sound waves you can [http://www.instructables.com/id/Simple-Wav-Player-Using-Arduino/?ALLSTEPS turn an Arduino into a .wav file player] or make it play back sounds you have recorded on your PC or perhaps [http://maxoffsky.com/maxoffsky-blog/how-to-play-wav-audio-files-with-arduino-uno-and-microsd-card/ yoda] == Advanced Projects == [http://www.tehnorama.ro/wp-content/uploads/2010/06/Arduino-LM386.png better sound] [http://blogspot.tenettech.com/?p=518 text to speech] Make a polyphonic synthesizer