Arduino Lesson 10. Making Sounds: Created by Simon Monk
Arduino Lesson 10. Making Sounds: Created by Simon Monk
Arduino Lesson 10. Making Sounds: Created by Simon Monk
Making Sounds
Created by Simon Monk
Guide Contents 2
Overview 3
Parts 4
Part 4
Qty 4
Playing a Scale 5
Sound 7
Pseudo-Theremin 8
Arduino Code 9
Other Things to Do 10
Piezo sounder
Half-size Breadboard
1
Arduino Uno R3
1
void setup()
{
for (int i = 0; i < numTones; i++)
{
tone(speakerPin, tones[i]);
delay(500);
}
noTone(speakerPin);
}
void loop()
{
}
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.
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.
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.
/*
Adafruit Arduino - Lesson 10. Pseudo Thermin
*/
void setup()
{
}
void loop()
{
int reading = analogRead(photocellPin);
int pitch = 200 + reading / 4;
tone(speakerPin, pitch);
}
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.
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.
https://adafru.it/aUr
https://adafru.it/aUr
Simon Monk is author of a number of books relating to Open Source Hardware. The following books written by Simon
are available from Adafruit: Programming Arduino (http://adafru.it/1019), 30 Arduino Projects for the Evil
Genius (http://adafru.it/868) and Programming the Raspberry Pi (https://adafru.it/aM5).