| OOPic to Generate Sounds |
Last Modified: 2006-11-20
This note describes how to generate tones and buzzing sounds by using an OOpic and a piezo transducer. Nancy , a robot here at Acroname, uses this technique to communicate with her human handlers. (This technique is not necessary on the new OOpic II because it has an oTone object for generating sounds.)
Pulse Width Modulation, or PWM , is typically used to control the speed of motors or the brightness of incandescent lights. The most basic form of PWM is a rectangular wave of a constant frequency with a carefully-controlled ratio of "on-time" to "off-time". More "on-time" makes a motor go faster or a light appear brighter. If the frequency is low enough, the PWM signal can drive an audio transducer and create tones.
The OOpic is cabable of sending a PWM signal to two of its pins. The oPWM object performs this function. It has three parameters that control the PWM signal. A Pre-scaler parameter sets the base frequency. A Period parameter further divides this frequency. A Value parameter determines the amount of "on-time" during one cycle. For this application, the Value parameter provides some control of the volume of the sound. For more information about the oPWM object, see the OOPic Web Site .
The oPWM object can generate audible frequencies with the appropriate settings. The maximum Pre-scaler is 16 which sets the lowest base frequency to 312.5KHz. The maximum Period is 255. This produces a minimum frequency of about (312.5KHz/255) 1225 hertz. The OOpic can play a reasonably good musical scale. The notes are listed in the example program below. (Because of integer division, the approximations to actual musical notes become increasingly bad beyond a Period of 126.) It is also possible to get a buzzing or clicking noise by simply turning the oPWM object on and off in a tight loop. For the tones, a Value of 8 limits the volume to a comfortable level. For the buzzing noises, a period of 255 and a Value of 255 ensure that the oPWM output will be high when the oPWM is activated.
The only additional component needed is a piezo transducer. Part 273-073 from Radio Shack works quite well. Other piezo transducers should work, but be sure they are the type that require a driver circuit. Connecting the piezo transducer is very simple. Crimp a connector to each wire. Connect the black wire to ground (pin 23) and the red wire to I/O Line 17 (pin 27). The program lists the ioline as 1 instead of 17. Either value can be used. This program compiles with version 3.0.4 of the OOpic compiler. With later versions of the compiler, you may need to change the oByte object declarations in the functions to Byte type declarations. Run the test program, and your OOpic should play a musical scale and make a rude noise.
' file: sound.osc ' OOPic Sound Using PWM ' dim pp as new oPwm dim ii as new oByte sub main() call initsound do ' play a musical scale call tone(126,20) ' Eb call tone(133,20) ' D call tone(141,20) ' C# call tone(149,20) ' C call tone(158,20) ' B call tone(168,20) ' Bb call tone(177,20) ' A call tone(188,20) ' G# call tone(199,20) ' G call tone(211,20) ' F# call tone(224,20) ' F call tone(237,20) ' E call tone(251,20) ' Eb oopic.delay=50 call rude oopic.delay=100 loop end sub sub initsound() pp.ioline=1 pp.prescale=2 pp.period=255 pp.value=8 end sub sub tone(bPer as oByte, bDur as oByte) ' ' Frequency=312500/bPer ' bPer=177 produces a tone of about 1765Hz ' which is close to two octaves ' beyond "A above Middle C" (440 Hz) ' pp.period=bPer pp.operate=cvTrue oopic.delay=bDur pp.operate=cvFalse end sub sub buzz(bCyc as oByte) pp.period=255 pp.value=255 for ii=1 to bCyc pp.operate=cvTrue pp.operate=cvFalse next ii pp.value=8 end sub sub clik(bTmr as oByte, bNum as oByte) pp.period=255 pp.value=255 for ii=1 to bNum pp.operate=cvTrue pp.operate=cvFalse oopic.delay=bTmr next ii pp.value=8 end sub sub rude() call buzz(20) call clik(2,10) call clik(8,4) end sub
| voice: 720-564-0373, email: firstname.lastname@example.org, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy |
© Copyright 1994-2012 Acroname, Inc., Boulder, Colorado. All rights reserved.