FIGURE 5. A spring-loaded leaf
switch makes a good object
contact bumper. When the switch
closes, the Arduino can register it
as a collision with something.
Ground clearance of the
Beginner Bot is limited, so the
best surfaces for testing include
tile, wood, or a kitchen table.
Carpet is acceptable as long as it
has a very low nap. If the bot
appears to struggle as it’s
moving along, relocate it to ride
over a smoother surface.
Both Parts 2 and 3 of this
series showed how to control
the Beginner Bot using a
flashlight by shining the light
into the photocell eyes. Listing 2
provides the same method, this
time with an Arduino. The
sketch tells the Arduino to read
the value from both photocells.
A series of If conditional logic
tests determine if there’s enough
light to follow, and if so, in what direction the robot should
The program first sets a threshold value to determine
the boundary between dark and light, as well as
determining the ambient (natural) level of light in the room.
I’ve set the light/dark threshold to 800 (out of a range of 0-
1,023) as a starting point. Try higher or lower values to see
what works best with your particular CdS cells. I’ve set the
ambient level at 600.
FIGURE 6. Mount the switch on one side of the front of the
Beginner Bot. By attaching a stiff wire to the leaf (solder or glue),
you can extend its reach across the entire front of the robot.
with the light shining into it.
If your robot moves when there’s no light falling on the
CdS cells, try changing the ambient and threshold values.
Conversely, if the light from the flashlight seems to make
• When both cells receive light over the ambient level, the
robot drives forward.
Upload the program in Listing 2. When transfer is
complete, remove the programming cable. Move to a
darkened room, apply power to the robot, and place it on
the ground. Aim a bright flashlight away from the Beginner
Bot. The robot should not move. Next, shine the flashlight
evenly into both photocells. The robot should move toward
you. Get close to the robot and aim the flashlight into just
one photocell. The robot should turn toward the photocell
FIGURE 7. Breadboard view of attaching the wires from the
switch to the Arduino. Connect one lead from the switch to
ground; the other to digital pin D2.
SERVO 11.2011 61