Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

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)

Data Type of a Variable


As stated earlier, specifying the data type of a variable is needed in declaring a variable. Aside from identifying
the kind of data to be stored, it also specifies the amount of space or RAM usage that needs to be allotted for it
in the storage. Data type in Arduino programming is almost the same as other common programming languages
like in C++ programming

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.

RULES IN NAMING VARIABLES


-The first character of a variable name must be a letter either uppercase and lowercase followed by a
combination of any letters and numbers. An underscore is the only acceptable special character.
-The use of an underscore to start a variable name is not recommended since it might conflict with the
computer system name and cause an error.
-A variable name can be of any length, though the compiler only checks the first 31 characters of a variable.
INITIALIZING VARIABLES
Initializing a variable means to assign a starting value to a variable during declaration or within the
program. As a good programming practice, you need to ensure that a variable has valid data in it. Assignment
operator will be used to assign a specific value to be stored in that variable. This can be done using a single
equal sign (=) after the name of the variable.

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.

Multiple Variables Declaration


Variables with the same data type can be declared in a single line to save space on your final output file .
Each variable declaration must be separated by a comma (,).

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.

You might also like