Professional Documents
Culture Documents
Piezo Buzzer and Variables
Piezo Buzzer and Variables
Adding more components to your circuit and assigning them values might make working on your sketch confusing
and look complicated and confusing. One way to help make this easier is to use an important concept in
programming—variables.
A VARIABLE serves as a data storage for the assigned or calculated value in your program. As its name
implies, the value it holds may vary or change depending on the conditions or instructions specified in the program.
A variable requires a data type to identify the kind of data to be stored and a variable name, which will be used.
DECLARING VARIABLES
Before using variables in a program, a variable declaration is required. Declaring a variable requires its data
type and name. Though it is not required, setting the initial value for the variable during variable declaration is
often useful. (note)
NAMING A VARIABLE
Naming a variable is very important since it will be used as reference or label when needed in the program . It is
highly recommended that the variable name must be descriptive enough for you, as a programmer, to
understand and remember when you edit and access your program. The name should be precise yet
descriptive to identify the data it will hold. For example, if you want to assign a variable for two LEDs, it is
recommended to name them as LED1 and LED2. Aside from that, there are also certain rules that you need to
follow in naming variables to avoid syntax error or declaration error in your program.
CONSTANT VARIABLES
Variables that are declared as constant are those variables that do not and must not change their value
throughout the program. These variables are best used to represent where a device is connected in the Arduino
microcontroller code.
VARIABLE SCOPE
Another thing you need to consider when declaring a variable is the part where you will declare it in the program.
The scope of the variable refers to the visibility or accessibility of the variable within the program. The part
where you declare the variable may identify the scope of the variable. Initially, your Arduino program is
composed only of setup( ) and loop( ) functions, but eventually you will be adding more functions as you make
your program more advanced. A function is composed of a block of code that allows the performance of
specific tasks to be executed in your program. A function is useful when you also need to perform the same
task within the program. This can be done by simply calling its function name.
Variables defined at the topmost part of your program to declare it outside all functions are called global
variables. These variables can be used and are accessible to all functions in the entire program.
Local variables, on the other hand, are only declared inside the block of a specific function. This means that
only that particular function can access that variable and will not be recognized outside that function.
PIEZO BUZZER
A transducer plays an important role in an electronic circuit as it converts electrical charges into energy.
The LED that is previously included in the previous circuit can be viewed as a transducer device. Another common
transducer, which will be used in the next circuit, is the piezo buzzer.
A piezo buzzer (or piezo speaker) is a small audio signaling electronic device using a piezoelectric material
that vibrates and moves when it receives electricity, thus producing tones or sounds. It is typically used in
alarm systems, timers, bells, and alert or feedback sounds in a computer input.
TONE ( ) FUNCTION
After setting up a pinMode ( ) to the piezo buzzer, the tone ( ) function is used to allow the piezo buzzer to play
a sound. Adjusting the value for frequency to play around with pitch and sound duration will allow you to generate
a nice audible melody.
The pin connected to the piezo buzzer can only generate one tone at a time. With that, if the tone ( ) is already
playing in a different pin, calling the function with the other pins will have no effect. frequency will be set when
tone ( ) is called on the same pin.
PARAMETERS
Note: Using tone( ) function with PWM output on pins 3 and 11 on boards other than the mega will cause
interference that might affect the tone produced by the buzzer.
Make sure that the frequency value to produce pitch is within the range for the type of board you are using as
shown below. Take note that higher frequency values generate higher pitch sounds.
NO TONE ( ) FUNCTION
When playing different pitches in multiple pins, the noTone ( ) function must be called on one pin before calling
the tone ( ) on the next pin. noTone ( ) function allows the generation of sound created with the tone ( ) function to
stop. In the same way, if the duration is not specified in the tone ( ) function, the sounds will continuously be
generated until the noTone ( ) function is called.
Activity
To create a sketch that uses the piezo buzzer to generate 1 octave of tones from the piano, including the black keys
(sharps and flats), you'll need to determine the frequencies for each of the tones. Here's how you can set up the
variables and create the sketch:
Variables
Firstly, you'll declare the variables representing the frequencies of each tone in the octave.
Sketch
Now, you'll write the sketch to play each tone for a second and then have silence for another second.