Do-it-yourself projects and technology updates

DIY Line Following Robot 2

Filed under: DIY Projects, Microcontrollers, Robots Greg Lipscomb on December 13, 2005 @ 4:02 pm



Introduction:
Well, I have finally had a chance to get over all of my finals, and hopefully did well. I will find out on Monday. Well, as I have promised, here is my next DIY how to project. Beware, it is going to have to be in depth, and long. As any good engineering project goes, we will start out with a problem and a solution. The problem is going to be to build a Lego robot with sensors, a motor controller, and a microcontroller that 1. Follows a black tape. 2. Upon reaching the end of the tape will pause for three seconds. 3. After pausing for 3 seconds will return back to the starting point.

The next step in an engineering problem is to come up with a solution. Ideas of implementation must be thought of. There are two possibilities of making a robot come back to the starting point after pausing for three seconds. The first and most obvious way is to make the robot turn around and then drive back to the starting point following the line. This way of implementing has a lot of benefits. The first is that it is the easiest to implement, and it is relatively straight forward. When the robot hits the end of the tape, the sensor will notice that it is not sensing the tape anymore, and this will cause the robot to turn around and when it sees the tape again, will continue on its path. There is one problem with this way though. It is common.

We have all seen a line following robot, and it is neat, but not impressive. I wanted to do something that has not been done. I chose to implement my robot the second possible way, and the hardest way to implement. I wanted my robot to sense the end of the line, and then pause, and after a three second pause, I wanted it to drive in reverse and follow the line going backwards using a second set of sensors, so this is the robot that I will present. The only warning, and sad part of this project is that I can’t find my code. It is on a disk somewhere, and I have no idea where that is. I will provide code for a simple line-following robot though, and will explain the differences. In essence, I will tell you how to build both robots. On a positive note though, I do have a schematic of my robot, so you can see exactly how it is done.

Simple Line following robot:
We now have an idea of what we want to do, so we need to decide what circuits we need, and how to implement them together. We need to break this project into its components, and that is exactly how I did it. Once again, the circuits that we need are 1. An LED detector circuit. 2. A motor controller. 3. A microcontroller to do all logic. 4. 5V voltage regulator.

5v Voltage Regulator:
The first part of the circuit is to use a voltage regulator. This is an integrated circuit that will take the voltage from a 9v battery, and output a constant 5v. This is important because the microcontroller is powered by a constant 5v. Here is a schematic of how to connect the wires to a voltage regulator. When you are looking at the 7805 voltage regulator, the left most pin is connected to the +9 volts, and the middle pin is connected to ground. The right most pin will be a constant 5v. This 5 volts is connected to power the LED emitter, the microcontroller, and in my schematic, the H-bridge motor controller.



LED detector:
The LED detector circuit is relatively straight forward. It consists of an infrared LED, and an infrared detector. The infrared detector acts like a transistor. When the infrared light hits it, the detector will complete the circuit. The way this detects a black line is, first you shine both the emitter and the detector down on the floor. If the sensor is over the white floor, the infrared light will bounce off the floor, and will be picked up by the detector. The detector output will now be a high voltage. If the sensor/emitter combo is over a black line, then no light will be reflected, and the output will be a low voltage. This voltage is connected to an analog/digital converter, which is in the microcontroller, and this controller can be programmed to make the car either turn right or left. If you start the car on the right side of the tape, the car will drive forward, but will turn to the left. As the sensor crosses over the black tape, the output will be low, and the microcontroller will tell the car to turn to the right. This happens over and over again, and the car will follow the tape.


There are some requirements on the parts. You don’t want to put too much voltage across an LED. I chose a 180 ohm resistor to put in series with the LED (pictured on the left in my circuit). I did not want to put 5v across my LED, so the 180 ohm resistor cuts that voltage down, and keeps the LED from being destroyed. There are some requirements for the microcontroller A/D converter also. The input voltage has to be less than 5v. The way I did this was to connect my 9v battery to the detector and played with the resistor values until I got the output voltages to what I liked. I built the circuit, and measured my outputs with a voltage meter, with the sensor over a black tape (Vnolight), and with my sensor over the white floor (Vlight). I found Vnolight to be .2v, and found Vlight to be 3.8v, after choosing a 21Kohm resistor for the detector.

