Many devices beep to get your attention. Microwave ovens let you know your food is ready, alarm system keypads respond when you press a number key, and smoke alarms warn you something is burning. These devices often use piezospeakers because they are inexpensive, yet make clear, audible tones. If you make a project that needs to beep to get someone’s attention, consider using a piezospeaker.
Ever notice how a guitar string has to vibrate to make sound? The same applies to the element in a piezospeaker (short for piezoelectric speaker). The piezoelectric element in the speaker bends when voltage is applied to it. If you turn the voltage on and off rapidly, it will vibrate rapidly and make a sound.
It's easy to program the Propeller to make an I/O pin turn on/off rapidly. Let's connect a piezospeaker's positive (+) terminal to P4, and its unmarked negative terminal to GND.
(1) Piezo speaker (#900-00001)
Note: if you are using a different Propeller board, build the circuit using the schematic on the right of the top image.
The test code will make the piezospeaker beep for one second with fairly high-pitched tone.
The freqout function has parameters of pin, msTime, and frequency. The function call freqout(4, 1000, 3000) sends an alternating high/low signal to pin 4, for 1000 ms (or 1 second), at a frequency of 3000 Hz (3000 times per second). Since the speaker is connected to P4, this rapid 3.3 V to 0 V high/low sequence makes the piezoelectic element in it vibrate 3000 times per second. The result is a high-pitched tone.
/* Piezo Beep.c Beep a piezo speaker connected to P4. */ #include "simpletools.h" // Include simpletools int main() // main function { freqout(4, 1000, 3000); // pin, duration, frequency }
If you want to make your code do something while it's broadcasting the tone, use the squre_wave function instead. It's the "set it and forget it" version of freqout. Check the simpletools library documentation for more info. You can find it at: ...\Documents\SimpleIDE\Learn\Simple Libraries\Utility\libsimpletools\Documentation simpletools Library.html
When you press a key on a piano, it makes a small hammer strike a tightly pulled wire, causing the wire to vibrate at a certain frequency. Here are the frequencies of some higher keys to the right side of the piano keyboard:
You can use an array to store note values, and then play them with a for loop. (See Array Variables [1] for a refresher.)
You can also use #define to give your frequencies note names, and then you can use those note names in your array in place of the numbers. It’ll make your digital composing a lot easier.
Links
[1] https://learn.parallax.com/node/620