A0 of your Arduino. Compile and upload the sketch, then
open the serial monitor window. You’ll see a series of
numbers; they correspond to the output voltage of the
sensor converted to a 10-bit (0 to 1023) numeric value. You
should get a low number when all light to the photocell is
blocked, and a higher number under full illumination.
FIGURE 8. Breadboard view of connecting the photocells.
The voltage decreases in darkness and increases as more
light strikes the photocell.
You will need to experiment with the series resistor to
determine its best value, based on the specific photocells
you use. You might want to try a 50 kΩ or 100 kΩ
potentiometer in place of a fixed resistor, allowing you to
fine-tune the series resistance as needed.
Listing 3 shows simplecds.pde, a basic sketch that
tests the operation of the photocell. Wire the photocell as
shown in Figure 6 and connect the output to analog pin
Steering Your Robot
With a Flashlight
By using two photocells mounted on each side of your
ArdBot, you can literally steer it by flashlight. Under just
room light, the robot is set to stop, waiting for your
command. Aim the flashlight so that light falls more or less
equally on both photocells, and the robot will move
forward. When the light levels aren’t equal, the robot will
turn toward the photocell that has more light falling on it.
Refer to Figure 7 for a schematic of the two-photocell
setup. Figure 8 shows the same circuit but in breadboard
view. For my prototype, I made small mounts for the
photocells using scrap PVC plastic, then attached the
mounts to the top deck of the ArdBot with metal brackets.
The photocells I used measured 0.29” x 0.25” (elliptical
shape). I drilled holes just slightly smaller, then used a rattail file to enlarge the holes so that the cell just fit inside.
On my prototype, the cells are held in just by friction, but
on yours you can use hot-melt glue or other adhesive that
when set leaves no moisture for a possible short circuit.
(Bear in mind light can strike the photosensitive surface
of the cell from the rear. You may want to add a layer or
two of black tape to prevent light spoilage.)
Figure 9 shows my ArdBot with the two photocell
“eyes” attached to the front. I’ve bent the brackets back a
bit so that the cells point slightly upward.
Refer to Listing 4 for lightsteer.pde. It uses the current
values of the photocells to make quick
steering adjustments to the left or to the
right. In the declarations area, the code:
const int ambient = 600;
const int threshold = 800;
sets two comparison values used elsewhere
in the sketch. You will need to experiment
with these values depending on the room
environment and photocell characteristics!
These values worked for me; you can start
with them, but expect to try other values as
you fine-tune the performance of the
The ambient value sets the upper level
of just the ambient (natural) light in the
FIGURE 9. Mount the photocells on the top of
the ArdBot — toward the left and right sides —
to make eyes for following the flashlight beam.
74 SERVO 02.2011