I actually had to use two sensor circuits in my design. I needed one in the front of the car when the car was driving forward, and needed one in the back of the car when the car was driving in reverse.



Motor Controller – H-bridge:
The next part of my circuit is a motor controller. I had to use an H-bridge for my motor controller, and I will explain why later. First, I am going to explain a simple motor controller. An NPN transistor can be used to control your motor. If the output pin is set high, it can be used to turn on the base of the NPN, and is basically used as a switch to power the motors. You can see on the robot circuit diagram (This is the schematic for the simple robot that just follows a robot in one direction) that the transistors I am talking about are Q1 and Q2. They are connected to GP2 and GP4 on the microcontroller through a 230 ohm resistor. The collector is connected to the positive pole of the motor, and the other pole is connected to + 5v through a 33 ohm resistor. The emitter of the transistor is connected to ground.
(This schematic is for a simple line following robot using a transistor as a motor controller. It corresponds with the code that I have supplied).



This is the circuit for a simple motor controller. You see that this microcontroller has pins left open, so it has enough pins to use this kind of controller. My robot though, is using all of the pins because of the two sensing circuits, so I had to use an H-bridge. I also need to have the ability to make the motors go backwards, and forwards, as well as turn. I also needed the capability of making both motors stop. If I was just going to drive forward, then all I need is one analog input (for the sensors), and two digital outputs (one for each motor). If I want the car to stop, I put both outputs low, and if I want to turn left, I turn the right motor on, and the left one off. I do the opposite to turn right. Now for my robot, I was using 2 analog inputs and 3 digital outputs. One output disabled my H-bridge, and the other two went to each motor controller. If the right motor output was high, the motor would drive forwards, and if it was low, it would drive in reverse. With this set up, I could either make the car spin left (by putting right motor forward, and the left motor in reverse), spin right, go forward, and go backwards.

This is an integrated circuit that has two amplifying circuits in it. Here is a schematic of an H-bridge. The way this works is that if the input pin (Phase A/B) is high, then it will cause a certain polarity of the two output pins (Out1A and Out 2A), if the phase pin goes low, it will switch this polarity. This will cause a motor to either drive forward or backwards.
Pins:
1. Ground- connected to ground for the entire chip
2. Phase A- connected to microcontroller to control Right motor
3. Enable A – this has to be grounded to enable the circuit
4. Out 1A – this is connected to one pole of the motor
5. Vea – This is connected to the emitter of the transistors, and needs to be grounded
6. Out 2A – this is connected to the other pole of the motor
7-12. These are the same as the first 6, and are connected the same

The motors that we used were geared LEGO motors.
Download H-bridge pdf



Microcontroller:
The Microcontroller that I used was a Microchip PIC 12C672. This is an 8 pin chip that does all of the logic for the robot. It is hard to understand, but I will do my best. For my robot, I made 2 analog inputs, I used pins 7 and 6, which was AN0, and AN1. (This all has to be set in the program that is written.) I used pins 5 (GP2), 3 (GP4), and 2 (GP5) as digital outputs. I used GP2 and GP4 to control my motors, and GP5 as an enable to make the motors stop. The PIC chip has a built in analog to digital converter. The first part of the code is setting up the A/D converter, as well as setting the pins up for either analog input, or digital output. There are multiple registers in the chip, and different registers (working ram for the chip), are in two different banks. That is what the bsf STATUS,RPO command is doing. BSF means bit set, it is setting the RPO bit in the status register to one. This selects bank one. The command movlw 0×04, sets the literal value of 04 hex, or 00000100, into the working register, and the next command movwf ADCON1, moves the contents of the working register into the functional register. ADCON1, is the register that tells what pins are set up as input or outputs.



After all of the settings are set for the A/D converter, you have to start the conversion. You need one conversion for each cycle of the code. This will take in the analog voltage from the sensor, and convert it to an 8 bit number. The catch is that you have to wait for the A/D conversion to finish before you can move on to retrieving the data, deciding if you are on white or black tape, and deciding which way to turn the car.

