This is only a preview of the June 2005 issue of Silicon Chip. You can view 39 of the 112 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 "The Coolmaster Fridge/Freezer Temperature Controller":
Items relevant to "PICAXE Colour Recognition System":
Items relevant to "PICAXE In Schools, Pt.2":
Articles in this series:
Purchase a printed copy of this issue for $10.00. |
By Clive Seager*
Last month, we assembled our Schools Experimenter board,
installed the Programming Editor software and ran a simple
test program. This month, we’ll look at how to write programs
that respond to input signals.
In this article you will learn:
how to write a program that responds to digital inputs;
• the difference between a digital and
analog input; and
• how to write a program that responds to analog inputs.
•
Inputs and outputs
The PICAXE-08M microcontroller
has five pins available for use in your
circuits (see Fig.1). Of these, pins 1, 2
and 4 can be used as outputs, digital
inputs or analog inputs. On the experimenter board, pins 1 and 2 are used as
outputs to drive the yellow and green
LEDs, whereas pin 4 is used as an
analog input for the light dependant
resistor (LDR).
* About the author: Clive Seager is the
Technical Director of Revolution Education
Ltd, the developers of the PICAXE system.
76 Silicon Chip
Pin 0 can only be used as an output.
In addition to driving the red LED,
it is used for communications when
downloading a program from your
computer into PICAXE memory. It is
useful to remember that this output
toggles rapidly (as is evident by the
flickering of the red LED) during program downloads.
Lastly, pin 3 can only be used as
a digital input. On the experimenter
board, this input is connected to the
pushbutton switch (SW1).
Important: in the PICAXE system,
the physical pins of the microcontroller are often referred to as “legs”.
On the other hand, port inputs and outputs are called “pins”. For example,
on the PICAXE-08M, pin 2 is input2
(or output2 or ADC2) and appears on
leg 5 (see Fig.1).
Getting started
Even those of us who don’t drive a
Fig.1: the pinouts for the PICAXE-08M microcontroller, as used in
the Schools Experimenter board described in Pt.1 last month.
siliconchip.com.au
Fig.2: a pushbutton switch
generates a
digital signal
with the aid of
a 10kW “pulldown” resistor.
Fig.3: when
connected in a
simple potential
divider circuit,
an LDR generates
an analog signal
proportional to
light intensity.
motor vehicle will be familiar with the
red - green - orange - red sequence of
traffic lights. The BASIC program to
simulate a traffic light sequence on the
PICAXE experimenter board is shown
in Listing 1.
Of course, we’ve used the yellow
LED in place of orange and we acknowledge that where you live, the sequence
might be slightly different, so jump
right in and change it to suit!
Note the use of the symbol command at the start of the program.
Symbol can be used to make a program
easier to understand, as you do not
have to remember which LED is connected to which output.
As you would expect for a traffic
light simulator, the program runs continuously in a loop, starting as soon
siliconchip.com.au
Fig.4: As light intensity decreases, the resistance of the LDR increases,
so a greater portion of the supply voltage appears across its terminals.
Conversely, the voltage at the PICAXE’s analog input decreases, as a
smaller portion is dropped across the 10kW resistor.
as the battery is connected. But what
if you only want the outputs to come
on when a switch is pushed? A realworld example of this can be seen in a
washing machine, where it’s necessary
to push the “Start” button to initiate
a wash cycle.
Digital inputs
A miniature pushbutton switch is
included on the experimenter board
and it’s connected to input3 of the
micro. Fig.2 shows the components
used in the switch circuit. As you can
see, it’s very simple; just the switch
and a 10kW resistor connected in series between the 4.5V and 0V power
supply rails.
The 10kW resistor performs an
important function. Without it, the
PICAXE input would not be connected to any electrical signal when
the switch is open, causing it to “float”
to an indeterminate logic state. How-
ever, with the 10kW resistor in place,
the input has two definite states: 0V
when the switch is not pushed and
4.5V when the switch is pushed. In
digital electronics, these two states are
referred to as a “low” (logic 0) and a
“high” (logic 1), respectively.
A BASIC program that demonstrates
how to respond to the switch input is
shown in Listing 2. In this program,
the green LED will come on every time
the switch is pushed (closed).
Task – write a program to make the LED
come on when the switch is open (rather
than closed).
Responding to multiple inputs
Making the program react to two (or
more) switches is also quite straightforward. By way of example, Listing
3 adds a second (hypothetical) switch
on input4. As shown, the LED will
be illuminated when either of the
switches is closed.
June 2005 77
Program Listings
Listing 1
symbol red = 0
symbol yellow = 1
symbol green = 2
main:
high red
pause 500
low red
high green
pause 500
low green
high yellow
pause 500
low yellow
goto main
Listing 2
loop:
if input3 = 1 then main
goto loop
main:
high 2
pause 500
low 2
goto loop
Listing 3
loop:
if input3 = 1 or input4 = 1 then main
goto loop
main:
high 2
pause 500
low 2
goto loop
Listing 4
loop:
if input3 = 1 and input4 = 1 then main
goto loop
Listing 4 shows how the program
is easily modified to react only when
both switches are closed at the same
time.
TASK – write a program to make the LED
come on when two switches (on input3 and
input4) are pressed together or when a switch
on input1 is pressed by itself.
Waiting until a switch
is released
Sometimes it is necessary to wait
78 Silicon Chip
the switch has been pushed and then
released.
Adding switch debouncing
main:
high 2
pause 500
low 2
goto loop
Listing 5
loop:
if input3 = 1 then loop1
goto loop
loop1:
pause 10
if input3 = 1 then loop1
main:
high 2
pause 500
low 2
goto loop
When most mechanical switches
close, two sprung metal contacts move
closer together and then eventually
touch. Unfortunately, these contacts
do not move precisely and quite often
“bounce” against each other a couple
of times before coming to a stop. This
means that the electrical connection
opens and closes rapidly a number
of times whenever the switch is activated.
A PICAXE microcontroller processes much faster than a mechanical
switch can operate and so will detect
the switch “bouncing” as legitimate
on/off switch action. By adding a
10ms delay into the loop (the pause
10 command in Listing 5), we provide
the switch contacts with time to settle
before the program reads the switch
input and makes the on/off decision.
Analog inputs
Listing 6
main:
readadc 4,b1
debug b1
pause 100
goto main
Listing 7
symbol action = 80
loop:
readadc 4,b1
if b1 < action then main
goto loop
main:
high 2
pause 500
low 2
goto loop
until a switch is pushed and then released before continuing the program.
In this case, the program in Listing 5
can be used.
As in the previous examples, the
program waits in a loop until the
switch is pushed. However, it then
jumps to “loop1” where it waits until
the switch is released again before
continuing.
This means that the “main” section
of the code is processed only after
As we’ve seen, a pushbutton switch
is essentially a digital device, as it has
only two states (on or off). However,
some sensors, such as light and temperature sensors, generate a continuously varying signal. These varying
signals are called analog signals.
Input4 on the Schools Experimenter
board is connected to an LDR and 10kW
resistor (see Fig.3). These two components are connected in series between
the +4.5V and 0V power supply rails,
forming a “potential divider”. This
term refers to the fact that each of the
components has a fraction of the 4.5V
supply across it, in effect dividing the
supply voltage.
As more light falls on the LDR, its
resistance decreases, meaning that a
smaller percentage of the 4.5V supply will appear across it. Therefore, it
follows that the voltage reading at the
PICAXE input will vary according to
how much light falls on the LDR. The
general idea is explored in Fig.4, where
three arbitrary light levels produce
different resistance values and correspondingly different voltage levels.
The PICAXE chip can measure this
varying voltage using the readadc
command. Readadc is shorthand for
“read-analog-to-digital-converter”.
This command instructs the PICAXE
to read the analog voltage value and
then save that value as a number in
memory. As the PICAXE works with
siliconchip.com.au
byte values, the result will always be
a whole number between 0 and 255.
In the simplest possible terms, if you
connect 4.5V to the input, you will
get the number 255 in your program.
Connect 0V to the input and you will
get the number 0. Connect any voltage
between these two values and you
will get a number between 0 and 255,
which in our case can then be used as
the “light level” reading.
Task – what values would be returned by
the readadc command with input levels of
2V, 3V and 4V?
The program in Listing 6 reads the
analog level on input4 and stores the
value in variable byte 1 (b1). The debug
command then transmits this value
via the serial cable to your computer
screen every 100ms.
Run this program and then vary the
light levels reaching the LDR using
your hand. You should see the value
of variable b1 change as the light falling on the sensor changes. Make a
note of the “bright” light level value
(sensor exposed) and the “dark” value
(sensor obscured). Use these values
to decide on an “action threshold”,
which should be about halfway between these two values.
Silicon Chip
Binders
REAL
VALUE
AT
$12.95
PLUS P
&
P
This is the basic Schools Experimenter
board described in Pt.1 last month.
The program in Listing 7 uses an
action threshold value of 80, which
you can change to suit your experimental value. When the light level is
less than the action level, the green
LED will light.
Task – write a program to make the LED
come on when the light level is below your
action value and the pushbutton switch is
pressed.
What’s coming
That’s all for this month. Next
month, we’ll look at a more sophisticated sensor for temperature measurement and have some fun with tunes
SC
using the piezo sounder.
H Each binder holds up to 12
issues H S ILICON C HIP logo
printed on spine & cover H Heavy
board covers with mottled dark green
vinyl covering
Price: $A12.95 plus $A5 p&p each
(available only in Australia). Buy five
and get them postage free.
Just fill in the handy order form in
this issue; or fax (02) 9979 6503;
or ring (02) 9979 5644 & quote
your credit card number.
From the publishers of SILICON
CHIP
PERFORMANCE
ELECTRONICS
FOR CARS
NOT A REPRINT: More than 160 pages of new and
exciting projects never published before – all designed
to get top performance from your car.
FASCINATING ARTICLES: 7 chapters explaining your
car – engine management, car electronics systems, etc
ADVANCED PROJECTS: You’ll build controllers for turbo
boost, nitrous, fuel injection and much more!
We explain the why as well as the how to!
Available direct from the Publisher ($22.50 inc postage):
Silicon Chip Publications, PO Box 139, Collaroy NSW 2097. Ph (02) 9939 3295; Fax (02) 9939 2648;
email silchip<at>siliconchip.com.au or via our website: www.siliconchip.com.au
siliconchip.com.au
June 2005 79
|