be replaced by a potentiometer so you have some
sensitivity adjustment. The connection table is shown
below, along with the schematic.
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
Read LED - Anode j18, Cathode -rail
Yellow Jumper - j22 to -rail
Orange Jumper - f22 to e22
Yellow Jumper - b22 to b18
White Jumper - b8 to b17
1k ohm - a17 to +rail
CdS Cell - d17 to d18
The start of the software requires a different setup since
we will be using the GP4 pin as an analog input. Using the
binary designation in the PICBASIC PRO compiler allows me
to easily set the AN4 bit of the ANSEL register making it analog,
while the rest of the I/O pins are zero or set to digital mode.
ANSEL = %00001000 ‘ Set I/O to digital except
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘ AN3/GP4 is Analog
Once again, the comparators are shut down.
CMCON0 = 7 ‘ Comparator off
The state of GP4 has to be set to input mode using
the TRISIO register which can also set the remaining I/O
pins to outputs. GP3 is always an input.
TRISIO = %00011000 ‘ GP4 input, GP2 thru GP0
The ADCIN command requires some setup
parameters to be established such as ADC resolution,
ADC clock source, and sampling time. These are easily
done with DEFINE statements.
‘ Define ADCIN parameters
Define ADC_BITS 8
Define ADC_SAMPLEUS 50
‘ Set number of bits
‘ in result
‘ Set clock source
‘ Set sampling time
‘ in uS
A variable is established to store the ADCIN result.
‘ Create adval variable
‘ to store result
The main label establishes the main loop, followed by
the ADCIN command line where the CdS cell is read.
ADCIN 3, adval
‘ Read channel AN3 to adval
After the value of the CdS cell is stored in the adval
variable, it is compared to the value 150. If it is less than
150, then the LED is off. If the value is greater than 150,
GETTING STARTED WITH PICs
then it is dark and the LED is lit using the HIGH command.
If adval > 150 then ‘Light LED if in the dark
High GPIO.0 ‘Light all LEDs
Another GOTO statement completes the main loop.
goto main ‘Loop Back to test
Changing the threshold value from 150 to something
higher or lower will determine how dark it has to be to light
the LED. The limit is 0 to 255 since we used an eight-bit
result. As with the switch project, other sensors can replace
the light sensor. For example, a thermistor could replace the
light sensor to measure temperature. A potentiometer could
be used to create a manual interface. If you remove the pull-up resistor, then a Sharp GP2D12 object-detection sensor
that produces a variable output voltage can be directly read
by the analog pin for more accurate robotic obstacle detection.
I would like to create more projects using this great
eight-pin setup, but I’m running out of time and space, as
I wanted to cover some news that has happened since my
last article. Give this little eight-pin part a try in your own
setup and see what you can come up with.
PICBASIC PRO COMPILER
In a previous article, I mentioned that you needed to
use Microchip’s MPLAB® IDE version 8. 15 or earlier with
microEngineering Labs PICBASIC PRO compiler. This is no
longer the case because microEngineering Labs recently
released a new version of the compiler that works with the
latest version of the MPLAB IDE. The setup instructions are
slightly different from the previous method of getting the
PICBASIC PRO compiler to work within the MPLAB IDE.
Full details are available at http://melabs.com/
Along with this update comes expanded chip support in
the sample version of the
PICBASIC PRO compiler.
What is really great is that
microEngineering Labs added
the PIC16F88X parts to the
list. This allows you to use
eight- to 40-pin parts without
the need for external MCLR
pull-up or crystals/oscillators
in order to test the compiler.
After you use this compiler
for a while, you’ll find the
full-priced version of it
November 2009 59
■ FIGURE 6. Final Light