To start the conversion you type the code, bsf ADCON0,GO, This code sets the GO bit on the ADCON0 register to 1, which tells the chip to start the conversion. This bit will automatically reset to zero when the conversion is done, so to check that, you constantly check to see if it is zero, if it is not you go and check it again. This is via a simple loop using the code

one btfsc ADCON0,GO
goto one

“One” is a address identifier, btfsc means “bit test f, skip if clear”. The functional register is the ADCON0 register, and the bit that you are testing is the “GO” bit. You are checking to see if it is a 1 or a 0. If it is a one, the test failed, and you read the next line, which says goto one. This is a jump routine. If it is zero, then you skip the next line, which means that the A/D conversion is done, and you can go on with the code.

The next set of code reads in the data, decides white or black, and then tells the program what to do.

movf ADRES,0
movwf resss
btfss resss,7

This movf command is moving the contents of the ADRES (A/D converter results) register to the functional register. This is the register where any mate, or testing can be done. You can not put any literal values straight into it. If you wanted to move the number 8 into the f register, you would first have to move it into the working register using, movlw 0×08, you then would move the contents of the w register into the f register using the command movwf (whatever register name you want). Anyway, the results are copied into the resss (results) memory spot, which was defined at the beginning. It is now going to check if white or black. Since the vlight = 3.8, and vnolight(tape) = 0.2, and any voltages between these values is going to be digitized into an 8 bit number from 00000000 to 11111111, then if you test to see if bit 7 is set or cleared, you can tell when the voltage is half way. This is an easy way to check if white or black. White would be set, and black would be not set.

btfss resss,7
goto Toff
goto Ton
This set of code is first checking, and if it is set, you go to Ton, if it is not set you go to Toff.

Toff bsf GPIO,4
bcf GPIO,2
goto loop
Ton bsf GPIO,2
bcf Gpio,4
goto loop
end

Toff subroutine sets GPIO4, and clears GPIO2, making the car turn right, whereas Ton does the opposite, and makes the car turn left. At the end of this decision making, the routine jumps back to loop, and does and A/D conversion and continues the process.

I am not going to attempt to give an exhaustive explanation of the microcontroller. It is very complex, and is out of the scope of this how-to project. I may eventually do an entire tutorial on using this PIC chip. The code that I have provided once again, is a simplified code for a simple line following robot. Mine (Which I can’t find) has a few differences. First, I set up different pins, and am using 2 sensors. If the car is driving in reverse, it needs an entire different routine because, you are setting up a different A/D conversion, and the car steers differently in reverse. Also, I have a routine that has a timer. If it sees white for a predetermined time, it activates the enable pin for the H-drive, which stops the car. It then goes through a loop counting for 3 seconds. After that it switches the car in reverse and switches to the back sensor circuit.



Code:

Click here to download code:

Conclusion:
In conclusion, my robot worked as expected. It would follow a line, pause, and then return by driving in reverse. You can view the working robot on this video. If any of you have any questions, or if any of this is unclear, let me know, and I will try to explain it better.
View video

The following schematic is for MY robot that drives forward and reverse.

