Sensing Light

Resistive Light Sensor

By The CHIPAXE Team

Sensing light can be useful and easy to accomplish with a resistive light sensor. In this project we'll use an Analog to Digital Converter (ADC) to read a CDS cell or resistive light sensor which changes resistance as the light around it changes. The ADC will create a digital value based on the resistance of the CDS sensor. As the light changes the ADC value will also change. The software will test the ADC value and if it's a high value (high resistance) then the sensor is indicating it's dark out and a single LED light will be driven to glow brightly. Figure 1 shows the final setup.

Parts list:

Part Description Manufacturer Part Number
Photocell Resistor CDS001-8001
PIC12F683 PIC12F683-VP
Red LED LH3330
330 Resistor CF1/4W331JRC
1k Resistor CF1/4W102JRC

Hardware

The schematic is very simple to build. The CDS cell is connected to a pull-up resistor to form a resistor divider or voltage divider to convert the changing CDS resistance into a changing voltage. The pull-up resistor could easily be replaced by a potentiometer to give you a sensitivity adjustment. The hardware only drives one LED on or off based on the outside light level similar to a night light.

Light Sensor ProjectFigure 1: Final Light Sensor Project

Connection Table

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

Light Sensor SchematicFigure 2: Light Sensor Project Schematic


Software

The start of the software is the same as the potentiometer setup since we will be using the GP4 pin as an analog input. Using the binary bit designation in PICBASIC PRO 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 ' AN3/GP4 Analog, GP2-GP0 Digital

The comparator is shut down once again.

CMCON0 = 7 ' Comparator off

The state of the GP4 has to be set to input mode using the TRISIO register, which can also set the rest of the I/O pins are set to outputs.

TRISIO = %00011000 ' GP4 input, GP2 thru GP0 outputs

The ADCIN command requires some setup parameters to be established such as the ADC resolution, ADC clock source and sampling time. These are easily done with DEFINE statements. The ADC is setup to run in 8-bit mode.

' Define ADCIN parameters
Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS


A variable is established to store the ADCIN result.

adval var byte 'Create adval variable to store result


The main label establishes the main loop followed by the ADCIN command line where the CDS is read.

main: 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, then it's 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
ELSE
LOW GPIO.0
ENDIF


Another GOTO statement completes the main loop.

goto main 'Loop Back to test potentiometer



Software Listing
'********************************************************************
'*   Name:CDS.BAS
'*   Date:8/20/2009
'*   Version:1.0
'*   Notes:
'* :CHIPAXE-8 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
'********************************************************************


ANSEL = %00001000 ' AN3/GP4 Analog, GP2-GP0 Digital
CMCON0 = 7 ' Comparator off
TRISIO = %00011000 ' GP4 input, GP2 thru GP0 outputs

' Define ADCIN parameters
Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS

adval var byte 'Create adval variable to store result

main:
ADCIN 3, adval ' Read channel AN3 to adval

If adval > 150 then 'Light LED if in the dark
High GPIO.0 'Light all LEDs
ELSE
LOW GPIO.0
ENDIF

goto main 'Loop Back to test light sensor


Next Steps

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 8-bit result. Just like the switch project other sensors can replace the light sensor. 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 read by the analog pin directly for more accurate robotic obstacle detection.

If you have any questions about this project please email us at [email protected].

You can get the CHIPAXE module from www.chipaxe.com.

If you are new to programming, then check out the books of Chuck Hellebuyck. The CHIPAXE Team has contracted Chuck to write books based on the CHIPAXE modules and he has many more that we recommend. His website is elproducts.com.