Chapter 4
Adding Sound and Motion to Your BASIC Stamp Projects
In This Chapter
Making noise with a piezo speaker
Creating movement with a servo
Everyone enjoys being able to create a din from time to time (except perhaps Trappist monks), and so in this chapter we lead you through working with devices that add sound (as well as motion) to your BASIC Stamp projects.
To make some noise, you can add a piezo speaker to create audible output tones, something that’s useful when your BASIC Stamp program needs to get a person’s attention or you want to create a sound effect. To produce movement, you can add a very useful device called a servo, which lets you control mechanical motion with a BASIC Stamp program.
Creating Sound with a Piezo Speaker and a BASIC Stamp
The BASIC Stamp Activity Kit (to which we introduce you in Chapter 1 of this minibook) comes with a small piezoelectric speaker, which you can connect directly to an I/O pin to create beautiful music. Well, perhaps the music is less than beautiful, but you can coax the BASIC Stamp into emitting a variety of squeaks, burps and squelches that resemble musical notes. Plus you can create interesting sound effects such as police sirens or chirping crickets. Figure 4-1 shows this handy little speaker.
Figure 4-1: The piezoelectric speaker that comes with the BASIC Stamp Activity Kit.
If you haven’t purchased the BASIC Stamp Activity Kit, you can order a similar component (specification around 5 Vp-p rated voltage square wave, 1 mA, 4 kHz) from online component distributors for just a few pounds.
Figure 4-2: Connecting a piezo speaker to a BASIC Stamp I/O pin.
Freaking out with the FREQOUT command
Programming a piezo speaker is remarkably simple. PBASIC includes a command called FREQOUT
that sends a frequency of your choice to an output pin. So you can create an audible tone on a piezo speaker by using the FREQOUT
command and the following syntax:
FREQOUT
pin
, duration
, frequency
Here’s an explanation:
pin is the pin number to which you want to send the frequency.
duration is the length of time in milliseconds (ms) you want the frequency to play.
frequency is the frequency in hertz that you want to generate.
For example, the following command generates a 2,000 hertz (Hz) frequency for 5 seconds on pin 8:
FREQOUT 8, 5000, 2000
You can easily create a beeping sound by alternately sending short bursts of a frequency to the speaker followed by a brief pause. For example:
DO
FREQOUT 8, 250, 1500
PAUSE 250
LOOP
This code repeatedly sends a 1,500 Hz signal for a quarter of a second, and then pauses for a quarter of a second. The result is a beep-beep-beep sound that isn’t irritating at all!
Testing the piezo speaker
Project 4-1 shows how to build a simple circuit that connects a piezo speaker to a BASIC Stamp so that you can create audible output; two pushbuttons vary the sound output. Figure 4-3 illustrates the circuit.
The piezo speaker is very quiet. It draws just 1 milliampere (mA) and so can’t make a lot of noise. The speaker is loudest with frequencies between 4,500 and 5,500 Hz.
Figure 4-3: A piezo speaker connected to a BASIC Stamp (Project 4-1).
Playing with sound effects
With creative use of the FREQOUT
command, PAUSE
commands and FOR-NEXT
loops, you can create some interesting and at times wonderfully annoying sound effects. The idea is to use short durations in the FREQOUT
command and use FOR-NEXT
loops or some other means to vary the frequency. You can also use PAUSE
commands between tones to create beeping or clicking effects. We explain these commands in this minibook's Chapter 2.
The program in Listing 4-1 plays two different beeping sounds when you press one of the pushbuttons. If you press Switch1
(on pin 14), a 5,000 Hz tone beeps twice a second. If you press Switch2
(on pin 10), a 5,000 Hz tone beeps five times a second.
Listing 4-1: Generating Two Different Types of Beeping Sounds
' Sound Program
' Doug Lowe
' July 15, 2011
'
' This program creates fast and slow beeping sounds.
' A piezo speaker must be connected to pin 0.
' The normally open pushbutton switches must be connected to pins 10 and 14.
' {$STAMP BS2}
' {$PBASIC 2.5}
Speaker PIN 0
Switch1 PIN 10
Switch2 PIN 14
Frequency VAR Word
Time VAR Word
DO
IF Switch1 = 1 THEN
FREQOUT Speaker,250, 5000
PAUSE 250
ELSEIF Switch2 = 1 THEN
FREQOUT Speaker,100, 5000
PAUSE 100
ENDIF
LOOP
Listing 4-2 shows how you can use FREQOUT
within a FOR-NEXT
loop to create a continuously rising or falling tone, much like a police siren. The program varies the frequency from 3,000 to 5,000 Hz. When you press either of the pushbuttons, the rate at which the pitch rises and falls changes.
This rising or falling is governed by a variable named Time
. Each time it runs through the FOR-NEXT
loop, the program calls a subroutine named GetTime
, which checks the status of the pushbutton switches and changes the Time
variable if either of the switches is down. In this way the program alters the rate of the pitch change when the buttons are pressed.
Listing 4-2: Generating a Siren Effect
' Siren Effect Program
' Doug Lowe
' July 15, 2011
'
' This program generates a rising and falling pitch similar to a police siren.
' The rate at which the pitch rises and falls changes if you press either
' of the two pushbuttons.
' {$STAMP BS2}
' {$PBASIC 2.5}
Speaker PIN 0
Switch1 PIN 10
Switch2 PIN 14
Frequency VAR Word
Time VAR Word
DO
FOR Frequency = 3000 TO 5000 STEP 15
GOSUB SetTime
FREQOUT 0, Time, Frequency
NEXT
FOR Frequency = 5000 TO 3000 STEP 15
GOSUB SetTime
FREQOUT 0, Time, Frequency
NEXT
LOOP
SetTime:
Time = 15
IF Switch1 = 1 THEN
Time = 5
ENDIF
IF Switch2 = 1 THEN
Time = 2
ENDIF
RETURN
Listing 4-3 shows a program that plays two songs on the piezo speaker: 'Mary had a little lamb' and 'Good morning to all'. The former is played when you press SW1
and the latter when you press SW2
.
To simplify the code that generates the musical notes, the program defines several constants that represent the frequency for each of the notes required by the songs. For example, the constant NoteC6
is 1,046, the frequency in Hz of C in the sixth octave of a piano keyboard. The constants span two full octaves, which is plenty of range for the songs to be played. Both songs are played in the key of C, and so no flats or sharps are required. (If this musical stuff makes no sense to you, don't worry about it – this is an electronics book after all, not one for budding songwriters.)
The program also sets up constants for the duration of a quarter note, half note and whole note. The constants make specifying a particular pitch for a particular duration easy in a FREQOUT
command. Thus, playing a melody is simply a matter of writing a sequence of FREQOUT
commands to play the correct notes for the correct durations in the correct order. That's precisely what the subroutines labelled Mary
and Morning
do.
Listing 4-3: Making Music with a BASIC Stamp
' Song Program
' Doug Lowe
' July 15, 2011
'
' This program plays one of two songs on the piezo speaker
' on pin 0.
' If SW1 on pin 14 is pressed, the program plays "Mary Had a Little Lamb."
' If SW2 on pin 10 is pressed, the program plays "Good Morning to All."
' {$STAMP BS2}
' {$PBASIC 2.5}
SW1 PIN 14
SW2 PIN 10
Speaker PIN 0
NoteC6 CON 1046
NoteD6 CON 1175
NoteE6 CON 1318
NoteF6 CON 1370
NoteG6 CON 1568
NoteA6 CON 1760
NoteB6 CON 1975
NoteC7 CON 2093
NoteD7 CON 2349
NoteE7 CON 2637
NoteF7 CON 2794
NoteG7 CON 3136
NoteA7 CON 3520
NoteB7 CON 3951
NoteC8 CON 4186
Whole CON 1000
Half CON 500
Quarter CON 250
DO
IF SW1 = 1 THEN
GOSUB Mary
ENDIF
IF SW2 = 1 THEN
GOSUB Morning
ENDIF
LOOP
Mary:
FREQOUT Speaker, Quarter, NoteE7 ' Mar-
FREQOUT Speaker, Quarter, NoteD7 ' y
FREQOUT Speaker, Quarter, NoteC7 ' Had
FREQOUT Speaker, Quarter, NoteD7 ' a
FREQOUT Speaker, Quarter, NoteE7 ' Lit-
FREQOUT Speaker, Quarter, NoteE7 ' tle
FREQOUT Speaker, Quarter, NoteE7 ' Lamb
PAUSE Quarter
FREQOUT Speaker, Quarter, NoteD7 ' Lit-
FREQOUT Speaker, Quarter, NoteD7 ' tle
FREQOUT Speaker, Quarter, NoteD7 ' Lamb
PAUSE Quarter
FREQOUT Speaker, Quarter, NoteE7 ' Lit-
FREQOUT Speaker, Quarter, NoteG7 ' tle
FREQOUT Speaker, Quarter, NoteG7 ' Lamb
PAUSE Quarter
FREQOUT Speaker, Quarter, NoteE7 ' Mar-
FREQOUT Speaker, Quarter, NoteD7 ' y
FREQOUT Speaker, Quarter, NoteC7 ' Had
FREQOUT Speaker, Quarter, NoteD7 ' a
FREQOUT Speaker, Quarter, NoteE7 ' Lit-
FREQOUT Speaker, Quarter, NoteE7 ' tle
FREQOUT Speaker, Quarter, NoteE7 ' Lamb
FREQOUT Speaker, Quarter, NoteE7 ' Its
FREQOUT Speaker, Quarter, NoteD7 ' Fleece
FREQOUT Speaker, Quarter, NoteD7 ' Was
FREQOUT Speaker, Quarter, NoteE7 ' White
FREQOUT Speaker, Quarter, NoteD7 ' As
FREQOUT Speaker, Quarter, NoteC7 ' Snow
PAUSE Half
RETURN
Morning:
FREQOUT Speaker, Half, NoteC7 ' Good
FREQOUT Speaker, Half, NoteD7 ' Morn-
FREQOUT Speaker, Half, NoteC7 ' ing
FREQOUT Speaker, Half, NoteF7 ' To
FREQOUT Speaker, Whole, NoteE7 ' You
FREQOUT Speaker, Half, NoteC7 ' Good
FREQOUT Speaker, Half, NoteD7 ' Morn-
FREQOUT Speaker, Half, NoteC7 ' ing
FREQOUT Speaker, Half, NoteG7 ' To
FREQOUT Speaker, Whole, NoteF7 ' You
FREQOUT Speaker, Half, NoteC7 ' Good
FREQOUT Speaker, Half, NoteC8 ' Morn-
FREQOUT Speaker, Half, NoteA7 ' ing
FREQOUT Speaker, Half, NoteF7 ' Dear
FREQOUT Speaker, Half, NoteE7 ' Child-
FREQOUT Speaker, Whole, NoteD7 ' ren
FREQOUT Speaker, Half, NoteB7 ' Good
FREQOUT Speaker, Half, NoteA7 ' Morn-
FREQOUT Speaker, Half, NoteF7 ' ing
FREQOUT Speaker, Half, NoteG7 ' To
FREQOUT Speaker, Whole, NoteF7 ' All
RETURN
Moving by Degrees with a Servo and a BASIC Stamp
A servo is a special type of motor designed to rotate to a particular position and hold that position until told to rotate to a different one. Hobby servos are frequently used in radio-controlled vehicles such as airplanes, boats and cars, but many other uses for servos also exist.
The BASIC Stamp Activity Kit comes with a servo that you can use to practise writing programs. You can also purchase servos from the UK distributors listed by Parallax (www.parallax.com
) or from electronics shops such as Maplin. Figure 4-4 shows a hobby servo.
Figure 4-4: A typical hobby servo.
Connecting a servo to a BASIC Stamp
Servos use a special three-conductor cable to provide power and a control signal that tells the servo what position it should move to and hold. The cable’s three wires are coloured red, black and white and have the following functions:
Red wire: Supplies the voltage required to operate the servo. For most servos, this voltage can be anywhere between +4 V and +9 V. On a BASIC Stamp HomeWork board, you need to connect this cable to one of the Vdd pins.
Black wire: The ground connection. On the BASIC Stamp HomeWork board, you connect it to a Vss pin.
White wire: The control wire, which connects to one of the BASIC Stamp’s I/O pins.
Figure 4-5 shows how to connect these wires in a BASIC Stamp circuit.
The control wire controls the position of the servo by sending it a series of pulses approximately 20 ms apart. The length of each one of these pulses determines the position that the servo rotates to and holds.
Most hobby servos have a range of motion of 180° – that is, half a complete revolution. The range of pulse durations is 0.5–2.5 ms, where 0.5 ms pulses move the servo to its minimum position (0°) and 2.5 ms pulses move the servo to its maximum position (180°). To hold the servo at the centre point of this range (90°), the pulses need to be 1.5 ms in duration.
To connect a servo to a BASIC Stamp HomeWork Board, you have to use a 3-pin header, which comes with the BASIC Stamp Activity Kit and is pictured in Figure 4-6. This header consists of three pins that you can plug in to the solderless breadboard. Then, you can plug the servo cable into the adapter.
Figure 4-5: Connecting a servo to a BASIC Stamp.
Figure 4-6: 3-pin header for connecting the servo to a BASIC Stamp HomeWork Board.
Programming a servo in PBASIC
The easiest way to control a servo from a BASIC Stamp microcontroller is to use the PULSOUT
command. This command sends a pulse of any duration you specify to an I/O pin of your choosing. The syntax of this command is as follows:
PULSOUT
pin
, duration
You specify the duration in units of 2 microseconds (µs). A microsecond is one-millionth of a second and so one thousand µs make up 1 ms: therefore 2 µs equal 500 µs. Thus, to send a 1.5 ms pulse with the PULSOUT
command, you need to specify 750 (1.5 × 500) as the duration, as follows:
PULSOUT 0,750
Here, a 1.5 ms pulse is sent to pin 0.
To make your servo-programming life easier, Table 4-1 lists the duration values to use for a typical hobby servo for various angles.
For example, to move the servo on pin 0 to 75°, use this command:
PULSOUT 0,667
DO
PULSOUT 0,500
PAUSE 20
LOOP
Listing 4-4 shows a complete program that moves the servo to 45° when SW1
(a pushbutton on pin 14) is pressed and 135° when SW2
(a pushbutton on pin 10) is pressed.
Listing 4-4: A Servo Control Program
' Servo Control Program
' Doug Lowe
' July 15, 2011
'
' This program moves a servo to one of two positions when SW1 is pressed
' and returns the servo to centre position when SW2 is pressed.
' {$STAMP BS2}
' {$PBASIC 2.5}
Servo PIN 0
SW1 PIN 14
SW2 PIN 10
Position VAR Word
Position = 500
DO
IF SW1 = 1 THEN
Position = 500
ENDIF
IF SW2 = 1 THEN
Position = 1000
ENDIF
PULSOUT Servo, Position
PAUSE 20
LOOP
Building a servo project
Project 4-2 shows you how to build a complete circuit that uses a servo as well as two pushbuttons. This circuit is capable of running the program shown in Listing 4-4. Figure 4-7 depicts the assembled project.
Figure 4-7: A BASIC Stamp project that controls a servo (Project 4-2).