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)
(2) Jumper wires
The Audio > Frequency out block can make piezospeaker beep.
The frequency out block has three fields to set. PIN specifies the Propeller I/O pin connected to the piezospeaker, 4 in our circuit. The duration field sets how long the beep will last, 1000 ms (1 second) in this example. The frequency (Hz) field sets the pitch of the beep, 3000 Hz in this example. To generate this frequency, the Propeller sets P4 to an output and rapidy toggles it between 0 V to 3.3 V, causing the piezoelectric element inside the speaker to vibrate 3000 times per second. The result is a high-pitched tone.
To play a tune, you could use a stack of frequency out blocks, one block per note played. This works, but makes for a very long program that is cumbersome to edit. This next example stores a whole list of frequency values in an array, which are then accessed and played in order using a repeat item loop.
First, the array initialize block sets an array we named Notes, with 8 elements. The next block fills the Notes array with eight values. These are the frequencies for notes C8 to C7, taken from the keyboard chart above. Each value can be retrieved from the array later, by its index number. With arrays, index counting starts at zero, going from left to right. So, this eight-element array has an index range of 0 to 7, with the 0th element being 4160, and the 7th element being 2093.
Next comes a repeat item loop, with a variable named play to keep track of the trips thorugh the loop. The value of play starts at zero and increases by one each time up through trip 7, for a total of eight repetitions.
Inside the loop, the frequency out block is using PIN 4, and a duration of 500 ms. But instead of a number value block, the frequency (Hz) field has an array get element block attached: array Notes element play. This means "from the Notes array, use the element at the index position play."
The first time through the loop, play equals 0. The 0th element in the Notes array is 4160 (since counting starts at zero). So, the piezospeaker emits a 4186 Hz frequency. The next time through the loop, play increments by one, so the frequency at index position 1 is used: 3951 Hz. In this manner, the loop plays each frequency in the array until play equals 7, and you hear the last frequency of 2093 Hz.
The last example program, each note played for the same length of time, 500 ms. You can make it more interesting by playing notes for different durations. Just set up a second array to hold duration values, and use a second get array get element block for the