CDS Photocell & Infrared Navigation


 Back To Index

This is a little more advanced experiment that expands on the fist basic example of using CDS photocells shown HERE to make Micro-Bot turn towards the brightest light source while moving about using infrared sensors for object detection & avoidance.

Build each of the circuits shown below for this experiment.

Parts List:

bullet

2 x Photonic Detectors, Inc. 10K to 200K CDS Photocells

bullet

2 x 0.01uF 50V Ceramic Caps [marked 103] or 1 x 0.1uF 50V Ceramic Caps [marked 104]

bullet

2 x 220Ω 1/8W Resistors [RED, RED, BROWN]

bullet

2 x 430Ω resistors [YELLOW, ORANGE, BROWN]

bullet

2 x OED-EL-1L2 Infrared LEDs with heat shrink

bullet

1 x 10uF Electrolytic Capacitor

bullet

1 x 10KΩ resistor [RED, BLACK, ORANGE]

bullet

1 x 100Ω resistor [BROWN, BLACK, BROWN]

bullet

1 x TSOP1740 40KHz Infrared Photo Module

Be sure to place both CDS photocells on the front of Micro-Bot similar to the placement diagram shown below. This will force Micro-Bot to steer towards the brightest spot in the room while moving forward.


CDS Photocell Placement Diagram


CDS Sensor Circuit

Next build the infrared circuits on the front end of Micro-Bot. Place one IR LED on the left, one on the right, and put the infrared detector between the LEDs similar to the placement diagram below.


IR Component Placement Diagram


Infrared LED Drive Circuit


Infrared Detector Circuit

Once you have all of the above circuits wired-in & checked, program the code below into Micro-Bot.

Micro-Bot will wander around avoiding obstacles with the help of the infrared object detection circuits, and increment the CDS_Count variable on each pass. Micro-Bot will use only infrared for navigation until the CDS_Count variable is equal to or greater than the MAX_Count constant.

Once the MAX_Count is reached, program execution will jump to the Check_CDS sub-routine & take light measurement readings from left & right side CDS photocells. Micro-Bot will then turn towards the left or right depending on which CDS photocell returned the brightest light reading.

Try swapping the 0.01uF with 0.1uF capacitors, and experiment with changing the value of the constant MAX_Count. Try using a flashlight in front of Micro-Bot as he moves around. This project is a lot of fun in a dark room with a flashlight.

Increasing the value of MAX_Count will cause Micro-Bot to turn very slowly towards brighter directions. Decreasing it will cause rapid turns & faster reactions to changing light conditions.

If you look closely at this code, you'll see we just clipped bits & pieces from previous IR & CDS experiments, and pasted them together to build this routine. Code examples from individual projects can be combined together to create much more sophisticated routines. It doesn't take long to see the real benefits of having the full version PicBasic Pro compiler vs. the demo version.

'***********************************************
'*  Name    : CDSBOT.BAS                       *
'*  Author  : Bruce Reynolds                   *
'*  Notes   : Micro-Bot CDS & IR Navigation    *
'***********************************************
CDS_Count VAR BYTE     ' Declare counter variable
CDS_Count=0            ' Initialize counter to 0
MAX_Count CON 30       ' Experiment with MAX_Count values to adjust behavior
Right_Val VAR BYTE     ' Right CDS reading for comparison
Left_Val  VAR BYTE     ' Left CDS reading for comparison

INCLUDE "HARDWARE.INC" ' Include hardware setup
  
Begin:
  FOR X=1 TO 4                      ' Move forward
   PULSOUT Left_Servo,ForwardLeft   ' Forward left servo
   PULSOUT Right_Servo,ForwardRight ' Forward right servo
   PAUSE 20		
  NEXT X
  GOSUB Check_Right        ' Check right side for obstacle
  GOSUB Check_Left         ' Check left side for obstacle
  CDS_Count=CDS_Count+1    ' Increment counter
  IF CDS_Count >= MAX_Count THEN Check_CDS ' If >= MAX_Count, check CDS reading

  ' Not up to 30 counts yet, then navigate with IR
  IF (Left_IR=No) AND (Right_IR=No) THEN Begin  ' If no contact skip further checking

KeepChecking:
  IF (Left_IR=Yes) AND (Right_IR=Yes) THEN Back ' If contact on both sides then reverse
  IF (Left_IR=Yes) AND (Right_IR=No) THEN Right ' If contact on left then turn right
  IF (Left_IR=No) AND (Right_IR=Yes) THEN Left  ' If contact on right then turn left
  GOTO Begin

Back:               ' Backwards
  FOR X=1 TO 4	 
   PULSOUT Left_Servo,ReverseLeft   ' Reverse left servo
   PULSOUT Right_Servo,ReverseRight ' Reverse right servo
   PAUSE 20     
  NEXT X
  GOSUB Check_Right
  GOSUB Check_Left
  IF (Left_IR=Yes) OR (Right_IR=Yes) THEN KeepChecking
  GOTO Begin        ' Return

Right:              ' Turn right
  FOR X=1 TO 4   
   PULSOUT Left_Servo,ForwardLeft    ' Forward left servo
   PULSOUT Right_Servo,ReverseRight  ' Reverse right servo
   PAUSE 20
  NEXT X            
  GOSUB Check_Right
  IF Right_IR=Yes THEN Right
  GOTO Begin        ' Return
    
Left:               ' Turn left
  FOR X=1 TO 4    
   PULSOUT Left_Servo,ReverseLeft   ' Reverse left servo
   PULSOUT Right_Servo,ForwardRight ' Forward right servo
   PAUSE 20
  NEXT X           
  GOSUB Check_Left
  IF Left_IR=Yes THEN Left
  GOTO Begin      ' Return
    
Check_CDS:
  CDS_Count = 0                ' Reset counter first
  HIGH Right_CDS               ' Make right CDS pin high
  PAUSE 3                      ' Let cap charge
  RCTIME Right_CDS,1,Right_Raw ' Time & record discharge time
  HIGH Left_CDS                ' Make left CDS pin high
  PAUSE 3                      ' Let cap charge
  RCTIME Left_CDS,1,Left_Raw   ' Time & record discharge time
  Right_Val = NCD Right_Raw
  Left_Val =  NCD Left_Raw   
  
  IF Right_Val > Left_Val THEN
   IF (Right_Val-Left_Val) >= 1 THEN
     FOR X = 0 TO 4
      PULSOUT Left_Servo,ReverseLeft   ' Reverse left servo
      PULSOUT Right_Servo,ForwardRight ' Forward right servo
      PAUSE 20
     NEXT X
   ENDIF
  ENDIF
  IF Left_Val > Right_Val THEN
   IF (Left_Val-Right_Val) >= 1 THEN
     FOR X = 0 TO 4
      PULSOUT Left_Servo,ForwardLeft   ' Forward left servo
      PULSOUT Right_Servo,ReverseRight ' Reverse right servo
      PAUSE 20
     NEXT X
   ENDIF
  ENDIF
  GOTO Begin
    
  END


 Back To Index

Copyright © 2007 Reynolds Electronics
http://www.rentron.com