In this section, you will learn how to use a new part called a piezo speaker as well as new code that will allow you to generate frequencies from the BS2.

| 3D model of piezo speaker on breadboard |
Building the circuit 1 Piezo speaker GP-1404. Jumper wires Introducing a New Part Piezo speakers are small speakers that you may be more familiar with than you know as they are the speakers used in smoke detectors and can be very loud if properly amplified. They have a much faster response time than magnetically based speakers because they are instead constructed of quartz crystals, which vibrate diaphragms. Quartz crystals will vibrate if given a small voltage and this causes the diaphragm to vibrate, which then induces the air to vibrate, creating the audible tone. We can therefore call these transducers, as they change an electrical signal into a mechanical motion, vibrating the air to the frequency of the output signal. Piezo speakers can also be used as sensors and you will often find these devices glued to glass as glass shatter sensors. They are also sometimes used as pickups for guitars or cellos, and all of these piezos are designed to respond to specific frequencies. The piezo speaker used in this lesson is designed to respond to higher frequencies and therefore, as you use the code you will notice that for higher frequencies of sound it is much louder and piercing, and for lower sounds not as loud. Many artists and inventors use piezo speakers as simple indicators so that when their software subroutines hit a certain point in the code a certain frequency will play, or if it hits another point in the code another frequency may play. Some use piezo speakers as audio indicators that the system is running well and they will put the FREQOUT code at the beginning of their programs for this purpose. Artist/inventors also use piezo speakers to give the system a sense of voice such as the artist/inventor Fernando Orellana uses to allow some of his robots to seem as if they are chattering to each other. Sound is a most important aspect in creating an immersive aspect to your work, and while the chatter of the piezo speaker may not be exactly what you are looking for there are other options, which will be mentioned at the end of this chapter. Programming the Project The Pbasic command FREQOUT allows you to generate sine-wave tones for a duration of time that you can specify. Freqout allows you to generate one of two sine-wave tones by using brackets to separate the tones. FREQOUT Pin, Period, Freq1 {, Freq2}Introductory Example Program: The syntax of FREQOUT is: • Pin is a variable or constant between (0 – 15) that specifies the input output pin you would like to use. FREQOUT automatically sets the pin to output mode so you do not need to also do this in your code. • Period is a variable or a constant or an expression between (0 - 65535) which tells the BS2 the length of time to generate the tone(s). • Freq1 is a variable, a constant, or an expression between (0 – 32767) which tells the BS2 the frequency of the first tone you would like to hear. • Freq2 is an optional tone you can play just like Freq1, however, when you put this after the first frequency, the two tones will be played together creating a kind of chortle. Example
Freqout _3200Hz FREQOUT 15, 2000, 3200 On the BS2, this command would generate a 3200 Hz tone for 2 seconds on the pin 15. In this first program, you will experiment with a main frequency of 500 Hz and a secondary frequency of both 300 and 100000 Hz consecutively so you can hear what a warning signal may sound like from the Piezo speaker. Type in the code below: ' {$STAMP BS2} ' {$PBASIC 2.5}
Start: FREQOUT 0,500,300 Pause 50 FREQOUT 0,500,10000 Pause 50 Goto start In this second program, you will experiment with a number of different frequencies, which will allow you to hear the range of sound possible and some of the rhythmic possibilities of the BS2 and the piezo speaker.
Type in the code below ' {$STAMP BS2} ' {$PBASIC 2.5}
Start: FREQOUT 0,200,1318 Pause 50 FREQOUT 0,300,287 Pause 50 FREQOUT 0,500,1174 Pause 50 FREQOUT 0,600,1318 Pause 50 FREQOUT 0,200,1174 Pause 50 GOTO start In this third program, you will experiment with a number of different frequencies, however you will choose these frequencies with a bit of code called RANDOM. RANDOM is an important method to add complexity to all of your works and it is fun to see how this manifests complex sound in the BS2 and piezo speaker. In order to use RANDOM, specify the command RANDOM and then a variable, which you must specify such as: RANDOM variable Variable is usually a word whose data bits will be mixed up to produce what we call a pseudo-random number. We call this pseudo-random because while it may not appear to repeat, the algorithm that produces complex randomness really does repeat over time, which makes it a pseudo-random number ranging from 0 to 65535. After you type in the program, see if you can detect patterns.
Type in the code below ' {$STAMP BS2} ' {$PBASIC 2.5}
a VAR Word B VAR Word C VAR Word D VAR Word Start: FREQOUT 0,500,300,292 PAUSE 50 FREQOUT 0,50,1000 FREQOUT 0,500,600,592 PAUSE 100 FREQOUT 0,50,5000 FREQOUT 0,50,6000 RANDOM a RANDOM d RANDOM c RANDOM d FREQOUT 0,200,a FREQOUT 0,100,b FREQOUT 0,200,c FREQOUT 0,100,d GOTO start In this third program you will experiment with a number of different frequencies, however you will choose these frequencies both with the Pbasic code RANDOM and you will be using some simple algebra to cause even more complexity. I am guessing that with the simple algebra you will find it more difficult to detect the pseudo-random sounds. Notice in the code how the phrases ((a + x)/2) + a/2 need to be place in brackets in order for the Pbasic program to recognize what you want to achieve. Now enter the program below ' {$STAMP BS2} ' {$PBASIC 2.5}
A VAR Word X VAR Word C VAR Word START: RANDOM a RANDOM x RANDOM c FREQOUT 0,150,a FREQOUT 0,20,((a+x)/2)+a/2 FREQOUT 0,50,(a+x)/10 FREQOUT 0,20,((a+x))/2+x/2 FREQOUT 0,200,x PAUSE 50 GOTO START
|