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).
Fig.1: the pinouts for the PICAXE-08M microcontroller, as used in the Schools Experimenter board described in Pt.1 last month.
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 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.
Fig.2: a push-button switch generates a digital signal with the aid of a 10kΩ "pull-down" resistor.
Fig.3: when connected in a simple potential divider circuit, an LDR generates an analog signal proportional to light intensity.
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 as the battery is connected. But
what if you only want the outputs to come on when a switch is pushed? A
real-world 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 10kΩ resistor connected in series between the 4.5V and 0V power
supply rails.
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 10kΩ resistor.
The 10kΩ 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. However, with
the 10kΩ 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.
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 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 the switch has been pushed and then released.
Adding switch debouncing
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
This is the basic Schools Experimenter board described in Pt.1 last month.
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 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.
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 using the piezo
sounder.
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
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
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
|