MEng 125n Laboratory Exercise 6
MEng 125n Laboratory Exercise 6
6:
Creating Sounds and
Tones with Piezo
Speaker
Introduction
In this exercise, we will generate different tones in piezo speaker. The code
sequence are as follows:
a. Assign variable names to use and create a tone[] array (10 different
tone frequencies)
b. Start using for:loop to create tone - Integer i is set to zero.
c. First tone plays for 0.5 seconds.
d. Integer i is incremented by one
e. Next tone is played for 0.5 seconds until the tenth tone.
f. After playing the last tone, stop by calling noTone() command.
Learning Outcomes
After this laboratory exercise, student should be able to:
1. discuss the concepts of Interfacing a Piezo Speaker with Arduino;
2. create a desired tone; and
3. use the programming data type “Array” and programming structure “for
loop”.
Materials
• 1 – Arduino UNO R3
• 1 – USB Cable (USB-A to USB-B)
• 1 – Piezo Speaker (Buzzer)
• 1 – Breadboard
• Connecting Wires
• Computer with Arduino software
22 MEng 125n: Basic Electronics
Procedure
Figure 6. Pictorial Diagram of Creating Sounds and Tones with Piezo Speaker
void setup() {
//we do not need to put any code initialization here.
}
Page 22 of 56
Vision: A globally competitive university for science, technology, and environmental conservation.
TP-IMD-04
Mission: Development of a highly competitive human resource, cutting-edge scientific knowledge V0 07-15-2020
and innovative technologies for sustainable communities and environment. No. CET.ME
LM21-01
For instructional purposes only • 1st Semester SY 2020-2021 23
void loop() {
// A "for" loop function is used to make a count from 0 to 9 using a
// variable "i". This "i" corresponds to the tone sequences listed like
// for i = 0 is for 261 and for i = 9 is for 440.
tone(buzzerPin, tones[i]);
delay(500);
}
noTone(buzzerPin); // call the "noTone" command to stop playing any
tones
delay(1000); // delay 1 second before playing again
}
3. Click Verify button to review code for errors. If the code has no
errors and missing commands a message on the window below will
display “Done Compiling” this means your code is
ready to be uploaded into the Arduino microcontroller board.
4. Click Upload button to upload the Arduino Code to the Arduino
microcontroller board.
The program starts to play the tones that we put in the tones[] array. 10 tones
will be played simultaneously with a 1 second delay after the last tone. The
program plays back the first tone after the last tone.
Page 23 of 56
Vision: A globally competitive university for science, technology, and environmental conservation.
TP-IMD-04
Mission: Development of a highly competitive human resource, cutting-edge scientific knowledge V0 07-15-2020
and innovative technologies for sustainable communities and environment. No. CET.ME
LM21-01
24 MEng 125n: Basic Electronics
References
Page 24 of 56
Vision: A globally competitive university for science, technology, and environmental conservation.
TP-IMD-04
Mission: Development of a highly competitive human resource, cutting-edge scientific knowledge V0 07-15-2020
and innovative technologies for sustainable communities and environment. No. CET.ME
LM21-01