Flashlight Control Project

Flashlight is a project, that along with 2 CDS photocells, 2 resistors and some jumpers, will make your robot move -- faster in bright light, slower in dim light. It includes both the Graphical program and the robot executable.

Flashlight Control Project

Here is the finished project, assembled and working on a CEENBoT.

Analog to Digital Conversion

The CEENBoT can measure voltages on the 2 analog-to-digital inputs, and turn those measurements into a number ranging from 0 – 1023. This feature lets you create some very interesting programs that connect the CEENBoT to the world.

In this project, you will assemble a simple electronic circuit on the CEENBoT breadboard that produces a small voltage that is controlled by the amount of light shining on a photocell. Photocells are a type of resistor that conduct more (resistance decreases) as more light strikes its surface.


Parts List

  • 2 - CDS photocells
  • 2 - 1,000 ohm, 1/8 watt or larger resistors
  • at least 4 - jumper wires, insulated to prevent short circuits
  • 1 - 20 pin to 20 socket adapter
  • 1 - flashlight


About the Circuit

The circuit diagram shows the electronic representation of the photocell sensor parts. The connector diagram shows the different voltages and signals on the 20-pin expansion connector. The photograph above shows one way to plug in the electronic parts and jumpers to assemble the circuit.

The circuit consists of two identical resistor divider networks. A resister divider network allows a measurement that is somewhere between the voltage put in at the top of the circuit and ground at the bottom of the circuit. We are putting 5 volts DC into the top. The bottom is at 0 volts. The voltage at the point between the resistor and the photocell is affected by the amount of light striking the photocell. As the photocell receives more light, its resistance drops and the voltage measured between the resistor and the photocell gets closer to 5 volts. As the photocell receives less light, its resistance goes up and the voltage measured between the resistor and the photocell gets closer to ground (0 volts).

Flashlight Circuit

The circuit diagram for this project.

Flashlight Connector

The connector on the CEENBoT allows you attach circuits to the robot.

About Breadboards

Breadboard Connections

Breadboards are nifty tools for quickly making electronic circuits. They are made of plastic, with many holes in horizontal rows and vertical columns.

At the top and bottom, the holes are electrically connected horizontally (see the yellow line to show which holes are connected). Anything plugged into the same horizontal row is electrically connected. Often, these top and bottom rows are used to provide power to the circuit.

In the middle section of the breadboard are vertical columns of connected holes, 5 in each column. A gap separated the top and bottom sets of columns. The top and bottom sets of columns are NOT electrically connected. The yellow lines show how the columns are connected. Anything plugged into the same column is electrically connected to anything else plugged into that column.


Assembling the Circuit

  • Start by plugging the left photocell into a hole in the circuit board. Insert leads so they are in horizontal (left to right) holes in the breadboard. Put the photocell on the left half of the breadboard.
  • Do the same for the right photocell, putting it on the right side of the breadboard.
  • Connect a resistor from the breadboard hole below the left photocell, right-hand wire, down to the very bottom row of holes on the breadboard.
  • Repeat this for the right photocell.
  • Connect a jumper from the breadboard hole above the left photocell, left-hand wire, up to the very top row of holes on the breadboard.
  • Repeat for the right photocell
  • Connect a jumper from the breadboard hole above the left photocell, right-hand wire, to the #1 hole on the 20 pin connector (ADC1).
  • Connect a jumper from the breadboard hole above the right photocell, right-hand wire, to the #2 hole on the 20 pin connector (ADC2).
  • Connect a jumper from a hole in the very top row of holes on the breadboard to the #19 hole on the 20 pin connector (+5VDC).
  • Connect a jumper from a hole in the very bottom row of holes on the breadboard to the #20 hole on the 20 pin connector (Ground).

Recheck your connections to make sure that the jumpers, photocell and resistors are correctly connected.

Programming Step 1 - Test the Electronic Circuit

Now that the physical connections are done, you will write a very simple program that tests whether your circuit is working. The program will read the values received on the 2 analog-to-digital converter inputs, and then display them on the LCD display.

• Read the left sensor ADC and store it in “Left Sensor Value”.

• Read the right sensor ADC and store it in “Right Sensor Value”.

• Display the “Left Sensor Value” and the “Right Sensor Value” on the LCD display.

• Repeat

An example of one way to write program is here.

>> Check Light Sensor Circuit Sample Program (.zip)

If the LCD display does not show a number between 0 and 1023 which changes with the amount of light falling on the photocells, go back and correct the errors in your circuit or connections.

Programming Step 2 - Gathering Minimum and Maximum Values

Once you are seeing correct values for the photocell, the next step is to gather some information about the circuit and the measured voltages.

