Piezo Beep
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.
Circuit
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.
Parts
(1) Piezo speaker (#900-00001)
- Plug your piezospeaker into the board as shown.
- Make sure when you connect the wires so they go into the same rows with the piezospeaker pins. Note that the Activity Board and FLiP diagrams use different peizo orientations, but the circuit connections are the same.
Note: if you are using a different Propeller board, build the circuit using the schematic on the right of the top image.
Test Code
The test code will make the piezospeaker beep for one second with fairly high-pitched tone.
- Click SimpleIDE’s Open Project button.
- Open Piezo Beep from …DocumentsSimpleIDELearnExamplesCircuits.
- Set the power switch to position 1 (if applicable for your board).
- Click the Load RAM & Run button.
- Verify that the speaker made a one-second beep.
How it Works
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: …DocumentsSimpleIDELearnSimple LibrariesUtilitylibsimpletoolsDocumentation simpletools Library.html
Did You Know?
- Frequency is a measure of how many times something happens in a second, and it is measured in hertz (Hz).
- kilo — k is an exmaple of a metric or SI prefix. This one abbreviates kilo, which means 1000.
- kHz — In the case of our piezospeaker, it received an on/off signal that repeated 3000 times per second, so that’s 3000 Hz, or 3 kHz.
- Human-audible sound vibrations range from around 20 Hz (very low pitch) to 20 kHz (very high pitch). Test your hearing!
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:
Try This
You can use an array to store note values, and then play them with a for loop. (See Array Variables for a refresher.)
- Use the Save Project As button to save a copy of your project in …DocumentsSimpleIDEMy Projects.
- Make the additions and modification shown here.
- Run the program, and it should play a familiar tune.
Your Turn
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.
- Try your hand at composing your own tune by combining sequences of notes and durations.
- Can you use the floating point library to play each notes’ exact frequency?