Two Inexpensive Line Tracking Sensors for the MIT
 Handy Board Microcontroller
March 2002 
[ Homepage ][ Robotics ][ Robotics Info ][ Send me mail ] © Kam Leang 


Here's a picture of one of my robots sportin' the line tracking sensor described in this article. 
Click here to see a MPG movie (340kB) of this little guy in action tracking a black line.

1  Introduction
The following short article covers the details on making two inexpensive and simple line tracking sensors for a robot which uses the MIT Handy Board microcontroller.  The design of these sensors is somewhat general and can easily be adapted to other microcontrollers, such as the BotBoard by Marvin Green, Parallalax Basic Stamps, to name a few.  The first sensor presented is infrared based, while the second one uses a red LED and a Cds photocell.  The theory of operation, relevant circuit diagrams, list of parts and vendors, assembly instructions, and sample Interactive C code is included.  And of course, I briefly mention some ideas about how to interface the line tracking sensors to other microcontrollers.  Let's get started with how the sensors work.

2  Theory of Operation
A line sensor in its simplest form is a sensor capable of detecting a contrast between adjacent surfaces, such as difference in color, roughness, or magnetic properties, for example.   The simplest would be detecting a difference in color, for example black and white surfaces.  Using simple optoelectronics, such as infrared photo-transistors, color contrast can easily be detected .  Infrared emitter/detectors or photo-transistors are inexpensive (usually under $1 per sensor) and are easy to interface to a microcontroller.  In addition, standard red LEDs and Cds photocells work well too and fall in the same price range as the infrared photo-transistors.

The theory of operation is simple and for brevity, only the basics will be considered.  For more information about the physics of these sensors, please refer to an optoelectronics and heat transfer text.  Suffice for now, we will consider the basic effects of light and what happens when it shines on a black or white surface.  When light shines on a white surface, most of the incoming light is reflected away from the surface.  In contrast, most of the incoming light is absorbed if the surface is black.  Therefore, by shining light on a surface and having a sensor to detect the amount of light that is reflected, a contrast between black and white surfaces can be detected.  Figure 1 shows an illustration of the basics just covered.


Figure 1.  Light reflecting off a white and black surface. 
More light is reflected from the white surface compared to the black surface.

Using what we know about light, and black and white surfaces, the objective of tracking a line is simple can be achieved using the appropriate sensors.  In this article, we will consider the use of three pairs of emitter and detector as shown in Figure 2 below.  The drive configuration for the robot is assumed to be differential, i.e., like the tracks of an army tank vehicle.  From the figure, the three pairs of sensors are used to keep the robot on the line as it moves.  Each sensor output is monitored to determine the location of the tape relative to the robot.  The main objective of the robot is to position itself such that the tape line falls between the two extreme sensors.  If the tape line ever ventures past these two extreme sensors, then the robot corrects by turning in the appropriate direction to maintain tracking.  Two different types of light sensors set up in the configuration shown below will be used for line tracking.  Sample Interactive C program for line following is presented below in Section 4.


Figure 2.  Top view of a robot using three pairs of emitter and detector sensors for line following.

3  Construction
Constructing the line sensor is straightforward.  I would recommend that a prototype be made to test the operation of the sensor before committing to any permanent construction.  Suggestions are presented below relating to sensor separation and placement, but you should experiment with your own configuration to see what works best.   You have the option of building the infrared version or the version which uses red LEDs and Cds photocells.  Refer to Section 6 for a performance comparison of the two types of sensors.

3.1  Circuit Diagram
The infrared emitter and detector sensors are shown below in Figure 3.  You can buy these from a number of vendors and depending on where they were purchased, the sensors shown in Figure 3 may differ in color and packaging from ones you're using.

Figure 3.  Infrared emitter and detector sensors.

The circuit diagram is shown below in Figure 4 and only one set of emitter/detector sensor is depicted.  The remaining two are constructed in the same manner.  Pay close attention to the anode (+V) and cathode (GND) lead for these sensors.  If you plan on making the sensor from color LEDs and Cds photocells, use the same circuit diagram shown below in Figure 4, but replace the IR emitter with a red LED and the IR detector with a Cds photocell.

Figure 4.  Circuit diagram for the infrared emitter/detector line sensor for the MIT Handy Board.  If you plan on making the sensor from color LEDs and Cds photocells, replace the IR emitter with a red LED and the IR detector with a Cds photocell.  Click here for a circuit diagram if the sensor is used with other microcontrollers.  Note the subtle difference in the way the IR detector is configured for this design.

Each detector sensor is connected to the analog input port of the Handy Board microcontroller.  Since the output of each detector may vary in range depending on the type of surface, the black and white tolerance should be adjusted accordingly (see the calibration function in Interactive C program below).  If the following two sensor designs are to be used with other microcontrollers other than the Handy Board, similar interface technique should be used as described, i.e., connect the detector outputs to available analog input ports of the target microcontroller.  Click here for a circuit diagram if the sensor is used with other microcontrollers.  Note the subtle difference in the way the IR detector is configured.