We don’t know yet how the photocell circuit will behave, and in order to write the final robot control program, we need to learn what is the range of values measured from low to high, light to dark.

Write a program that collects the value from each ADC input, save those values in variables for each input and for maximum value and minimum value. Then continue to read the ADC inputs and see if the new left and right values are higher or lower than the previous highs or lows.

• Preset the “Left Minimum”, “Left Maximum”, “Right Minimum” and “Right Maximum to 512. (This number is halfway between the lowest and highest possible values).

• Read the left sensor ADC and store it in “Left Sensor Value”.

• Read the right sensor ADC and store it in “Right Sensor Value”.

• Check to see if the “Left Sensor Value” is greater than the “Left Maximum” and if it is, replace the “Left Maximum” with “Left Sensor Value”.

• Check to see if the “Left Sensor Value” is less than the “Left Minimum” and if it is, replace the “Left Minimum” with “Left Sensor Value”.

• Check to see if the “Right Sensor Value” is greater than the “Right Maximum” and if it is, replace the “Right Maximum” with “Right Sensor Value”.

• Check to see if the “Right Sensor Value” is less than the “Right Minimum” and if it is, replace the “Right Minimum” with “Right Sensor Value”.

• Display the “Left Sensor Value” and the “Right Sensor Value” on the LCD display.

• Repeat

An example of one way to write program is here.

>> Capture Min and Max Sample Program (.zip)

Run the program and expose the sensors to different light levels by covering each photocell with your finger, and shining a bright light on it. When you have recorded what seem to be the highest and lowest values, copy these left and right maximum and minimum values.

Some example values are:

  • Left ADC Right ADC
  • Minimum 85 78
  • Maximum 975 967

Programming Step 3 - Determining How To Adjust The Values To Fit The Move Command

The CEENBoT motor speed is set by a value from 0 – 100. The value must not be negative, and going higher than 100 will not change the speed. Knowing this, we will need to transform the left and right values so that they start at no less than 0 and only go to 100.

For each left and right input, subtract the minimum you found in the last step from the current value and if the result is less than 0 (it should not be, but this is just for safety), make it 0. Now the values start at zero.

  • Left ADC Right ADC
  • Adj Minimum 0 0
  • Adj Maximum 890 891

The next step is calculating how to scale down the value that can be much higher than 100 into a value that is no more than 100.

Subtract the minimum value you recorded earlier from the maximum value to get the adjusted maximum. The adjusted maximum, if divided by a scale adjustment value, will result in a number between 0 and 100.

To figure out the adjustment number, we will use the formula

(adjusted maximum) / (scale adjustment) = 100

890 / (scale adjustment) = 100

Get both known amounts, 100 and (adjusted maximum) to the same side of the equation and the formula is now

(scale adjustment) = 890 / 100 = 8.9

CEENBoT Commander only works with whole numbers so round 8.9 up to 9.

Using the examples above, if the sensor reads 890, the calculation will divide it by 9

890 / 9 = 99

99 is close enough to 100 for this project.

Programming Step 4 - Putting The Conversion Into A Program

To get the robot to respond to light correctly, the conversion formula you figured out above must be turned into a program on the robot. For the conversion steps in your program,

• Read the left sensor ADC and store it in “Left Sensor Value”.

• Read the right sensor ADC and store it in “Right Sensor Value”.

• Subtract 85 (the left minimum) from the “Left Sensor Value” and store it back in “Left Sensor Value”

• Check “Left Sensor Value” to see if it is less than 0, and if it is, set it to 0

• Divide “Left Sensor Value” by 9 (the scale adjustment) and store it back in “Left Sensor Value”

• Subtract 78 (the right minimum) from the “Right Sensor Value” and store it back in “Right Sensor Value”.

• Check “Right Sensor Value” to see if it is less than 0, and if it is, set it to 0

• Divide “Right Sensor Value” by 9 (the scale adjustment) and store it back in “Right Sensor Value”.

• Display the “Left Sensor Value” and the “Right Sensor Value” on the LCD display.

• Repeat

An example of one way to write program is here.

>> Adjust Light Sensor To Motor Speed Sample Program (.zip)

Run the program and confirm that the values are between 0 and 100 for both the left and the right, and that the left and the right values change depending on the amount of light falling on the photocells.

Programming Step 5 - Make the Robot Move

If your program does this, it is ready to control the motors. After the Display step in your program,

• Add a Move command

• Make the Move command “freerunning”

• Make the left motor speed equal to the Left Sensor Value

• Make the right motor speed equal to the Right Sensor Value

An example of one way to write program is here.

>> Flashlight Control Sample Program (.zip)

Run the program in a darkened room and use a flashlight to control the robot. Notice that shining light on the right sensor makes the robot turn left, and shining the light on the left sensor makes the robot turn right.