In this project we build a robot that has two optical light sensors and turns its head in the direction of light. The head is the only moving part of the robot and it is controlled by a gearbox manufactured by Tamiya. The light sensors are formed by two CdS photo-resistors available from RadioShack. I used two smallest ones from the package of 5 photo-resistors available there. The cell diameter is about 5mm, the maximum dark resistance is about 14M and the minimum light resistance is about 0.5K. The daylight resistance in my room is about 50K.
Light sensor | Motor and gear |
---|---|
![]() |
![]() |
The photo-resistors are mounted on the robot head which in turn is attached to the gear axe. The first prototype is shown on the picture below. I used a paper strip separating the photo-resistors and its optimal length in my setting is 1in measured from the photo-resistors. The separator is needed to shadow one of the photo-resistors when the light source is moving. For simplicity, the head can move in a 2-dim horizontal plain only, thus making a difference with a real sunflower. The head is formed by a small breadboard shown in the bottom part of the image, which for now has just the photo-resistors and the paper separator mounted on it.
The light sensors (L and R on the schematic) are connected to the PIC which periodically measures their resistance and controls the motor accordingly. To measure the resistance of the photo cells I use a classic RC-chain and measure the time of charging a capacitor, which for a fixed C is proportional to R. The direction of the motor rotation is controlled by the classic H-bridge composed entirely from NPN Darlington transistors TIP120. These transistor structures contain the diodes protecting them from the high voltage caused by inductive load. The bases of the bridge transistors are connected to PIC. If the RB6 and RB7 outputs are both 0, the motor is not rotating. If one of them is 0 and the other one is 1, the motor is rotating in the corresponding direction. The situation when both outputs are 1 is prevented by the software, since in this case the 3V battery would be short cut.
Schematic | Layout | |
This is just the first prototype of the design and I use LCD for tuning
and debugging. The LCD displays the numbers coming out of the resistance
measurement. The larger numbers correspond to a darker resistance. The
built-in PIC program does not allow the numbers to exceed 255. The minimum
numbers corresponding to lighting the device with a desktop 60W lamp is about
30, so we have almost the full range of the light intensity measurements of
30 - 255. The motor starts to move if the absolute difference between the
numbers is larger than 15, which is defined experimentally. This constant
defines how much the light source can move before the robot starts following
it. The larger is the constant, the less is the accuracy of following the
light. The sensor resistance is measured approximately every 80msec, which
is also near optimal for the gear ratio 719:1 and the motor voltage in the
range 3 - 5V. Increasing the measurement time up to 250msec causes the head
moving back and force several times before it finally stabilizes.
The embedded program source for the first prototype is
photo1.asm
The LCD is actually not needed in a real device and can be excluded. This decreases the number of interface pins down to 6. Hence, a smaller PIC can be used as it is shown on the updated schematics. This PIC 12F675 has built-in 4MHz RC-oscillator which further simplifies the circuit. Also, smaller transistors can be used to drive the motor. However, they do require the diodes protecting them from the high voltage peaks caused by the motor.
Schematic | Layout | |
Excluding the LCD significantly simplifies the program. One needs,
however, to rename th output ports and other registers according to the PIC
specs. The 12F675 has built-in comparators and ADC that are not used in this
design and must be turned off. Also, all I/O ports must be setup for the
digital mode. Finally, the PIC configuration fuses have some extra bits.
The embedded program source for the second prototype is
photo2.asm
The robot electronics is assembled on a small board available form
RadioShack. To simplify the power supply I added 3 silicon diodes 1N4003 that
drop the 5V voltage down to about 3V for the motor. This way the entire unit
can be powered up from a single 5V source. The maximum current consumption is
about 200ma when the motor is on and just a couple of milliamperes when it is
off. Click on the video clip link below to see the robot in action.
To watch the clip you need DivX codec to be installed. It can be
downloaded from divx.com
Assembly | Video clip | |
![]() click (DivX, ~5.5Mb) |
---|
The code is practically the save as the one for the second prototype
with just a few changes. Two procedures that measure the light intensity
are merged into one and I set up manually all PIC control registers instead
of relying on their default values after power reset.
The embedded program source for the final design is
photo3.asm
The used way for measuring the resistance if not optimal. It takes 2 pins of PIC - one for charging/discharging the cap and one for actually measuring a voltage. This can be accomplished with just one PIC pin. For this disconnect the right (on schematic) end of the cap and attach it to +5V. Rising up the voltage on PIN GP3 (in this case it should be configured for output) will discharge the cap. Now, configure this pin for input, and measure the voltage as described above.
Recently this project got a new life. It was assembled ion a board with SMD components and MOSFET-based H-bridge. The CdS photo-resistors R3 and R4 are still the ones from RadioShack. However, since now I used a bigger ones as in the previous design, their light resistance is much less and is about 300 Ohm. This overloaded PIC's I/O port when the resistor measuring caps C1 and C2 got discharged. To overcome this problem the circuit was equipped with resistors R1 and R2, which decrease the charge-discharge currents through C1 and C2. The photo sensors are mounted inside short heat shrink tubes of about 1cm length to protect them from catching light from a wrong direction. The "nose" is made of thin brass and soldered onto the PCB.
Diode D1 prevents damaging the circuit by applying a wrong polarity from mains. The 7805 chip provides a stable voltage for powering up the microcontroller. Diodes D2 and D3 are used to drop the driving motor voltage down to about 3V. MOSFET arrays Q1,Q2 and Q3,Q4 form the H-bridge which is used to change the motor direction. In the steady state both output pins 2 and 7 are at 0V, transistors Q1,Q3 are open while Q2,Q4 are closed, so the motor does not run. As R3 senses more light than R4, the output at pin 2 jumps to 5V. This causes Q1 to close and Q2 to open, so the motor starts to rotate the head towards the light source. Otherwise, if R4 senses more light, the voltage at pin 7 becomes 5V, which leads to closing Q3 and opening Q4. The motor then rotates in the opposite direction. Rotation of the head stops when both photo resistors sense about the same amount of light. In the program the light intensity corresponds to the values of variables n1 and n2, which vary in the range from 0 to 255 (lower values correspond to less light). The motor stops as the difference |n1 - n2| becomes below 15. This threshold was determined empirically. Capacitor C6 reduces the voltage spikes caused by the motor brushes.
Assembly | Schematic | Board | ||
Note that diodes D2,D3 get pretty hot as the motor runs. I suggest to replace them with a more powerful ones, e.g., BAV23S. Here is the board file for Eagle and updated microcontroller code.
Last modified:Mon, Jan 23, 2023.