5 An Analog Light Sensor

Sometimes it's handy to be able to detect different intensities of light. Perhaps you'd like to have a simple switch to turn on your outside lights at dusk and then turn them off again at dawn. If you need to detect different degrees of lighting, you need an analog light sensor.

     The analog light sensor built in this chapter is a cadmium sulfide photocell. It's a variable resistor whose resistance changes proportionally to the amount of light which strikes its light-sensitive surface. This, in turn, provides the computer with information regarding just how much light is present. The resistance increases as the amount of light striking it decreases. Exposed to bright light, the cadmium sulfide photocell has a resistance of a few hundred ohms between its terminals. In darkness, this resistance increases to approximately 0.5 megohms-about the same value range as the potentiometer on the Commodore paddles. In fact, it's possible to connect the cadmium photocell in much the same manner as a paddle.

Light Sensor-Commodore Computers
To build an analog light sensor for a Commodore 64, Commodore 128, or VIC-20, you'll need the following parts:

parts
Quantity
Part
Part
Number
1
Cadmium sulfide photocell
276-116
1
9-pin D-subminiature female
276-1538
1
0.1 microfarad capacitor
272-1069
1
1K ohm resistor
271-8023
1
Solderless breadboard
276-175
You'll also need some stranded copper wire, as well as some solid copper wire (Radio Shack part number 278-1306).

To complete the project, follow these steps:
step-by-step
1. Cut two pieces of the stranded copper wire about 24 inches long and strip about 1/4 inch of insulation from each end. Cut several small pieces of solid wire about 2 inches long to use as jumpers. (You'll need them in this circuit and most others in this book.)
2. Solder the end of one wire to pin 7, and the other wire to pin 9 of the 9-pin plug.
3. Connect the wire attached to pin 7, the +5-volt lead, to point X1 on the solderless breadboard. Place the other wire (that attached to pin 9) to point Y1 on the board.
4. Install jumper wires between points Y2 and J2, points X2 and A2, and points J13 and Y13.
5. Place the 0.1 microfarad capacitor between points F2 and E2.
6. Bend the leads of the 1K ohm resistor, and install it between points B2 and B13.
7. Install the cadmium sulfide photocell between points E13 and F13.

Figure 5-1. Schematic Drawing

Light Sensor-Atari
Putting together an analog light sensor for the Atari computer series involves some different components and different directions.
     You'll need:

parts
Quantity
Part
Part
 Number
1
Cadmium sulfide photocell
276-116
1
9-pin D-subminiature female
276-1538
1
1M potentiometer
271-211
1
Solderless breadboard
276-175
You'll also need some stranded copper wire as well as some solid copper wire.

Figure 5-2. Breadboard Layout

step-by-step
1. Cut two pieces of the stranded copper wire about 24 inches long and strip about 1/4 inch of insulation from each end. Cut several small pieces of solid wire about 2 inches long to use as jumpers.
2. Solder the end of one wire to pin 7, and the other wire to pin 9 of the 9-pin plug.
3. Connect the wire attached to pin 7, the +5-volt lead, to point X1 on the solderless breadboard. Place the other wire (that attached to pin 9) to point Y2 on the board.
4. Install jumper wires between points Y6 and J6, and points X2 and A2.
5. Place the 1M potentiometer between points E6 and F6.
6. Install the cadmium sulfide photocell between points B2 and B6.

test

To see how your analog light sensor works, use Program 4-1 from the previous chapter. If you're using a Commodore 64, or a Commodore 128 in 64 mode, you can use Program 5-1 below instead. If you have an Atari computer, enter and run the two-line program you see below. For a Commodore 64 or 128, the light sensor should be plugged into port 2. Plug the Atari light sensor into port 1.

Program 5-1-Commodore 64/128
AD 10 I=828
PE 20 READ A:IF A=256 THEN50
RG 30 POKE I,A:I=I+1:GOT020
FX 40 REM ANALOG LIGHT METER
GH 50 SYS 828:A=PEEK(252)
DH 60 IFB=ATHEN50
GX 70 B=A:PRINT"{CLR}LIGHT LEVEL IS NOW";A
DG 80 GOT050
KA 90 DATA 76,63,3,162,1,120,173
FR 100 DATA 2,220,133,167,169,192,141
BJ 110 DATA 2,220,169,128,141,0,220
QG 120 DATA 160,128,234,136,16,252,173
QE 130 DATA 25,212,149,251,173,26,212
CS 140 DATA 149,253,169,64,202,16,232
FJ 150 DATA 165,167,141,2,220,88,96,256

 
Program 5-1-Atari
10 PRINT PADDLE(0)
20 GOTO 10

Download (Saved BASIC)
Download / View (Listed BASIC)

     When the you run the program, numbers in the range 0-255 (1-228 for the Atari) will scroll on your screen. These numbers are related to the amount of light which is falling on the photocell. To see this, try reducing the light reaching the photocell by covering it with your hand. As you do, the numbers in the column will increase. Conversely, increasing the amount of light which hits the photocell, perhaps by shining a flashlight on it or by holding it closer to a lamp, causes the numbers on the screen to decrease.

attention
Note: On the Commodore light sensor, you may need to adjust the value of R1, the 1K ohm resistor, to cause a swing through the entire range of values. In other words, you may need to use a different resistor, one rated higher or lower than 1K. Just experiment until you find the proper value for your application. That's part of the fun in breadboarding-making changes is easy.
     The computer treats the light sensor as though it were a game paddle. The varying resistance of the sensor provides the computer with a signal between 0 volts and +5 volts at its port. The voltage is converted to a digit between 0 and 255 by the analog-to-digital converter in your computer. This value is stored in a memory location, where it can be read by a program. It shouldn't be hard, once you have the sensor built, to take this information and develop a light meter program which will determine the degree of light present at the sensor.

test
Two analog light sensors can be connected to your computer through the single 9-pin plug (just like two game paddles). The second photocell is attached between pins 7 and 5 of the 9-pin plug. To read the value of this second sensor, simply PEEK memory location 54298 ($D41A) for the Commodore 64/128, or PEEK location 36873 ($9009) for the VIC. Don't forget to set the data direction registers for output. The BASIC command PADDLE(1) can be used to read this value for Atari computers.


Return to Table of Contents | Previous Chapter | Next Chapter