Arduino Lesson 9: Making Sounds

Overview

In this lesson, you will learn how to make sounds with your Arduino. First you will make the Arduino play a ‘musical’ scale and then combine this with a photocell, to make a Theremin-like instrument that changes the pitch played as you wave your hand over the photocell.

learn_arduino_overview.jpg

Parts

learn_arduino_R-1k-level.jpg

1 kΩ Resistor (brown, black, red stripes)

1

learn_arduino_piezo_sounder.jpg

Piezo sounder

1

learn_arduino_resistor_LDR.jpg

Photocell (Light Dependent Resistor)

1

learn_arduino_breadboard_half_web.jpg

Half-size Breadboard

1

learn_arduino_uno_r3_web.jpg

Arduino Uno R3

1

learn_arduino_jumpers_web.jpg

Jumper wire pack

1

Playing a Scale

For the first part of this lesson, the only thing on the breadboard is the Piezo buzzer. One pin of the piezo sounder goes to GND connection and the other to digital pin 12.

 

learn_arduino_just_sounder.jpg
learn_arduino_fritzing.jpg

Program your Arduino with the following sketch:

  1. /*
  2. Adafruit Arduino – Lesson 10. Simple Sounds
  3. */
  4.  
  5. int speakerPin =12;
  6.  
  7. int numTones =10;
  8. int tones[]={261,277,294,311,330,349,370,392,415,440};
  9. // mid C C# D D# E F F# G G# A
  10.  
  11. void setup()
  12. {
  13. for(int i =0; i < numTones; i++)
  14. {
  15. tone(speakerPin, tones[i]);
  16. delay(500);
  17. }
  18. noTone(speakerPin);
  19. }
  20.  
  21. void loop()
  22. {
  23. }

To play a note of a particular pitch, you specify the frequency. See the following section on sound. The different frequencies for each note are kept in an array. An array is like a list. So, a scale can be played by playing each of the notes in the list in turn.

The ‘for’ loop will count from 0 to 9 using the variable ‘i’. To get the frequency of the note to play at each step, we use ‘tone[i]’. This means, the value in the ‘tones’ array at position ‘i’. So, for example, ‘tones[0]’ is 261, ‘tones[1]’ is 277 etc.

The Arduino command ‘tone’ takes two parameters, the first is the pin to play the tone on and the second is the frequency of the tone to play.

When all the notes have been played, the ‘noTone’ command stops that pin playing any tone.

We could have put the tone playing code into ‘loop’ rather than ‘setup’, but frankly the same scale being played over and over again gets a bit tiresome. So in this case, ‘loop’ is empty.

To play the tune again, just press the reset button.

 

Sound

Sound waves are vibrations in the air pressure. The speed of the vibrations (cycles per second or Hertz) is what makes the pitch of the sound. The higher the frequency of the vibration, the higher the pitch.

learn_arduino_sound.png

Middle C is usually defined as a frequency of 261 Hz. If you turn a digital output on and off again 261 times every second then that output will be middle C.

To hear the output, we need to attach something that will convert the electrical signal into sound waves. This can be done with a loudspeaker or as we have used here a piezo sounder.

Piezo sounders use a special crystal that expands and contracts as an electrical signal passes through it. This will generate a tone that we can hear.

 

Pseudo-Theremin

The Theramin (http://en.wikipedia.org/wiki/Theremin) is a musical instrument that makes spooky synthesized sounds as you wave your hands in front of it. It was used in the theme music for the original Star Trek series.

We are going to make a similar instrument, albeit a lot less musical, but it will change the pitch of the note as you wave your hand in front of it.

We can leave the piezo sounder where it is, attached directly to the Arduino, but we will need the breadboard for the photocell and resistor that are going to control the pitch.

learn_arduino_fritzing2.jpg

Arduino Code

Upload the following code on to your Arduino board.

  1. /*
  2. Adafruit Arduino – Lesson 10. Pseudo Thermin
  3. */
  4.  
  5. int speakerPin =12;
  6. int photocellPin =0;
  7.  
  8. void setup()
  9. {
  10. }
  11.  
  12. void loop()
  13. {
  14. int reading = analogRead(photocellPin);
  15. int pitch =200+ reading /4;
  16. tone(speakerPin, pitch);
  17. }

The sketch is actually really straightforward. We simply take an analog reading from A0, to measure the light intensity. This value will be in the range of something like 0 to 700.

We add 200 to this raw value, to make 200 Hz the lowest frequency and simply add the reading divided by 4 to this value, to give us a range of around 200Hz to 370Hz.

 

Other Things to Do

Try changing the value 4 in the line below to lower and higher values.

  1. int pitch =200+ reading /4;
This will expand or restrict the range of frequencies.

Returning to the first sketch, try and modify it to play a tune. Hint, you can just change the values in the ‘tones’ array. Note that if you change the number of notes from 10 notes, then you will need to change ‘numTones’ accordingly.

Posted in Arduino Lessons Tagged with: , , , ,
One comment on “Arduino Lesson 9: Making Sounds
  1. abablowski says:

    You can plug a piezo buzzer directly to an Arduino port? No need for a series resistor?

Leave a Reply

Your email address will not be published. Required fields are marked *

*