# Unit 14 – Playing Sound on the Arduino

After so much work we must not forget to celebrate. Therefore, we will need some music!

In order to play a tone we have to create an acoustic wave. If you hit a drum the drumhead starts to vibrate. This vibration generates an acoustic wave which is transmitted through the air and perceived by our ears. Speakers also create vibrations but by electrical signals. We will use a Piezo speaker. They are used in squeaking electronic toys and melodic greeting cards (by the way this is a great source for more piezo speakers).

## Arduino Sound Circuit

If you have a different type of piezo you can also use it.

``````int ledPin = 9;

void setup(){
pinMode(ledPin,OUTPUT);
}

void loop(){
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
}``````

Hm, if you listen closely you can hear a repeating clicking sound, right? That is a very slow vibration. So we need a faster one. Let’s try to reduce the values of the delays.

``````int ledPin = 9;

void setup(){
pinMode(ledPin,OUTPUT);
}

void loop(){
digitalWrite(ledPin, HIGH);
delay(2);
digitalWrite(ledPin, LOW);
delay(2);
}``````

A little bit better. By reducing the switching time we accelerated the frequency (the repetition of switching on and off).

Good argument but the frequency is always calculated through time.
BTW: The unit for the frequency is called Hertz (Hz). It is named after the physicist Heinrich Hertz.
When we have 10 repetitions per second the frequency is 10/1s = 10 Hz. When we have 100 repetitions per second the frequency is 100/1s = 100 Hz. That means the higher the frequency, the higher the tone. Let’s try it:

``````int ledPin = 9;

void setup(){
pinMode(ledPin,OUTPUT);
}

void loop(){
digitalWrite(ledPin, HIGH);
delay(1);
digitalWrite(ledPin, LOW);
delay(1);
}``````

And let’s go deeper:

``````int ledPin = 9;

void setup(){
pinMode(ledPin,OUTPUT);
}

void loop(){
digitalWrite(ledPin, HIGH);
delay(4);
digitalWrite(ledPin, LOW);
delay(4);
}``````

Super! I would say let’s try to play both tones together. To hear a difference, we have to play the same frequency for some time. Let’s use a for-loop to do so:

``````int ledPin = 9;

void setup(){
pinMode(ledPin,OUTPUT);
}

void loop(){
for (int repetitions =0; repetitions<=100; repetitions = repetitions +1){
digitalWrite(ledPin, HIGH);
delay(1);
digitalWrite(ledPin, LOW);
delay(1);
}

for (int repetitions =0; repetitions<=100; repetitions = repetitions +1){
digitalWrite(ledPin, HIGH);
delay(2);
digitalWrite(ledPin, LOW);
delay(2);
}

for (int repetitions =0; repetitions<=100; repetitions = repetitions +1){
digitalWrite(ledPin, HIGH);
delay(4);
digitalWrite(ledPin, LOW);
delay(4);
}
}``````

Sound good? Now, we understood how to generate a frequency.

But it is not very handy to write so much. Fortunately, Arduino provides an extra command to generate sounds:

``tone(pin, pitch);``

The command gets two arguments. The pin where the speaker is connected to and the pitch as a frequency value.
And the most important command is switching the tone off, again:

``noTone(pin);``

This command stops the tone at the pin. Oh, by the way if we use tone() we don’t have to declare it in the setup() as an OUTPUT.

Now, try it out:

``````void setup() {

}

void loop() {
tone(9, 440);
delay(500);
noTone(9);
delay(500);
}``````

The frequency of 440 Hz is the so called concert pitch or Standard Pitch A. If you want to learn more, check this out: Concert pitch

You can find the other piano key frequencies here: Piano key frequencies

Let’s play a melody:

``````void setup() {

}

void loop() {
tone(9, 262);
delay(200);
noTone(9);
delay(200);

tone(9, 294);
delay(200);
noTone(9);
delay(200);

tone(9, 330);
delay(200);
noTone(9);
delay(200);

tone(9, 349);
delay(200);
noTone(9);
delay(200);

tone(9, 392);
delay(400);
noTone(9);
delay(200);

tone(9, 392);
delay(400);
noTone(9);
delay(2000);
}``````

Now, you can go on and finish the programming of this song. At first try it by your own.

#### Solution

``````void setup() {

}

void loop() {
tone(9, 262);
delay(200);
noTone(9);
delay(200);

tone(9, 294);
delay(200);
noTone(9);
delay(200);

tone(9, 330);
delay(200);
noTone(9);
delay(200);

tone(9, 349);
delay(200);
noTone(9);
delay(200);

tone(9, 392);
delay(400);
noTone(9);
delay(400);

tone(9, 392);
delay(400);
noTone(9);
delay(400);

tone(9, 440);
delay(200);
noTone(9);
delay(200);

tone(9, 440);
delay(200);
noTone(9);
delay(200);

tone(9, 440);
delay(200);
noTone(9);
delay(200);

tone(9, 440);
delay(200);
noTone(9);
delay(200);

tone(9, 392);
delay(800);
noTone(9);
delay(800);

tone(9, 440);
delay(200);
noTone(9);
delay(200);

tone(9, 440);
delay(200);
noTone(9);
delay(200);

tone(9, 440);
delay(200);
noTone(9);
delay(200);

tone(9, 440);
delay(200);
noTone(9);
delay(200);

tone(9, 392);
delay(800);
noTone(9);
delay(800);

tone(9, 349);
delay(200);
noTone(9);
delay(200);

tone(9, 349);
delay(200);
noTone(9);
delay(200);

tone(9, 349);
delay(200);
noTone(9);
delay(200);

tone(9, 349);
delay(200);
noTone(9);
delay(200);

tone(9, 330);
delay(400);
noTone(9);
delay(400);

tone(9, 330);
delay(400);
noTone(9);
delay(400);

tone(9, 392);
delay(200);
noTone(9);
delay(200);

tone(9, 392);
delay(200);
noTone(9);
delay(200);

tone(9, 392);
delay(200);
noTone(9);
delay(200);

tone(9, 392);
delay(200);
noTone(9);
delay(200);

tone(9, 262);
delay(800);
noTone(9);
delay(800);
}``````