3.2  List of Parts
As mentioned above, the line sensor can be made from either infrared emitter/detector pairs or from LED and CDs photocells, and Tables I and II list the required parts for both.  For best performance, try to match the infared emitter/detector pairs during your purchase.  High intensity red LEDs are recommended for the other sensor design.

Table I.  Parts list for infrared line following sensor.

Qty
Part
Vendor
Part No. 
$ Each
3
IR Photo Transistor
 Digikey
 QSD123QT-ND
 $1.80
3
 High-OutputIR Emitter
(or similar)
Radio Shack 
 276-143
 $3.00
 3
220 Ohm Resistor
Digikey
Radio-Shack
N/A 
 $0.30
 1
Small Printed Circuit Board (PCB)
Radio-Shack
 910-1268
 $2.00
3
Small lengths of 1/4" diameter heat shrink tubing
Radio-Shack
 
$0.50


Table II.  Parts list for color LED and Cds photocell line following sensor.

Qty
Part
Vendor
Part No. 
$ Each
3
Red LEDs
 Radio-Shack
900-6110 
 $2.50
3
 CdS Photoresistors
Radio-Shack 
 276-1657
 $3.00
 3
220 Ohm Resistor
Digikey
Radio-Shack
 N/A
 $0.30
 1
Small Printed Circuit Board (PCB)
Radio-Shack
910-1268
 $2.00
3
Small lengths of 1/4" diameter heat shrink tubing
Radio-Shack
 
$0.50


3.3  Prototype Assembly
I recommend that a prototype of the sensor be made first for testing.  Depending on the application, slight changes may be required.  Position the sensors on a small PCB with the spacing shown in Figure 5 below.  This recommended spacing works well for 3/4" width tape lines, but experiment with various spacing to get the most optimal design.  The sample Interactive C program presented below was written for this configuration.  However, you can experiment with various arrangements and the number of sensors, but be sure to tailor the sample code to work with your design.


Figure 5.  Sensor spacing.
The assembled prototype is shown below in Figure 6.  Note that heat shrink tubing was added to isolate each detector/emitter pair to increase sensitivity.

Figure 6.  Sensors assembled on a small PCB.  Note the use of heat shrink tube to increase sensitivity.

And finally, the mounted prototype sensor and robot system is shown below in Figure 7.  The sensor should be such that is rides between 1/16" to 1/8" above the ground.


Figure 7.  The prototype sensor and PCB.  Make sure the sensor is positioned between 1/16" to 1/8" above the ground.

The LED/Cds photocell version of the line sensor is shown below in Figure 8.  Notice that red LEDs were used to emit light and the Cds photocells detected the reflection.  Please refer to Section 6 for a comparison of the performance between the infrared and LED/Cds photocell versions of the line sensor.

Figure 8.  Red LED and CDs photocell line sensor.

4  Sample Code
4.1  Sample Interactive C Code for the Handy Board Microcontroller
A sample Interactive C code used to track a 3/4" black tape line on a white or near white surfaces is shown below in Listing I.  The algorithm is straightforward.  A configuration routine is run to determine the required tolerances between black and white surfaces for each sensor.  Once tolerance values have been established for each emitter/detector pair, the robot uses these values to determine if a line is present.  If not, then a scanning routine is performed to detect the line.  Once the line is found, the robot tracks it by correcting appropriately based on the location of the tape line relative to the three sensors.

Listing I.  Sample Interactive C line following program.  To download, click here.

/* LINETRAC.C
    Kam Leang March 2000
    kleang@eng.utah.edu
    http://www.leang.com/kam

    Line tracking program for robot using 3 sensor pairs.
    Robot is assumed to have differential drive configuration.

    To run the program, turn on Hand Board.  Place the robot on a
    no-tape surface and press "stop" to acquire calibration value.
    Next, place robot on black tape surface and press "stop" to
    acquire calibration value.

    Then, to run place robot on tape line and press "start".  Robot
    will track continuously.
*********************************************************************/

int motl=0, motr=1;   /* Define motor left, right motor ports */
int s=25;             /* Define motor speed level */
int l=4, m=3, r=2;    /* Define left, middle, right sensor ports */

/* Declare variables */
int l_notape,m_notape,r_notape;
int l_tape,m_tape,r_tape;
int l_tol,m_tol,r_tol,d;

void main(){
    calibrate();      /* Run calibration to determine tolerance */
    init();           /* Run initialize routine */
    d=3;              /* Assume right sensor saw tape last */
    while(1){         /* Scanning routine, look for tape */
      if (d==3) {
  while((analog(l)<=l_tol)&&(analog(m)<=m_tol)&&(analog(r)<=r_tol)){
         motor(motl,s);
         motor(motr,-s);
         if (analog(l)>l_tol) /*Check which sensor saw tape last*/
            d=1;
         if (analog(m)>m_tol)
            d=2;
         if (analog(r)>r_tol)
            d=3;
         }
      }
      while((analog(l)>l_tol)||(analog(m)>m_tol)||(analog(r)>r_tol)){
      motor(motl,2*s); /*Drive straight routine once tape found */
      motor(motr,2*s);
      if (analog(l)>l_tol)
            d=1;
      if (analog(m)>m_tol)
            d=2;
      if (analog(r)>r_tol)
            d=3;
      }
      if (d==1) {  /* Find tape if lost */
      while((analog(l)<=l_tol)&&(analog(m)<=m_tol)&&(analog(r)<=r_tol)){
         motor(motl,-s);
         motor(motr,s);
         if (analog(l)>l_tol)
            d=1;
         if (analog(m)>m_tol)
            d=2;
         if (analog(r)>r_tol)
            d=3;
         }
      }
    }
}

