Lab03 Sound
Lab03 Sound
Parts required for project 1. So far All the projects have been visual, so now it’s time
to make some music. In this project we will be using a piezoelectric buzzer to play
some melodies.
1. Arduino board
2. Piezo buzzer
Parts required for project 2. For centuries clandestine groups have used secret knocks
to prevent unauthorized entry. Let’s bring this system into modern times, by creating
our own electronic gatekeeper.
1. Arduino board
2. Breadboard
3. Jumper wires
4. Tower Pro SG90 9g servomotor
5. Piezo buzzer
6. 3 LEDs
7. 1M-ohm resistor
8. 3 220-ohm resistors
Page 1
PROJECT 1. ARDUINO MELODY
1. How It Works
The Arduino melody uses a piezo buzzer to create frequencies that resemble
recognizable notes. You use the Arduino IDE to give the order, rate, and duration of the
notes to play a specific tune. Piezos are inexpensive buzzers often used in small toys. A piezo
element without its plastic housing looks like a gold metallic disc with connected positive
(typically red) and negative (typically black) wires. A piezo is capable only of making a clicking
sound, which we create by applying voltage. We can make recognizable notes by getting the
piezo to click hundreds of times a second at a particular frequency, so first we need to know
the frequency of the different tones we want. Table 1 shows the notes and their
corresponding frequencies. Period is the duration of time, in microseconds, at which the
frequency is created. We halve this number to get the timeHigh value, which is used in the
code to create the note.
Table 1. Notes and their corresponding frequences
Page 2
The code sends a square wave of the appropriate frequency to the piezo, generating
the corresponding tone (see Project 2 for more on waveform). The tones are calculated
through the following equation:
timeHigh = period / 2 = 1 / (2 * toneFrequency)
The setup of this project is really simple and uses only two wires connected to the
Arduino.
2. The Build
▪ Step 1. Connect the piezo’s black wire directly to GND on the Arduino, and the red wire
to Arduino pin 9.
PIEZO ARDUINO
▪ Step 2. Check that your setup matches that of Figure 1-1, and then upload the code shown
next in “The Sketch”.
3. The Sketch
We’ll start off with a simple tune. At u, we tell the IDE that the tune is made up of 15
notes. Then we store the notes of the melody in a character array as a text string in the order
in which they should be played, and the length for which each note will play is stored in
another array as integers. If you want to change the tune, you can alter the notes in the
Page 3
array at v, and the number of beats for which each corresponding note plays at w. Finally at
x we set the tempo at which the tune will be played. Put it all together, and what does
it play?
Page 4
PROJECT 2. SECRET KNOCK LOCK
1. How It Works
In this project, you’ll make a circuit that moves a servo arm to unlock a box or door
when you provide the correct secret knock. So far we’ve been using a piezo buzzer only to
make noise, but we can also use it as a sensor to detect sounds—in this case, knocks. When
a piezo is struck it rings like a bell, but instead of producing sound it outputs voltage, which
generates a number depending on the force of the strike. We’ll measure this voltage in
numbers, and if the knocks fall within a certain range, the Arduino will register them
as correct. If three knocks of the correct voltage are detected, you’ve cracked the code, and
the servo arm moves to unlock the box or door. Here are the two lines of code we’ll use later
in the sketch to set the range for the voltage; if the voltage is between 10 and 100, the knock
will be registered.
If you knock too softly or too hard, the knock won’t register. You’ll need to do three
“correct” knocks to trigger the servo arm to move. When the correct sequence and strength
of knock are registered, the servo arm swings 90 degrees to “unlock” whatever it is set up
with. The LEDs, shown in Figure 2-1, serve as indicators of your lock’s status: the red LED
lights when the knocks are incorrect and the servo arm has not moved (that is, the box or
door is still locked); the yellow LED flashes when a knock is registered and a correct code is
sensed; and the green LED lights and the servomotor moves after three correct knocks.
Page 5
Figure 2-1. The LED setup
For the best result, remove your piezo from its casing and attach it directly to the inside
of a box or outside of a door so it is more sensitive to the vibration of the knock.
2. The Build
▪ Step 1. Insert a 1M-ohm resistor into your breadboard and connect the piezo’s red wire
to one leg and its black wire to the other. Connect the black wire to the GND rail, and the
red wire to Arduino pin A0.
PIEZO ARDUINO
▪ Step 2. Connect the servo’s yellow signal wire directly to Arduino pin 9, its brown wire to
GND, and its red wire to +5V.
SERVO ARDUINO
▪ Step 3. Insert the LEDs into your breadboard with the short, negative legs connected to
GND. The positive legs should connect to the pins via 220-ohm resistors as follows: yellow
connects to Arduino pin 3, green to pin 4, and red to pin 5.
LEDs ARDUINO
Page 6
▪ Step 4. Connect Arduino pin 2 to the positive power rail. In our setup this is always on,
but you could add a switch in the connection between Arduino pin 2 and the power rail
to save power when the project is not in use.
▪ Step 5. Connect the breadboard rails to Arduino GND and +5V.
▪ Step 6. Make sure your setup matches the circuit diagram in Figure 2-2, and then upload
the code in “The Sketch” on next page.
Figure 2-2. The circuit diagram for the secret knock lock
3. The Sketch
We first call on the Servo library and set Arduino pin 9 to control the servo. LEDs are
attached to Arduino pins 3, 4, and 5, and these will light depending on the validity of a knock.
The piezo acts as a sensor rather than a buzzer in this project and is attached to Arduino pin
Page 7
A0. When someone knocks, the knock is sensed by the piezo and a voltage value is sent to
the A0 analog pin of the Arduino depending on the strength of the knock—the harder the
knock, the higher the value. A knock with a value below 10 is considered too quiet, and one
with a value above 100 too loud, so neither will be accepted as a valid knock. The red LED
lights if the knock is not accepted, and the yellow LED lights if it is. Any knock value between
10 and 100 is accepted as a valid knock and counted, and if three valid knocks are received,
the servomotor moves and the green LED lights.
As mentioned earlier, these are the two lines of code that set the parameters for
measuring the voltage:
Page 8
Page 9