Generating a sound A Piezo diaphragm can be added to an output line of a microcontroller to deliver a whole range of tones, beeps and signals. It is important to know there are two main types of piezo sound-emitting devices. One has active components inside the casing and only requires a DC supply for the "sounder" to emit a tone or beep. Generally the tones or beeps emitted by these "sounders" or "beepers" cannot be changed - they are fixed by the internal circuitry. This is not the type we are discussing in this article. The other type consists of a piezo diaphragm and requires a signal to be delivered to it for it to function. Depending on the frequency of the waveform, the output can be a tone, tune, alarm or even voice messages. In order for them to work we must deliver a cycle consisting of a HIGH and LOW. It is the change from HIGH to LOW or LOW to HIGH that causes the diaphragm to "dish" (move) to produce the characteristic "tinny" sound. The waveform can be a smooth change from one value to the other (called a sinewave) or a fast change (called a SQUARE WAVE). A computer is ideal for producing a square wave. The square wave delivery produces a slightly harsher output. Connecting a piezo diaphragm is very simple. One pin is connected to the negative rail and the other to an output of a microcontroller, as shown in the diagram below. This will deliver a 5v waveform to the piezo diaphragm. To produce a higher output, the waveform must be increased and this requires a driver transistor and inductor. Connecting a piezo diaphragm to a microcontroller As with a key, you can employ a macro that will deliver a BEEP ROUTINE into a program when needed. BEEP macro has two arguments: freq: frequency of the sound. The higher number produces higher frequency duration: sound duration. The higher the number, the longer the sound. Example 1: BEEP 0xFF, 0x02 The output of the piezo diaphragm has the highest frequency and duration at 2 cycles per 65.3mS which gives 130.6 mS Example2: BEEP 0x90, 0x05 The output of the piezo diaphragm has a frequency of 0x90 and duration of 5 cycles per 65.3mS. It is best to determine these macro arguments through experimentation and select the sound that best suits the application. The following is the BEEP Macro listing: The following example shows the use of a macro in a program. The program produces two melodies which are obtained by pressing T1 or T2. Some of the previously discussed macros are included in the program.