/********** Subroutines ***************************************/
void calibrate(){/* Calibration subroutine to determine tolerance*/
   int temp;
   while(!stop_button()){
      l_notape=analog(l);
      m_notape=analog(m);
      r_notape=analog(r);
      printf("No Tape: %d %d %d\n",l_notape,m_notape,r_notape);
      sleep(.1);
   }
   tone(800.,1.);
   while(!stop_button()){
      l_tape=analog(l);
      m_tape=analog(m);
      r_tape=analog(r);
      printf("Tape: %d %d %d\n",l_tape,m_tape,r_tape);
      sleep(.1);
   }
   temp=(l_tape-l_notape)/2;
   l_tol=l_notape+temp;
   temp=(m_tape-m_notape)/2;
   m_tol=m_notape+temp;
   temp=(r_tape-r_notape)/2;
   r_tol=r_notape+temp;
   printf("Tol: %d %d %d\n",l_tol,m_tol,r_tol);
   tone(800.,1.);
}
void init(){
   printf("Press Start to  Go!\n");
   while(!start_button()){}
   tone(800.,1.);
   }

4.2  Software for Other Microcontrollers
In order to use the line sensor with other microcontrollers, use the circuit diagram that has been modified for other microcontroller with analog input ports similar to the Handy Board.  Tailor the above Interactive C line following algorithm to suit the target microcontroller.  With some simple adjustments, the line sensors can easily be interfaced to just about any popular microcontroller available.
5  Final Design of the Sensor and Robot
After the prototype sensor was built and tested, one of my robots was fitted with a final sensor system.  A sensor mount was built for the robot to make the line sensor more robust and permanent.  Figure 9 is the robot with a new face plate housing the line following sensor.

Figure 9.  Robot with new face plate to house line sensor.

5.1  Sensor Housing
In order to increase sensitivity, each emitter and detector had to be isolated from each other.  As mentioned above, using heat shrink tubing will suffice; however, for a more robust design, a sensor housing (Figure 10) should be used to enclose each sensor.  Note that the material chosen was black Delrin plastic!  I recommend that you use black plastic materials.  Avoid materials such as such as acrylic, Lexan, or white plastic because they are transparent to infrared or visible light.  Figure 11 shows the sensor mounted to the base of the robot.  Note that additional sensors have been added to the design to make the system more flexible with the option of tracking a variety of line widths.

Figure 10.   Inserting the sensor/PCB unit into the newly designed housing for mounting to the robot.

Figure 11.  The new line sensor and housing mounted to the base of the robot.

6  Performance
A comparison was made between the IR and LED/CDs photocell line sensor.  Both sensors were easy to construct and cost about the same amount, however; the IR sensor out performed the LED/CDs version because of two observed reasons: (1) larger sensor output range and (2) robustness.  First, the output of the IR sensor showed higher contrast between black and near white surfaces, i.e., had more range resulting in a definite distinction between black and white surfaces.  During test runs, the IR sensors worked great, even when the black tape was place on a wooden floor or on a gray/white floor as shown in the photo at the top of this article (that was my kitchen floor!).  The sensor could easily track a black tape line on almost any color floor other than black or near black, which makes it a very robust and reliable line sensor.

On the othe hand, with careful tuning the LED/Cds version worked well, unfortunately, required more maintenance.  Variations in floor color and shades greatly effected the performance of LED/Cds sensor.  If you are using the LED/Cds photocell, be sure the surface color is nearly homogenous, i.e., no altering shade of gray and white, etc.  One advantage of the LED/Cds sensor is that it is obvious when the sensor is turned on because of the red LEDs.  In the case of the IR sensor, no visual indication is present, which can make debugging somewhat a pain in the arse!

Overall, both sensors worked great for line following.  The inexpensive design and ease of interface makes them great for robotics applications.

Check out the movie of the robot in action using the line sensor:  [ Click to view MPEG movie (340kB) ]

7  Acknowledgements and References
A big thanks to all of the robot builders over the Net that have somehow influenced the creation of this article!  Also, thanks to the 1999-00 mechatronics students for demanding this article.  I'm a really busy guy these days, but if you want, try to direct all comments, suggestions, and complaints to kam@leang.com and I'll do my best to respond.  Good luck!
 

  • Carr, Joseph "Designer's Handbook of Instrumentation and Control Circuits", Academic Press, Inc. 1991
  • Martin, Fred G. "The Handy Board Technical Reference Manual" MIT Press.  1998
  • Green, Marvin "The BotBoard Owners Manual V1.03"