This is only a preview of the July 2005 issue of Silicon Chip. You can view 37 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Articles in this series:
Items relevant to "Remote-Controlled Automatic Table Lamp Dimmer":
Items relevant to "Lead-Acid Battery Zapper":
Items relevant to "PICAXE In Schools, Pt.3":
Articles in this series:
Purchase a printed copy of this issue for $10.00. |
By Clive Seager*
Last month, we programmed our Schools Experimenter board
to respond to digital and analog inputs. This month we’ll see how
to use a sophisticated sensor for temperature measurement and
have some fun playing mobile phone ring tones!
In this article you will learn:
• how to make sounds and play
tunes;
• how a for...next loop operates;
• how to connect and use a digital
temperature sensor.
Sounds in electronic projects are
usually generated with an electromechanical or piezo buzzer. We’ll
describe only the piezo type here, as
they use much less power than the
electromechanical types and are well
suited for use in battery-powered
circuits.
A typical piezo buzzer contains
two main parts: a piezoelectric
transducer and its driving circuit.
The piezo transducer consists of a
polarised ceramic material bonded to
a metal disc.
A voltage applied to opposite sides
* About the author: Clive Seager is the
Technical Director of Revolution Education
Ltd, the developers of the PICAXE system.
68 Silicon Chip
of the disc will cause it to flex, so by
applying a varying voltage, it can be
made to vibrate and produce an audible sound. The built-in driving circuit
in a buzzer generally applies a voltage
that varies (or “oscillates”) at a single
frequency, resulting in a monotonous,
irritating tone.
In conjunction with the sound command, PICAXE micros can generate
a voltage at an output pin that oscillates at a programmable frequency. By
connecting a piezoelectric transducer
(without the inbuilt driver circuit) to
that pin, we can therefore make our
own buzzer for signalling purposes –
and much more.
Making sounds
In use, three numbers must follow the sound command. The first is
simply the output pin number that
is to be driven and on the Schools
Experimenter board, this will always
be output 2. The second number is
the desired pitch of the sound and can
be between 0 and 127. As one might
expect, a higher number generates
a higher frequency signal. The third
number is the length of the sound in
approximately 10ms steps.
The simple program in Listing 1
shows how to use the sound command.
In this example, three sequential tones
of equal duration are produced. Listing
2 shows how the three lines can be
combined into a single command to
save PICAXE memory space.
Note that on the Schools Experimenter board, the piezo transducer
and green LED share the same output
(output 2), so the green LED will flicker
as the sound is being generated.
For…next loops
Often it is useful to repeat a section
of BASIC code a number of times. For
example, if you wish to flash the yellow LED (on output 1) five times, you
can use a for...next loop as shown in
siliconchip.com.au
Fig.1: the digital output from the
DS18B20 temperature sensor
connects to input 4 of the PICAXE
micro via the header socket (H1),
as shown here. The sensor also
requires power (+V) and ground
(0V) connections and a 4.7kW
pull-up resistor.
Listing 3. In this example, variable
b1 (b for “byte”) is used as a counter
to keep track of the number of loops
completed.
Task – write a program that flashes the
green LED 10 times when the switch on
input 3 is pressed (you might need to look
at last month’s article about inputs for a
reminder).
We can use the same for…next structure to make the piezo play every possible note using the sound command,
as shown in Listing 4. The second
for…next loop in this example shows
how to repeat the scale in reverse by
using a step value of -1 (the default
step value is +1).
Fig.2: here’s how to wire up the sensor using a small solderless breadboard.
The jumper wires can be purchased in pre-cut lengths, or you can make
your own with solid-core alarm cable and wire strippers.
is lifted (a “birthday box”, perhaps).
As described last month, the program
makes use of the LDR to sense light
level, allowing it to play the tune only
at the appropriate moment.
Two numbers follow the play command, both of which can take the value
0 to 3. The first number represents the
tune (0 = Happy Birthday, 1 = Jingle
Bells, etc). The second number can be
used to flash LEDs connected to output
0 and/or output 4. We will not use this
function in our experiments, so the
second number should always be 0.
Playing your own tunes
The tune command is used when
you want to compose your own tunes
or import them using the Programming
Editor, as we’ll see shortly. The tune
command has the following syntax:
TUNE LED, speed, (note, note, note...)
where:
• LED is a variable/constant (0 -3) that
Playing built-in tunes
The sound command is great
for making simple “beep” noises
but is not well suited for playing musical tunes. Instead, the
PICAXE-08M offers the play and
tune commands for this purpose.
These musical commands operate on output 2 only, so unlike the
sound command, the play and tune
commands do not need an output
pin number.
The play command is used to
play one of the four internally preprogrammed tunes, which are: Happy
Birthday, Jingle Bells, Rudolf the Reindeer and Silent Night. For example,
the program in Listing 5 could be used
to play a tune when the lid of a box
siliconchip.com.au
The 10-way header socket (H1)
allows you to connect external
circuits to the Schools Experimenter board. A solderless breadboard
provides a convenient base on
which to construct your circuits.
July 2005 69
Fig.3: import RTTTL tunes or create
your own with the Tune Wizard,
included in versions 4.0.1 and later of
the PICAXE Programming Editor
specifies if output 0 and/or output 4
toggle at the same time as the tune is
being played. This can be used to flash
LEDs but is not used in our experiments and should always be 0.
• speed is a variable/constant (115) which specifies the tempo of the
Par t s Lis t
1 DS18B20 temperature sensor IC
1 4.7kW 0.25W 5% resistor
1 10-pin SIL header socket
1 breadboard & jumper wire
Where To Buy Parts
The DS18B20 sensor can be
ordered from MicroZed and their
resellers, see www.picaxe.com.
au for more information.
Single-in-line (SIL) header sockets
are available from Altronics (Cat.
No. P-5390). These are supplied in
40-pin sections but are easily cut
down to the desired size.
Breadboards and pre-cut jumper
wire packs are available from major
kit suppliers. Jumper wires of any
length can also be made using
solid-core telephone or alarm cable
and wire strippers.
tune; and
• note, note, etc, are the encoded
musical note data.
A detailed explanation of these parameters is beyond the scope of this
beginner’s series. More information
can be found in the PICAXE BASIC
Commands manual. We also published
details in the “PICAXE the Red-Nosed
Reindeer” project (SILICON CHIP, September 2004).
A good way to get a feel for the tune
command and its capabilities is to
import a ready-composed tune in the
form of a favourite mobile phone ring
tone. It’s actually quite easy to include
ring tones in PICAXE programs. Let’s
see how.
Importing ring tones
With a little help from the Programming Editor and its Tune Wizard, any
monophonic mobile phone ring tone
can be imported and automatically
formatted for use with the tune command.
Ring tones must be in RTTTL (ring
tone text transfer language) format
for use with this system. These are
available from a number of internet
websites, including Revolution Education’s site at www.picaxe.co.uk
An example ring tone in RTTTL
format is given in Fig.4. The first step
to importing a ring tone is to highlight
its text in Internet Explorer (or any text
editor if you have downloaded it as a
file) and use the Edit -> Copy function
to copy it to the Windows clipboard.
Next, select PICAXE -> Wizards ->
PICAXE-08M Tune from the Programming Editor’s main toolbar. This will
open the “Tune Wizard” screen (see
Fig.3). To import the tune data, simply
select Edit -> Paste ring tone. Finally,
click the large “Copy” button to copy
the formatted tune command complete
with ring tone data into your BASIC
program.
As a matter of interest, the program
in Listing 6 provides similar functionality to that in Listing 5 but uses the
tune command instead of the play
command.
Important: in Listing 6, we’ve divid-
Fig.4: Star Wars Theme Tune In RTTTL Format
Star Wars Theme:
d=4,o=5,b=125:1a#,1f6,8d#6,8d6,8c6,1a#6,2f6,8d#6,8d6,
8c6,1a#6,2f6,8d#6,8d6,8d#6,2c6,8f,8f,8f,2a#,2f6,8d#6,8d6,
8c6,1a#6,2f6,8d#6,8d6,8c6,1a#6,2f6,8d#6,8d6,8d#6,1c6
70 Silicon Chip
We found that the jumper wires
were a little loose in the socket, so
we plugged a 10-way header into
the socket first and then soldered
our wires to that to ensure reliable
connections.
ed the note data for the tune command
over several lines but in the Programming Editor, all note data must appear
on a single line.
Task – import a ring tone and write a
program that will play the tune when
the switch is pressed and the light level
(sensed using the LDR) is low.
Digital temperature sensor
Last month, we used the on-board
light-dependant resistor (LDR) and
some simple programs to measure
relative ambient light levels. In theory,
we could use a thermistor (temperature sensitive resistor) and a similar
scheme to measure temperature.
However, thermistors are typically
non-linear devices and our measurements would need considerable manipulation to give accurate results in
degrees Celsius. An easier way is to
use a DS18B20 digital temperature
sensor IC. These devices contain an
accurate temperature measurement
circuit, with a digital output for direct
interfacing to our PICAXE chip.
The DS18B20 sensor’s digital output connects to input 4 of the PICAXE
via the 10-way header socket (see
Fig.1). If you haven’t already installed
this header, then you should do that
next.
Rather than plugging the sensor pins
directly into the header, it is easier to
plug it into a small external breadboard.
Connections between the breadboard
and header can then be made with
solid-core wire, as depicted in the accompanying photograph and on Fig.2.
As shown, a 4.7kW resistor is also required for correct operation.
Note that as input 4 is already used by
the LDR, it must be disconnected from
siliconchip.com.au
Program Listings
Listing 1
main:
sound 2,(50,50)
sound 2,(80,50)
sound 2,(120,50)
pause 1000
goto main
Listing 2
main:
sound 2,(50,50,80,50,120,50)
pause 1000
goto main
Listing 3
main:
for b1 = 1 to 5
high 1
pause 100
low 1
pause 100
next b1
end
Listing 4
main:
for b1 = 0 to 127
sound 2,(b1,20)
next b1
for b1 = 127 to 0 step -1
sound 2,(b1,20)
next b1
goto main
Listing 5
main:
readadc 4,b1
if b1 > 80 then play_tune
goto main
play_tune:
play 0,0
goto main
Listing 6
main:
readadc 4,b1
if b1 > 80 then play_tune
goto main
Silicon Chip
Binders
REAL
VALUE
AT
$12.95
PLUS P
&
play_tune:
tune 0, 6,($AA,$85,$43,$42,$40,$8A,$C5,
$43,$42,$40,$8A,$C5,$43,$42,$43,$C0,
$65,$65,$65,$EA,$C5,$43,$42,$40,$8A,
$C5,$43,$42,$40,$8A,$C5,$43,$42,$43,$80)
goto main
P
Listing 7
main:
readtemp 4,b1
debug b1
pause 100
goto main
Listing 8
main:
readtemp 4,b1
if b1 > 25 then too_hot
if b1 < 15 then too_cold
temp_ok:
low 0
high 1
low 2
goto main
too_hot:
high 0
low 1
low 2
goto main
too_cold:
low 0
low 1
high 2
goto main
These binders will protect your
copies of S ILICON CHIP. They
feature heavy-board covers & are
made from a dis
tinctive 2-tone
green vinyl. They hold up to 14
issues & will look great on your
bookshelf.
H 80mm internal width
H SILICON CHIP logo printed in
gold-coloured lettering on spine
& cover
H Buy five and get them postage
free!
Price: $A12.95 plus $A7 p&p per
order. Available only in Aust.
Silicon Chip Publications
PO Box 139
Collaroy Beach 2097
Or fax (02) 9979 6503; or ring (02)
9979 5644 & quote your credit
card number.
Use this handy form
Enclosed is my cheque/money order for
the on-board circuits by moving position 4 of the 4-way switch (SW2) to the
“off” position (leave all other contacts
in the “on” position for now).
The program in Listing 7 is almost
the same as the LDR test program from
last month, except that we’re using
the readtemp command here instead
of readadc. The readtemp command
reads the temperature value from the
DS18B20 sensor, which is then displayed on the computer screen with
the aid of the debug command.
The final program this month (Listsiliconchip.com.au
ing 8) demonstrates how your system
can act as “cold-warm-hot” indicator,
using only the DS18B20 temperature
sensor and the three on-board LEDs.
Task – write a program that plays a musical tune when the temperature drops
below 20°C.
$________ or please debit my
Bankcard
Visa Mastercard
Card No:
_________________________________
Card Expiry Date ____/____
Next month
Signature ________________________
Next time around, we’ll look at
controlling devices that move. Motors,
servos and solenoids are all on the
menu, so start looking through that
SC
junk box for motors to salvage!
Name ____________________________
Address__________________________
__________________ P/code_______
July 2005 71
|