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.
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.
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
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
â€œ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.
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.
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
Ton bsf GPIO,2
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.
Click here to download code:
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.
The following schematic is for MY robot that drives forward and reverse.
64 Responses to “DIY Line Following Robot 2”
Leave a Reply