58 Responses to “DIY Line Following Robot 2”

  1. kaz_ Says:
  2. hey, i’ve been reading the site for a while but have never commented. just wanted to let you know i really like the projects and keep up the good work!

    // kaz_

  3. Administrator Says:
  4. Thanks Kaz. I am glad you enjoy them. I will keep them up as much as I can.

    Greg

  5. TeamDroid Says:
  6. DIY Line Following Robot2

     
     There you go, a fantastic DIY project that involved LEGOs, electronics, and robotics.
    As any good engineering project goes, we will start out with a problem and a solution. The problem is going to be to build a Lego robot with sensors, …

  7. .mindless Says:
  8. Hey! It’s a line sniffing robot, how cool. Getting enough coke wont be very cheap tho.. but cool anyway!

  9. mcbigboy Says:
  10. Will, I love your liller robot. I was think, I know its a bad thing at times, but can this be done with the LEGO Robotics? I’m going to get my set out and look at the software and see. I know I’ve build a line robot before. Keep up the good work.

  11. GRYNX » Blog Archive » Line following robot Says:
  12. [...] Link: Line following robot [...]

  13. shawn Says:
  14. Excellent explanation of procedures and concepts. Extremely hard to find another engineer that can explain stuff in everyday language .

  15. Administrator Says:
  16. Thanks. I really enjoy doing this kind of stuff, and explaining how to do it. I am glad you like my stuff.

    Greg

  17. Jim Says:
  18. Yea, you can buy one of these on the TV for $14.99… 2 cars, and includes two “mats”… a city scape map, and a jungle scape map…

    it follows a line drawn by a dry erase marker…

  19. Daniel Jackson’s Blog » Blog Archive » DIY Line Following Robot Says:
  20. [...] When surfing hack a day, I found this neat line following robot that uses LEGO bricks. What this person did was created a light sensor that will follow a black line. LEGO released a light sensor for their Mindstorms kit long ago, but for someone that doesn’t have an RCX brick and/or a Mindstorms kit, this is a neat, if not more technical way to make a sensor. This was the guys objectives: 1. Follows a black tape. 2. Upon reaching the end of the tape will pause for three seconds. 3. After pausing for 3 seconds will return back to the starting point. Pretty neat that it works! Click on the picture to view a video, and here.   [link] [...]

  21. Administrator Says:
  22. Thanks a lot

  23. DIY Live » DIY Archive » Ten most needed circuits for the DIYer Says:
  24. [...] Line Following Robot [...]

  25. Andrew Says:
  26. I think theres a minor mistake in the article where you reference Vlight and Vnolight with the programming part. I think you may have switched up the two values.

    “Since the vlight = 0.2, and vnolight(tape) = 3.8″

  27. Andrew Says:
  28. Other than that, it’s perfect!

    Nice article, keep up the good work!

  29. Administrator Says:
  30. Thanks Andrew. I see that I mentioned it correctly when I introduced the topic. I fixed it. The funny thing is that I actually presented it to my class that way. Whoops. I can’t remember what we were thinking, but it seems like that is the way that they wanted us to present it. I don’t even know what the variables mean, but I am making this project, so I changed it to what makes sense. Thanks for pointing it out.

  31. dan Says:
  32. At Carnegie Mellon we had a contest like this, called “Mobot”. There were lines drawn along the sidewalk prior to the competition, and on competition-day the contestants were told which route to follow and gates to pass through, etc.

    One of my buddies did a lego Mobot; quite impressive…

  33. anonymous Says:
  34. Why don’t you just hook a few photoresistors to a comparator? Look at Sandwich at http://www.robotroom.com.

  35. DIY Live » DIY Archive » Run out of pins on your PIC chip? Says:
  36. [...] Line Following Robot [...]

  37. xxEnfusionxx.com :: December :: 2005 Says:
  38. [...] Sounds hard huh? Impossible for the common reader. Think again, Robot Building for Beginners by David Cook offers a simple guide inside the electronics and robotics world. Offering a step-by-step tutorial to Sandwich, a simple line following robot costing a mere $20 in parts. Or you can even buy a printed circuit board to make things easier( that will set u back $12). For those who want more, you can always try building a custom one as did [greg] from DIY Live. His line following robot post, includes the technical know-how for the more experinced reader who is not afraid of soldering irons. Comments (0) [...]

  39. xxEnfusionxx.com :: Line Following Robots :: December :: 2005 Says:
  40. [...] Sounds hard huh? Impossible for the common reader. Think again, Robot Building for Beginners by David Cook offers a simple guide inside the electronics and robotics world. Offering a step-by-step tutorial to Sandwich, a simple line following robot costing a mere $20 in parts. Or you can even buy a printed circuit board to make things easier( that will set u back $12). For those who want more, you can always try building a custom one as did [greg] from DIY Live. His line following robot post, includes the technical know-how for the more experinced reader who is not afraid of soldering irons. Comments » [...]

  41. Donghai Ma » links for 2006-01-04 Says:
  42. [...] DIY Live » DIY Archive » DIY Line Following Robot 2 (tags: DIY hacks robotics electronics projects) [...]

  43. Greg Lipscomb Says:
  44. list p=12c672
    #include
    count equ H’0025′ ;defines count variable and gives a memory address
    resss equ H’0030′ ;defines resss variable and gives a memory address
    org 0×00 ; defines the start of memory as 00h
    reset
    goto start
    ; initialize the settings of the A/D converter, and picks input/output pins
    start org 0×05 ; defines where to start the code
    bsf STATUS, RPO ;selects bank 1
    movlw 0×04 ; moves 04 hex into working register
    movwf ADCON1 ; moves working register into functional register ADCON1
    movlw 0×03 ; moves 03h into working register
    movwf TRISIO ; moves working register into TRISIO
    bcf STATUS, RPO ;selects bank 0
    movlw 0×89 ; moves 089h into working register
    movwf ADCON0 ; moves w into f

    ;subroutine that starts A/D converter, and checks results
    loop movlw 0×10 ;put 10h into the working register
    movwf count ;move contents of working register into f register
    good decfsz count,1 ;decrements count by one, skips next if zero
    goto good ;if not zero go to good
    bsf ADCON0,GO ;starts A/D by setting GO bit of ADCON0
    one btfsc ADCON0,GO ;tests to see if A/D is done, skips next code if 0
    goto one ;if not zero go to �one�
    movf ADRES,0 ;
    movwf resss ;moves results into ress
    btfss resss,7 ;tests to see if results bit 7 set, skips next if set
    goto Toff ;calls Toff subroutine
    goto Ton ;calls Ton subroutine
    Toff bsf GPIO,4 ;sets GP4 to high output
    bcf GPIO,2 ;sets GPIO2 to low output
    goto loop ;jump to loop subroutine
    Ton bsf GPIO,2 ;sets GPIO2 to high output
    bcf GPIO,4 ;sets GPIO4 to low output
    goto loop ; starts A/D conversion over again
    end

  45. Mark Says:
  46. Hi Really liked the idea of this project,
    im a new comer to robotics although i have done a electronic
    projects before, would really like to undertake doing one of these
    but i have never used a H-Bridge before in a project, and the only
    H-Bridge i have access to is the STMicroelectronics L298N, spec
    sheet here:
    http://www.st.com/stonline/products/literature/ds/1773.pdf
    would anyone know how to integrate this H-Bridge into this
    project, as it would seem to have 4 input pins instead of the
    2 phase pins used on the H-Bridge in this project, or could
    someone suggest a replacement H-Bridge that would do a
    similar job, cheers for any help.

  47. Greg Lipscomb Says:
  48. You probably won’t need a Hbridge unless you want to make the robot go backwards also. You will just need two transistor motor controllers. The code provided is for a simple line following robot.

  49. Greg Lipscomb Says:
  50. Hey, you need to buy a pic programmer. I have the PICkit 1 Flash Starter kit. It cost like $30 or so. You can get it at http://www.microchip.com

  51. Greg Lipscomb Says:
  52. There is a video. Look at the bottom of the article

  53. DIY Live » DIY Archive » Line Following Robot code Says:
  54. [...] Line Following Robot [...]

  55. Sardar Says:
  56. Hello..
    i like your project.

    Could you tell me the Practical use of it.

    Thank you.

    Wishing for Positive response.

  57. Ahmad Says:
  58. i hv a question about the Q2N3904 i brought that transistor but this transistor hv 3 legs so i connected the 2 legs but still one leg free since on the schematic only shown 2 legs !!! What about the 3rd leg

  59. Ahmad Says:
  60. anothe question if i didnt put an H-bridge will the circuit work properly? or not yet cz
    that 12 pin H-bridge i didnt find it in the market :S

  61. simonlee Says:
  62. i like the robot, i will build it as my enginerring degree final year project. thanks for your
    priceless information. i really appreciate it……..

  63. mostafa Says:
  64. Very cool,But u can tell us the code for stopping and reversing please….If u please,tell me can i use any other dc motor or stepper motor istead of geared lego motor ?

  65. dosibule Says:
  66. like robot too

  67. Mostafa Says:
  68. i don’t find this H-BRIDGE anywhere can anyone tell me how to make the L2980 make the same thing ?

  69. styl Says:
  70. hii greg tis s styl here . found ur s very interesting . i’m doin a mini project on ur “line following robot”. i lk’d ur work very much tat i wish u may suggest me some more innovative tips in it……….. hope u…………. tanku

  71. Mo Says:
  72. Nice idea, what I would like to know is whether I can port the program
    to my PIC16F873A and get it to work normally.

  73. chek Says:
  74. hi..it’s really fantastic project from you..I surely like it and really interested to know better
    about it..I like to do this project as my final project..can i get the list of all the component that
    you used in this DIY Line Following Robot 2 ?..can you share it with me..

  75. rICK Says:
  76. Keep up the good work. Pay no mind to those who like to criticize about
    origins of the diagrams, nobody has a patent on these and wikopedia also
    borrows them. If anyone can be used as an example of borrowing certainly
    they are the best at it. Just make sure you do not have any inaccuracies.
    Correct them. Your site is a great service to those who want to experiment,
    hobby, or just luv this stuff. Thanks.

  77. Iqbal Says:
  78. Hi,

    Nice to see your efforts. Well about line following robotics, mostly the line is either black on
    white or vice versa. I wanna ask, how we can track a line of Orange colour? Is it possible.? Could
    you please email me ! Tx & Bye

  79. Ygrek Says:
  80. This is cool for a beginer !!!
    I can’t make the hex file with this code….
    Can somebody sent the hex file of this code?
    Thanks a lot.

  81. Warren Dean Says:
  82. Hello, brilliant curcuit! just one question, what h-bridge are you using to control
    the motors?

  83. Warren Dean Says:
  84. Gooday.
    i have built this awesome circuit but i have one problem, i cant program the pic. i have a
    registry error on ADCON1.
    can anyone help me with the hex file that works.
    wazi555@yahoo.com
    thanks
    Warren

  85. Priya Says:
  86. Hi Greg! Can I use Atmel AT89C2051 controller instead of using PIC coz I dont think it will be
    available easily?
    Neways,it was gud n thanx. :)

  87. Karan Says:
  88. hey do you think it would be possible to send me a part list…and further how would you update this project so that it works on a PIC16F877?

  89. sam james bains Says:
  90. hi,

    bye!

  91. satheesh Says:
  92. sir,

    actually i have a seminar on line following robot on 12th nov.so plz send me description of ckt coding,applications and advantages.thanking you sir.

  93. OBIAJULI ALIGBE Says:
  94. HOW CAN I REGISTER TO A MOTOR ENGINERRING SCHOOL

  95. OBIAJULI ALIGBE Says:
  96. SO HOW CAN I REGISTER INTO YOUR SCHOOL

  97. OBIAJULI ALIGBE Says:
  98. SO HOW CAN I GET ALL THE EQUIPMENT AND MANNUAL SO THAT I CAN TRY TO DO WHAT YOU DID

  99. Doodee Says:
  100. Thanks for sharing

  101. DeemyReurce Says:
  102. I’d prefer reading in my native language, because my knowledge of your languange is no so well. But it was interesting! Look for some my links:

  103. ahmed Says:
  104. how it move in one direction

  105. Data Conversion Solution Says:
  106. Data Entry & Data Conversion India

    “One of the big advantages to outsourcing is flexibility–it can be a lot easier to cut back on a vendor than an employee. (Think of how you would feel if you had to tell an employee who is dependent on their job that you only need them half-time now….

  107. kishore Says:
  108. whays the cost of this robot

  109. kishore Says:
  110. whats the cost of this robot

  111. hamza Says:
  112. hi

  113. alireza Says:
  114. hi,greg
    i wil builte the line following robot.can you help me?

  115. rahul Says:
  116. hey this ne works but is not fast enough to win races

Leave a Reply