In this article you will learn:
-
how to make sounds and play tunes;
- how a for...next loop
operates;
- how to connect and use a digital temperature
sensor.
Sounds in electronic projects are usually generated with an
electromechanical or piezo buzzer. We’ll describe only the piezo type here, as
they use much less power than the electromechanical types and are well suited
for use in battery-powered circuits.
A typical piezo buzzer contains two main parts: a
piezoelectric transducer and its driving circuit. The piezo transducer consists
of a polarised ceramic material bonded to a metal disc.
A voltage applied to opposite sides of the disc will cause it
to flex, so by applying a varying voltage, it can be made to vibrate and produce
an audible sound. The built-in driving circuit in a buzzer generally applies a
voltage that varies (or "oscillates") at a single frequency, resulting in a
monotonous, irritating tone.
In conjunction with the sound command, PICAXE micros can
generate a voltage at an output pin that oscillates at a programmable frequency.
By connecting a piezoelectric transducer (without the inbuilt driver circuit) to
that pin, we can therefore make our own buzzer for signalling purposes – and
much more.
Making sounds
In use, three numbers must follow the sound command. The
first is simply the output pin number that is to be driven and on the Schools
Experimenter board, this will always be output 2. The second number is the
desired pitch of the sound and can be between 0 and 127. As one might expect, a
higher number generates a higher frequency signal. The third number is the
length of the sound in approximately 10ms steps.
The simple program in Listing 1 shows how to use the
sound command. In this example, three sequential tones of equal duration
are produced. Listing 2 shows how the three lines can be combined into a single
command to save PICAXE memory space.
Note that on the Schools Experimenter board, the piezo
transducer and green LED share the same output (output 2), so the green
LED will flicker as the sound is being generated.