1. EXPERIMENT#7. Audio Output
Front Page
I.Learning Objectives:
After successfully completing this lab, students will be able to:
1. create sound by adding a speaker.
2. play tones and a simple melody.
II. Discussion:
Sound is a very powerful output that is usually taken for granted. We see things such as LEDs,
we feel things such as motors, but we also hear. Arduino has a nice little library called Tone that
aids in generating sounds at specific frequencies. For people passionate about music, we can
actually play monophonic songs for the most geekish sound possible.
Sound is produced by vibrating air. A sound has a distinctive pitch if the vibration repeats
regularly.
The tone() function is very easy to use. It generates a square wave of 50% duty cycle at the
specified frequency. What does that mean? It means that the used pin will be HIGH half the time
and LOW half the time. It will change between these two states at the specified frequency. Every
musical note has a specific frequency; in our case, Do, which is a C3, has the frequency of 131
Hz. This wave will make the speaker vibrate and generate sound. Arduino can only support
monophonic sound using the Tone function. This means it can only generate one note at a time.
Still, it is quite useful and fun.
NOTE: if you want to play different pitches on multiple pins, you need to call noTone() on one
pin before calling tone() on the next pin.
Syntax:
tone(pin,frequency)
tone(pin, frequency, duration)
Parameters
pin: the pin on which to generate the tone
frequency: the frequency of the tone in hertz - unsigned int
duration: the duration of the tone in milliseconds (optional) - unsigned long
III. Materials:
An Arduino board connected to a computer via USB.
A small 8-ohm speaker.
A 120-ohm resistor; larger values also work, but the sound will be less powerful. Don't
use resistors under 100 ohms.
IV.Procedure:
Follow these steps to connect a speaker to the Arduino.
1. Connect one terminal of the speaker directly into the GND of the Arduino.
2. 2. Using a 120-ohm resistor in series, connect the other terminal to an available digital pin.
Schematic
This is one possible implementation on the 13th digital pin. Other digital pins can also be used.
Here is an example of how to wire it in the air. No breadboard needed here:
Code
The following code will play the famous —Do Re Mi Fa Sol La Ti:
// defining the 8 frequencies that make the 7 notes
#define Do 131
#define Re 147
#define Mi 165
#define Fa 175
#define Sol 196
#define La 220
#define Ti 247
3. #define Do2 262
// defining the pin connected to the speaker
int tonePin = 13;
void setup(){
// Tone pins don't need to be declared
}
void loop(){
// Do
tone(tonePin, Do, 125);
delay(125);
// Re
tone(tonePin, Re, 125);
delay(125);
// Mi
tone(tonePin, Mi, 125);
delay(125);
// Fa
tone(tonePin, Fa, 125);
delay(125);
// Sol
tone(tonePin, Sol, 125);
delay(125);
// La
tone(tonePin, La, 125);
delay(125);
// Ti
tone(tonePin, Ti, 125);
delay(125);
// Higher Do
tone(tonePin, Do2, 125);
delay(125);
}
If the speaker is connected to a different pin, simply change the tonePin value to the value of the
pin that has been used.
Playing Tones
const int speakerPin = 9; // connect speaker to pin 9
const int pitchPin = 0; // pot that will determine the frequency of the tone
void setup()
{
}
void loop()
{
int sensor0Reading = analogRead(pitchPin); // read input to set frequency
// map the analog readings to a meaningful range
4. int frequency = map(sensor0Reading, 0, 1023, 100,5000); // 100Hz to 5kHz
int duration = 250; // how long the tone lasts
tone(speakerPin, frequency, duration); // play the tone
delay(1000); // pause one second
}
Playing a Simple Melody
const int speakerPin = 9; // connect speaker to pin 9
char noteNames[ ] = {'C','D','E','F','G','a','b'};
unsigned int frequencies[ ] = {262,294,330,349,392,440,494};
const byte noteCount = sizeof(noteNames); // number of notes (7 here)
//notes, a space represents a rest
char score[ ] = "CCGGaaGFFEEDDC GGFFEEDGGFFEED CCGGaaGFFEEDDC ";
const byte scoreLen = sizeof(score); // the number of notes in the score
void setup()
{
}
void loop()
{
for (int i = 0; i < scoreLen; i++)
{
int duration = 333; // each note lasts for a third of a second
playNote(score[i], duration); // play the note
}
delay(4000); // wait four seconds before repeating the song
}
void playNote(char note, int duration)
{
// play the tone corresponding to the note name
for (int i = 0; i < noteCount; i++)
{
// try and find a match for the noteName to get the index to the note
if (noteNames[i] == note) // find a matching note name in the array
tone(speakerPin, frequencies[i], duration); // play the note
}
// if there is no match then the note is a rest, so just do the delay
delay(duration);
}
Exercise:
Make a program that will play your own choice of music.