The light pen you built in Chapter 6 is a type of light sensor-it detects
light from the computer's screen and sends out an appropriate signal. But
what if you want to be able to tell whether it's dark outside, or whether
somebody opened your closet door (letting light inside)? Or perhaps you
want to build an electronic timer that starts and stops timing when a beam
of light is broken? To detect different levels of light, you need a slightly
more complex sensor.
This chapter will show you how to build and use a
variable digital light sensor. This sensor provides the computer with either
a high (1) or low (0) digital signal. When the light sensor is adjusted
for a certain level of light, the computer receives a logic low (0) signal
as long as this level of light, or more, is maintained. However, if the
level of light falls below this setting, the computer gets a logic high
(1) signal from the sensor. This type of sensor is useful for applications
such as light beam timers and counters, two applications which will be demonstrated
here as well.
To build the digital light sensor, you need
these parts:

Quantity

Part

Part
Number

1

3900 op amp IC

276-1713

1

500K potentiometer (Commodore)

271-210

1

1M potentiometer (Atari)

271-211

1

100K ohm resisotr

271-8045

Quantity

Part

Part
Number

1

10K ohm resistor

217-8034

1

1K ohm resistor

271-8023

1

TIL 414 infrared phototransistor

276-130

1

9-pin D-subminiature female

276-1538

1

Solderless breadboard

276-175

You'll also need some solid copper
wire for jumper connections and some stranded copper wire for connections
to the phototransistor.

These steps will take you through the process of building your digital
light sensor:

1. Cut three pieces of solid copper wire and
remove about 1/4 inch of insulation from each end.
2. Connect one wire each to pins 1, 7, and 8 of the 9-pin plug.
3. Connect the wire from pin 8 to location X1 of the solderless breadboard.
4. Connect the wire from pin 7 to location Y1.
5. Connect the wire from pin 1 to location F5.
6. Plug the 3900 op amp IC into the solderless breadboard so that pin
1 goes to point E15, and pin 8 goes to point F9.
7. Cut two pieces of solid copper wire about 2 inches long and remove
about 1/4 inch of insulation from each end. Solder one wire to an out-side
lead of the potentiometer. Solder the other wire to the middle lead of the
potentiometer.
8. The wire from the outside lead connects to point X22 of the solderless
breadboard. The wire from the potentiometer's middle lead connects to point
A22.
9. Cut two pieces of stranded copper wire about 12 inches long and remove
about 1/4 inch of insulation from each end. Solder one wire to the emitter
of the phototransistor and the other wire to the collector.
10. The wire from the collector plugs into point Y22 of the solderless
breadboard, while the wire from the emitter inserts into point J22.
11. Insert the resistors as follows on the solderless breadboard:

Resistor

From

To

1K ohm

B4

J4

10K ohm

B22

B13

100K ohm

D13

D12

12. Connect five solid copper wire jumpers on the solderless breadboard
as follows:

Jumper

From

To

1

E22

F22

2

Y15

J15

3

X14

A14

4

C12

C4

5

X9

A9

Inside the Sensor
The phototransistor in this circuit acts as a switch. It's in series
with the 500K potentiometer. When light strikes the light-sensitive surface,
current will flow through the emitter-collector junction. The sensitivity
is set by adjusting the 500K ohm potentiometer in series with the phototransistor.
The operational amplifier's (op amp) inverting input is connected between
the potentiometer and the phototransistor. The voltage at the op amp's input
is determined by the resistances of the potentiometer and phototransistor.
The op amp is set up using the 10K ohm and 100K
ohm resistors so that when the input voltage is a certain level, its output
goes to the logic low level. The output of the op amp is connected to pin
1 of the control port through a 1K ohm resistor.

The phototransistor must be set for the desired
level of light. This is done by aiming the light-sensitive part of the phototransistor
at a light source, then adjusting the potentiometer for triggering at the
desired light level. Program 7-1 provides the necessary lines for testing
and adjusting this circuit.
Adjust the potentiometer so that the output
to the computer turns logic low. Once this is done, if the phototransistor
receives less light, the input voltage of the op amp no longer will be the
correct voltage to force its output low. As a result, pin 1 of the 9-pin
plug returns to its normal logic high level.

Putting It
to Use

To use the light sensor, first turn off your computer.
Insert the 9-pin plug into port 2 of the Commodore 64 or 128 (there's only
one port on the VIC), or into port 1 of any Atari computer. Turn your computer
back on, type in, and run the version of Program 7-1 for your machine. For
this demonstration, the sensor is set to the lighting in the room, so make
sure that the light-sensitive portion of the phototransistor is uncovered
and facing up.

When you run the program, a column of messages
should appear on the screen. Either the message LIGHT IS ON or LIGHT IS OFF
will be displayed.
If the message is LIGHT IS ON, it means the
sensor is detecting light at the level of or greater than the potentiometer
setting. When the sensor detects the light level, it forces pin 1 of the
control port low, just as connnecting it to pin 8 (ground) does. If the sensor
does not detect the light level it's set for, pin 1 of the control port
remains at logic high.

Adjust the setting of the potentiometer by turning
its shaft. Doing this controls the sensitivity of the light sensor. You should
find a point as you turn the potentiometer shaft where the messages in the
column change from LIGHT IS OFF to LIGHT IS ON. In order to set the light
sensor to detect the light in the room, leave the potentiometer set at the
point where the messages just change from LIGHT IS OFF to LIGHT IS ON.
Just as with a joystick, programs can check
the control port for the light sensor's signal. All that's necessary is to
PEEK the appropriate register, or use the STICK command in the case of the
Atari, then check the bit (corresponding to pin 1, in this case) for a 1
or 0.

A Digital
Light Beam Timer
This circuit, quite similar to the light sensor, can be used as a timer
circuit for slot cars, or for any application involving measuring time over
a distance. This project is essentially the same as the digital light sensor
circuit you just completed. The following parts are required:

Quantity

Part

Part
Number

1

3900 op amp IC

276-1713

1

500K potentiometer (Commodore)

271-210

1

1M potentiometer (Atari)

271-211

1

100K ohm resistor

271-8045

1

10K ohm resistor

217-8034

1

1K ohm resistor

271-8023

1

Infrared emitting diode

276-142

1

Infrared detecting diode

276-142

1

9-pin D-subminiature female

276-1538

1

Solderless breadboard

276-175

You'll also need some solid copper
wire and some stranded copper wire.

Construct the circuit as you did for the digital light
sensor, but position the infrared emitting diode and the infrared detecting
diode so that the emitting surface and detecting surface face each other.
Tests indicate that the maximum usable distance between these diode pairs
(the two parts come in one package, listed as Radio Shack part number 276-142)
is less than one foot.
Connect the infrared emitter's anode to the
y line on the solderless breadboard, and the cathode to the x line. Use
the appropriate version of Program 7-1 to adjust and focus the diodes so
that the circuit triggers when the beam of light from the emitter is broken,
not when ambient room light is interrupted. The light in the infrared is
invisible to the naked eye. You'll see no indication that the diode is turned
on. Be sure you've in-stalled it correctly in the circuit.

When you've tested the circuit and are satisfied
that it's functioning properly, type in and run Pro-gram 7-2. Adjust the
potentiometer as you did with the digital light sensor, then press the X
key to continue the program.
Start the timer by breaking the beam between
the infrared emitting and infrared detecting diodes. When the beam is broken
a second time, the elapsed time is displayed and the timer stops. Press
the X key to reset the timer and run the program again.
This circuit could be useful as a burglar alarm,
one that detects when an object or intruder breaks the beam of infrared
light.