resistor to five volts so the input to the micro is high when
the switch is not pressed and low when the switch is
pressed. If the parts were reversed where the switch was
connected to five volts and the resistor to ground, then it
would be a high-side switch.
The software will test the input pin GP4 to see if it
changes to low, indicating the switch has been pressed.
The GP4 pin will be configured as an input pin in the
software, so all we need from the hardware is a known
idle state which is determined by the pull-up resistor. The
connection table for the breadboard is as follows:
Micro - Pin 1 at C6
Yellow Jumper - a6 to +rail
Yellow Jumper - j6 to -rail
Green Jumper - j7 to j12
330 ohm - i12 to i18
Red LED - Anode j18, Cathode -rail
Yellow Jumper - j22 to -rail
Orange Jumper - f22 to e22
Orange Jumper - b22 to b19
White Jumper - b8 to b17
10k ohm - a17 to +rail
Switch - d17 to d19
The software starts with the same I/O setup, but now
adds a new line to make the GP4 pin an input for the
switch. This command acts on the internal TRISIO register
of the PIC12F683. That register determines whether the
I/O pin is an input or output. Each bit of that eight-bit
register represents a pin. A 1 in the GP4 slot makes it an
input and a 0 makes it an output. The INPUT GPIO 4
command line sets the GP4 pin to a 1 for input mode.
following the ELSE line will be executed. In this case, if the
GP4 pin is high — meaning the switch is not pressed —
then the red LED is lit. If instead the GP4 pin is low —
meaning the switch is pressed — then the LED is shut off.
If GPIO.4 = 1 then
HIGH GPIO.0 ‘Light Red LED
LOW GPIO.0 ‘Red LED off
A GOTO statement completes the loop and sends
control back to the main label, so the switch can be
GOTO main ‘Loop Back to Red LED
You could add the extra LEDs from the traffic light
project and change to red, yellow, or green with the push
of a button. You could also create a speech timer where
the green indicates time is okay, yellow means time is
running out, and red means time is up. The switch starts
the process. You could also just reverse the logic and have
the LED light when the switch is pressed. This is a very
simple change that I’ll let you figure out.
The thing to remember is that the switch represents
several different options. A Sharp GP2D15 obstacle-detection sensor can easily replace the switch for robotic
applications. A magnetic reed switch could replace the
switch to create a simple alarm system. Any device that
has a simple open-collector output and a digital on/off
output state can replace the switch.
ANSEL = 0 ‘ Set I/O to digital
CMCON0 = 7 ‘ Comparator off
The main label starts us off again, followed by an
IF-THEN-ELSE command from the PICBASIC PRO compiler.
This command will test the equation after the IF, to
determine whether it is true or false. If the equation is
true, then the command
following the IF command line
will be executed. If it is not
true, then the command
■ FIGURE 4. Final Switch
■ FIGURE 5.
The PIC12F683 has a built-in analog-to-digital converter
(ADC), so I’ll use that to read a cadmium sulfide (CdS) cell.
A CdS cell or photoresistor is a resistor whose resistance
decreases with increased light exposure. It can also be referred
to as a light-dependent resistor or photoconductor. You can get
a pack of them from RadioShack under part number 276-1657.
The PICBASIC PRO compiler has an ADCIN command
to make this another easy task to complete. I used an eight-bit resolution result that worked perfectly. As the light
changes, I have the ADC value
tested. If the result is a high
value (high resistance), then it is
dark out and the LED lights up.
Figure 6 shows the final setup.
The schematic is very
similar to the switch schematic,
except the switch is replaced by
the CdS cell. The pull-up resistor
is also lower, but it could easily