Arduino Line Follower Robot With QRD1114

26
Arduino Line Follower Robot with QRD1114 This is my first arduino robot, and I am quite happy with the outcome. What I came up with is a robot that with modifications can do just about anything in the realm of small arduino robots. The electronics are very simple. You only need an H bridge, and a simple setup for the sensor that you are using (in this case the QRD-1114 infrared reflective sensor). The chassis is also very simple. implementing the track and wheel set from Tamiya (this worked quite well with the homemade chassis), and is made from materials that are cheap, and easy to come by. If you follow this instructable, you should end up with a small robot that is capable of being configured to avoid obstacles, follow lines, and stay on a table top using the arduino microcontroller. Arduino Line Follower Robot with QRD1114 1

Transcript of Arduino Line Follower Robot With QRD1114

Page 1: Arduino Line Follower Robot With QRD1114

Arduino Line Follower Robot with QRD1114

This is my first arduino robot, and I am quite happy with the outcome. What I came up with is a robot that with modifications can do just about anything in the realm of small arduino robots. The electronics are very simple. You only need an H bridge, and a simple setup for the sensor that you are using (in this case the QRD-1114 infrared reflective sensor). The chassis is also very simple. implementing the track and wheel set from Tamiya (this worked

quite well with the homemade chassis), and is made from materials that are cheap, and easy to come by. If you follow this instructable, you should end up with a small robot that is capable of being configured to avoid obstacles, follow lines, and stay on a table top using the arduino microcontroller.

     If you like the instructable, please vote for it in the arduino challenge. 

Step 1 Materials

This robot can be configured in many different ways, so the materials you will need may vary. Here is a list of parts that I ended up using.for the chassis:

-two pieces of peg board about 6cm by 13.5 cm-assortment of scrap wood-Tamiya track and wheel set (get from amazon)Arduino Line Follower Robot with QRD1114 1

Page 2: Arduino Line Follower Robot With QRD1114

-screws

for electronics:-8x 2222A NPN transistors (you can avoid the use of transistors with the use of H bridge ICs such as the 298)-8x 1k resistors-perfboard-wire-QRD-1114 -10k resistor -68ohm resistor -type M male power jack-Tamiya twin motor and gear box

software:-arduino uno (any other arduino board should work)

Step 2 Building the chassis

I made the chassis based on the universal plate sold by Tamiya. basically you need a piece of pegboard big thin enough that there is enough of the shaft coming out from the side of your gearbox that the tracks won't get caught on the board, that is long enough that you can fit all of your hardware on it. I started by cutting a piece of pegboard about 6 cm wide (the measurement you need if using the twin motor and gear box from tamiya) by 13 cm long. I then put two pieces of wood measuring 7cm by 1.5 cm by1.5 cm on the bottom of the pegboard with equal space on either end of the wood. I then attached two thin pieces of wood to the top of the pegboard on the front of the robot. I drilled a hole on equal distances on both pieces of wood to hold the front axle of the track and wheels. I attached

Arduino Line Follower Robot with QRD1114 2

Page 3: Arduino Line Follower Robot With QRD1114

two smaller wheels to the bottom pieces of wood using the partially threaded screws included in the track and wheel set.   

Step 3 Motors and gearbox

I used the twin motor and gear box from tamiya. I am not going to explain construction, because if you buy it there is instructions included. Be sure that you assemble it as the c type or the low speed type. The c type has a gear ratio of 203:1 as opposed to the  a and b types which have a gear ratio of 58:1. The c type is the only one with enough torque for our purposes. If you are using a different gear box, assemble it with the highest gear ratio  you can. when attaching to the chassis, line up the shafts with the end of the chassis, and screw it on.

note: One of the motors the gear box came with did not work. it would begin to stain and move very slowly after moving for a few minutes. I replaced it with a geared motor I got from radio shack for much better results.

Step 4 Building the H bridge.

The H bridge is an electronic circuit that enables a voltage to be applied across a load in either direction. These circuits are often used in robotics and other applications to allow DC motors to run forwards and backwards. H bridges are available as integrated circuits, or can be built from discrete components.

Arduino Line Follower Robot with QRD1114 3

Page 4: Arduino Line Follower Robot With QRD1114

General

The term H bridge is derived from the typical graphical representation of such a circuit. An H bridge is built with four switches (solid-state or mechanical). When the switches S1 and S4 (according to the first figure) are closed (and S2 and S3 are open) a positive voltage will be applied across the motor. By opening S1 and S4 switches and closing S2 and S3 switches, this voltage is reversed, allowing reverse operation of the motor.

Using the nomenclature above, the switches S1 and S2 should never be closed at the same time, as this would cause a short circuit on the input voltage source. The same applies to the switches S3 and S4. This condition is known as shoot-through.

Operation

The H-bridge arrangement is generally used to reverse the polarity of the motor, but can also be used to 'brake' the motor, where the motor comes to a sudden stop, as the motor's terminals are shorted, or to let the motor 'free run' to a stop, as the motor is effectively disconnected from the circuit. The following table summarises operation, with S1-S4 corresponding to the diagram above

Arduino Line Follower Robot with QRD1114

S1 S2 S3 S4 Result1 0 0 1 Motor moves right0 1 1 0 Motor moves left0 0 0 0 Motor free runs0 1 0 1 Motor brakes1 0 1 0 Motor brakes

4

Page 5: Arduino Line Follower Robot With QRD1114

control the motors, you need a setup called an h bridge. You probably know that when you connect the terminals on the motors to the reversed end of the battery, it turns in the opposite direction. using switches you can switch the polarity of each terminal of the motor, and change the direction. To allow the arduino to make the switches, you need to use an electronic switch. In this case, we will use transistors, but you can also use relays, or even better, a H bridge IC chip. You will need four transistors for each motor. one transistor for each polarity on both terminals. What I did, is I connected two transistor bases together, for each direction the motor would spin, and that would be connected to the output pins on the arduino. I found that for the negative connection, you had to connect the emitter of the NPN transistor to ground, and then the collector the the terminal of the motor, and for the positive connection, you had to connect the collector to power, and the emitter to the motor terminal.

Note: the transistors I used had an Ic max of about 500 ma. The transistors got very hot unless I connected the base of the transistor to the arduino through a 1k resistor. If you choose to make a H bridge with transistors you may want to get a transistor with a higher current tolerance, or maybe even power transistors.

The theory and practice of building H-bridge for controlling brushed DC motors in robotics

Let's start with the name, H-bridge. Sometimes called a "full bridge" the H-bridge is so named because it has four switching elements at the "corners" of the H and the motor forms the cross bar. The basic bridge is shown in the figure to the right.

Of course the letter H doesn't have the top and bottom joined together, but hopefully the picture is clear. This is also something of a theme of this tutorial where I will state something, and then tell you it isn't really true :-).

The key fact to note is that there are, in theory, four switching elements within the bridge. These four elements are often called, high side left, high side right, low side right, and low side left (when traversing in clockwise order).

The switches are turned on in pairs, either high left and lower right, or lower left and high right, but never both switches on the same "side" of the bridge. If both switches on one side of a bridge are turned on it creates a short circuit between the battery plus and battery minus terminals. This phenomena is called shoot through in the Switch-

Arduino Line Follower Robot with QRD1114 5

Page 6: Arduino Line Follower Robot With QRD1114

Mode Power Supply (SMPS) literature. If the bridge is sufficiently powerful it will absorb that load and your batteries will simply drain quickly. Usually however the switches in question melt.

To power the motor, you turn on two switches that are diagonally opposed. In the picture to the right, imagine that the high side left and low side right switches are turned on. The current flow is shown in green.

The current flows and the motor begins to turn in a "positive" direction. What happens if you turn on the high side right and low side left switches? You guessed it, current flows the other direction through the motor and the motor turns in the opposite direction.

Pretty simple stuff right? Actually it is just that simple, the tricky part comes in when you decide what to use for switches. Anything that can carry a current will work, from four SPST switches, one DPDT switch, relays, transistors, to enhancement mode power MOSFETs.

One more topic in the basic theory section, quadrants. If each switch can be controlled independently then you can do some interesting things with the bridge, some folks call such a bridge a "four quadrant device" (4QD get it?). If you built it out of a single DPDT relay, you can really only control forward or reverse. You can build a small truth table that tells you for each of the switch's states, what the bridge will do. As each switch has one of two states, and there are four switches, there are 16 possible states. However, since any state that turns both switches on one side on is "bad" (smoke issues forth),  there are in fact only four useful states (the four quadrants) where the transistors are turned on.

High SideLeft

High SideRight

LowerLeft

LowerRight Quadrant Description

On Off Off On Motor goes ClockwiseOff On On Off Motor goes Counter-clockwiseOn On Off Off Motor "brakes" and deceleratesOff Off On On Motor "brakes" and decelerates

The last two rows describe a maneuver where you "short circuit" the motor which causes the motors generator effect to work against itself. The turning motor generates a voltage which tries to force the motor to turn the opposite direction. This causes the motor to rapidly stop spinning and is called "braking" on a lot of H-bridge designs.

Of course there is also the state where all the transistors are turned off. In this case the motor coasts if it was spinning and does nothing if it was doing nothing.

Building a FET based H-Bridge for line follower robotBipolar Junction (BJT) H-Bridge

Now that we have the transistors picked out, we look at implementing an H-bridge using them. Because these transistors are a complementary pair, the circuit using them is very symmetrical.

The upper right and upper left corners of the H-bridge are called "sources." This name originates from their function which is to be a source of current for the load (in our case

Arduino Line Follower Robot with QRD1114 6

Page 7: Arduino Line Follower Robot With QRD1114

the motor). Referring to the schematic on the right, you can see that there is a 10K resistor between the base of the TIP107 and the positive terminal of the battery. This is a pull-up resistor that insures that the transistor is "off" (or not sourcing any current) when the switch is open. The 1K resistor that is connected in series with the switch is used to limit the current coming out of the base when the base is grounded by the switch. So in this case when the switch is closed, the emitter-base junction becomes forward biased and current flows "out" through the base. If the battery is 12V and the voltage drop of the Base-Emitter junction is .7V, then the current flow in the base will be -11.3mA (negative because it is flowing "out"). Given the Hfe specs for this transistor, this level of base current will completely turn "on" this transistor allowing the load to consume as much current as it needs.

Conversely, the schematic on the left is called a "sink." Its name is derived from this circuit's function which is to provide a place for current to go once it has passed through the load.

The lower left and lower right corners of the H-bridge are implemented as sink circuits. In the sink circuit the 10K resistor connects the base of the transistor to ground, which forces the transistor off when the switch is open. When the switch is closed, current is injected into the base through the now forward biased base-emitter junction. Again the amount of current is nearly identical to the source configuration so an equal amount of

current is allowed to flow through the load.

Of course if we have to throw switches to turn these transistors on it doesn't make for a very useful H-bridge. In order to drive this H-bridge from a microprocessor we need to put the microprocessor pins in control.

In the sink circuit, the transistor stays turned off if the base is grounded, it turns on when current flows through the 1K resistor and into the base. A typical microprocessor's output pin can generally put out either 3.3V or 5V as a logic "true" output. Given that we need only approximately 5 mA to turn the transistor fully on, you could compute the necessary base resistor by subtracting the base-emitter voltage drop (.7V) from the logic high voltage (3.3 or 5) and then dividing by the desired base current (5 mA) to get the appropriate resistor (660 or 1K ohms respectively). The source circuit is another problem entirely and an area I call "the

high side problem."

Turning on the source is easily accomplished by grounding the 1K resistor, however turning it off requires bringing the 1K resistor up to the motor voltage. You could tri- state the output and let the 10K resistor bring it up to the motor voltage. However, if you did, you're microprocessor pin might be exposed to a voltage that is was not designed to handle. A solution for this problem is to add another transistor to the high side which is a sink circuit to turn on the source circuit!

In the schematic you can see that I added a 2N3904 NPN type transistor which replaces the switch in the original schematic. This works because the microprocessor only need turn on the 2N3904 (which is referenced to ground), then that pulls the base of the source transistor to ground through its 1K resistor which turns on the source transistor and current then flows into the load. But as you need to add two transistors anyway to control the two upper corners of the bridge, there is an even better way to go.

The picture on the left is the schematic representation of an opto-isolator. An opto-isolator is an LED and a photo-transistor packaged into a single 4 lead package. You can

Arduino Line Follower Robot with QRD1114 7

Page 8: Arduino Line Follower Robot With QRD1114

get four opto-isolators in a single IC DIP using the NEC PS2504-4. When current flows in the LED the photons it emits illuminate the base of the photo-transistor. This causes the photo-transistor to turn on and begin conducting current. The photo-transistor is completely electrically isolated from the LED so the voltage on the phototransistor has nothing to do with voltage on the LED. This isolation brings with it two benefits and one drawback.

The major benefit is that the photo-transistor can used in place of the switches in our sink and source circuits. This means that the microprocessor sees four LEDs to light up, but what they really do is activate the circuits in the bridge. The second benefit is that the motor power supply with all of the switching noise is completely isolated from the power supply that is powering the microprocessor.

The drawback is that the opto-isolator doesn't switch on and off as nearly as fast as a regular transistor. In fact it can take 4 uS to turn this baby on. That means that simply turning it off and on as fast as possible results in a 125Khz, 50% duty cycle waveform. (4uS on, 4uS off). In practical terms, if you want equal PWM steps from 1% to 100%, then the minimum "off" or "on" time is one 4uS increment. The equal step constraint would limit you to a PWM period of 400uS, or 2,500 Hz. If you don't care about the edge conditions you could choose 1 uS as your increment, then given the minimum 4uS switch time, your duty cycle could be 0%, 4%-96%, and 100%. The gaps at the low end and high end represent the 4uS switching minimum. The benefit however is that your base period is now 100uS and your base frequency is 10Khz. In summary, a completely symmetric PWM control will be no faster than 2500 Hz. If the areas of very low duty cycle and very high duty cycle are un-interesting to your application, then the frequency can be increased. 

The final thing we have to consider before we design the circuit is the nature of DC motors.  A DC motor looks like a resistor and an inductor (coil) to the H-bridge. The resistor doesn't cause us much grief, but the inductor does. That is because inductors do not like being switched on and off. When the current through an inductor changes, the voltage across it goes up in proportion to inductance and the rate of change in the current. This is expressed in engineering texts as V = L(di/dt). Well switching off 6 amps of the current in 4 uS means the the voltage is something like L*(6/.000004) or L*1,500,000. Clearly you aren't going to see several million volts but you will see a spike in voltage when the inductor is switched.

I'll dive a bit more deeply into this phenomena in the analysis section but for now this means we need some diodes (called free wheeling diodes) to direct those voltage spikes to places where they can do no harm. Fortunately, the TIP107 and TIP102 have freewheeling diodes built into them, but if you decide to use transistors that don't have built in diodes the circuit in the next section provides for them.

Building the BJT H-Bridge for our line follower robot

I really like this H-bridge for a number of reasons, not the least of which is that if you buy your parts in quantities of 1, all the parts cost under $5 (excluding tax & shipping of course) for a 1 - 5 amp capable H-bridge. This keeps the final circuit in the "cheap" category which was one of the design parameters we're optimizing for. The complete schematic for the bridge is shown below.

Arduino Line Follower Robot with QRD1114 8

Page 9: Arduino Line Follower Robot With QRD1114

The fast recovery diodes (shown in light blue) are optional when you are using the TIP102/107 as those transistors have a diode built into them.

Tests

In order to investigate how this bridge responds when it is driving an inductive load, a slight change was made. The modification consists of adding two fast recovery silicon rectifiers. I've used a pair of 1N4936 diodes which are a bit of overkill with a 400V reverse voltage limit but they are inexpensive. Further, having a better margin for error is always good. The updated schematic

is shown to the right. You will recall these diodes are shown as "optional" in the full H-bridge circuit. The newly added diodes are installed in place of the sink transistor in the lower left of the bridge and the source transistor in the upper right area.

The two transistors are sporting light blue diode symbols to remind you that the TIP 102 and TIP 107 transistors have a built in freewheeling diode.  If you re-arrange the circuit it would like like the schematic shown on the left.

Arduino Line Follower Robot with QRD1114 9

Page 10: Arduino Line Follower Robot With QRD1114

With The test circuit I still can't reverse the motor's direction, but I can change its speed. As the h-bridge circuit is symmetric, I can use this circuit to study the behavior of the bridge when it is driving a motor with a DC signal and a PWM signal.

Now I can draw a picture of the test circuit and all the various current paths. This is shown in the schematic below. The blue dashed line represents the current flow before the PWM wave form is switched "off." The red dashed lines

represent the path of the decay current out of the inductor depending on which option you use for switching off the transistors.

Using the H-Bridge

Layout Considerations

Generally this circuit is fairly free of layout restrictions, however there are some things that you can do to make your life easier. A sample layout is shown below.

One of the things to note is that the transistors are arranged "back to back" with their tabs facing each other. In my layout I have spaced them 3/8" apart which allows me to put a piece of 1" x 3/8" x copper bar stock down the middle and with #4-40 machine bolts to secure it. A 1 - 1/2" piece of this stock weighs about 3 oz. This basically doubles to current capacity of the bridge, and if you then bolt the copper bar to a metal enclosure you can triple the capacity to a full 6 amps continuous duty. Further, the two left transistors are the "upper" source transistors and the two right transistors are the lower "sink" transistors. That means that any thermal solution will have heat being

Arduino Line Follower Robot with QRD1114 10

Page 11: Arduino Line Follower Robot With QRD1114

injected from diagonal corners which further maximizes the benefit by spreading out the heat injection. The point here is to think about whether or not you are going to put heat sinks on the transistors and lay them out accordingly.

Alternatively you can build this bridge on a piece of perfboard and just solder it together. Be sure and use at least 18 ga wire on the legs of the transistors.

Microprocessor Control

To use this h-bridge with a microprocessor, you must connect the three control lines to output pins on the microprocessor. Using  the BasicStamp II as an example, consider the following hookup diagram.

As you can see three pins from the Basic Stamp are connected to each H-bridge board. In this example they are P0, P1, and P2 to the board controlling the left motor and P4, P5, and P6 to the board controlling the right motor. One of the advantages of using three pins that are both right next to each other, and in the same group of four bits (called nybbles) is that you can use a single variable (one of OUTA, OUTB, OUTC, or OUTD) to write to four pins at once.

This is really only important on chips like the BASIC Stamp where their can be a millisecond or more between the execution of one instruction and the next. By connecting them this way you can cause both motors to start turning with a single instruction such as this assignment:

    OUTL = $33

Whereas if you did two instructions :

Arduino Line Follower Robot with QRD1114 11

Page 12: Arduino Line Follower Robot With QRD1114

    OUTA = $03    OUTB = $03

You would find that the left motor started turning first, then the right motor. So on a robot that steered with two motors the motor would make a slight turn to the right, then go straight. If you turned them off in the same sequence you would find that the robot corrected its heading back to the original heading but would not have traveled "straight" ahead. For systems that use gear motors such as the 12V Brevel motors or the Globe motors, this won't be a noticeable problem, but higher performance motors will definitely suffer.

Alternatively you could use something like my ServoGizmo project to drive one or two of these boards. The AntWeight ESC code could be easily modified to drive this bridge circuit rather than the 754410, however you could even drive two of these at the same time with some additional code. When the 754410 is not mounted on the Gizmo board you get 6 outputs from the PIC. If I have time I'll write a dual motor control with serial input so that you could connect the Gizmo to just one pin of the BASIC stamp and send it serial commands to control two motors.

The easiest way to use PWM on the motor is to start with the direction and enable bits "high" or at a logic 1 value. This turns on the high side (source) transistor and leaves the sink side transistor off. You can then send "low" pulses out the ENA* line to turn the motor on and off. This would allow you to use a single 'PWM' output, such as the one that is available on the PIC16F628, to control the PWM duty cycle in hardware while the PIC managed other aspects of controlling the motor. The most common use would be to provide encoder feedback into the PIC that would allow a simple PID algorithm to be implemented. With two bits of encoder input, three bits of motor control, and two bits for serial I/O the 16F628 would be well engaged.

Step 5 Mounting the electronics

Arduino Line Follower Robot with QRD1114 12

Page 13: Arduino Line Follower Robot With QRD1114

to mount the electronics, I screwed another small piece of pegboard to the pieces of would used to hold the front axle. I hot glued the H bridge down, and velcroed  the arduino so I could remove it easily for use in other projects.

Step 6 Setup the sensor

To get it to work with the arduino, you need to setup the QRD in a specific way. If you saw my instructable on servos, you will setup the qrd in the same way. just follow the schematic.

Step 7 Obstacle avoiding

Finally, the actual building part is finished (once you attach your sensor). Make sure your sensor is held by the its wires several cm away from the chassis of the robot. When an object was in front of the QRD, the value from the analog pin got smaller. this works out fine for our purposes because we can using this very easily in the software. copy paste the following code to your IDE, compile and upload. 

//This code is for an obstacle avoiding robot with one QRD-1114 reflectived sensor.

Arduino Line Follower Robot with QRD1114 13

Page 14: Arduino Line Follower Robot With QRD1114

//feel free to make changes to improve it or fix problems.const int Lf = 9;//forwards on leftconst int Lb = 10;//backwards on leftconst int Rf = 11;//forwards on rightconst int Rb = 12; // backwards on right

int val = 0;  //store data from sensor

void setup() {   pinMode(Lf, OUTPUT); //left motor output  pinMode(Lb, OUTPUT); //left motor output  pinMode(Rf, OUTPUT); //right motor output  pinMode(Rb, OUTPUT); //right motor output   //analog pins automatically set as input }

void loop() {   val = analogRead(0); // reading value of sensor   if(val < 100){    digitalWrite(Lf, LOW);// stop going forwards    digitalWrite(Rf, LOW);//stop going forwards    digitalWrite(Lb, HIGH);// back away fromn object    digitalWrite(Rb, HIGH);//back away from object    delay(2500);    digitalWrite(Rb, LOW);//turn away from object    delay(2500);    digitalWrite(Lb, LOW);// stop moving  }  else{    digitalWrite(Lf, HIGH);    digitalWrite(Rf, HIGH); //go forwards if no object  }}

Step 8 Wire things up to the arduino

Connect the left motor's forward transistors to pin 9, left motor backwards to pin 10, right motor's forwards to pin 11, right motor backwards pin 12. connect power from the H bridge to the 3.3v pin on the arduino, and the H bridge's ground to one of the ground pins. connect the output of the sensor to analog pin 0, the power of the QRD to the 5v pin, and the ground to one of the ground pins. We will keep these connections for all three tasks the robot will be able to perform. 

Arduino Line Follower Robot with QRD1114 14

Page 15: Arduino Line Follower Robot With QRD1114

Step 9 Make the robot into a "table top wanderer"

is very easy to do with this robot. with obstacle avoiding, the robot moves forward as long as there is nothing in front of it. for cliff avoidance it is the exact opposite. Bend the sensor down so it is facing the ground. In the code it comes down to a very simple change though. simply change the less than sign to a greater than sign, and upload it. just in case, here is the code:

//This code is for an obstacle avoiding robot with one QRD-1114 reflectived sensor.//feel free to make changes to improve it or fix problems.const int Lf = 9;//forwards on leftconst int Lb = 10;//backwards on leftconst int Rf = 11;//forwards on rightconst int Rb = 12; // backwards on right

int val = 0;  //store data from sensor

void setup() {   pinMode(Lf, OUTPUT); //left motor output  pinMode(Lb, OUTPUT); //left motor output  pinMode(Rf, OUTPUT); //right motor output  pinMode(Rb, OUTPUT); //right motor output   //analog pins automatically set as input }

void loop() {   val = analogRead(0); // reading value of sensor

Arduino Line Follower Robot with QRD1114 15

Page 16: Arduino Line Follower Robot With QRD1114

   if(val > 100){    digitalWrite(Lf, LOW);// stop going forwards    digitalWrite(Rf, LOW);//stop going forwards    digitalWrite(Lb, HIGH);// back away fromn object    digitalWrite(Rb, HIGH);//back away from object    delay(2500);    digitalWrite(Rb, LOW);//turn away from object    delay(2500);    digitalWrite(Lb, LOW);// stop moving  }  else{    digitalWrite(Lf, HIGH);    digitalWrite(Rf, HIGH); //go forwards if no object  }}

using this code, with the sensor bent downwards, you can make your robot able to move around a table on its own without falling off. 

Step 10 Make a line follower

Arduino Line Follower Robot with QRD1114 16

Page 17: Arduino Line Follower Robot With QRD1114

QRD-1114 was actually designed to be used in line following robots, so it is the perfect sensor for this. keep the sensor bent down wards facing the ground. I made a line on a dry erase board making a line with black electrical tape. use the following code:

//this is the code for line following on an arduino robot using a single//QRD 1114 infrared reflective sensor. feel free to make improvments, as//this is a very simple software.const int left = 9; //left motorconst int right = 11; //right motor//be sure to connect these pins through transistors.

int val = 0; //store value from sensor

void setup() {  pinMode(right, OUTPUT);  pinMode(left, OUTPUT);}void loop() {  val = analogRead(0);   if(val < 100){    digitalWrite(right, LOW);    digitalWrite(left, HIGH); //if the surface is white turn the left motor                              //on and the right off.  } else{    digitalWrite(left, LOW);    digitalWrite(right, HIGH); // if the surface is black turn the right motor on                               //and the left motor off.  }}

Note:  The robot due to the nature of the code, will move relatively slowly along the line, I don't think you can really make it go much better with only one sensor. The robot will every now and then freeze up while running. When this happens simply nudge the robot in either direction, and it will continue to run.

Step 11 Finished

the robot can do three main tasks, and I feel that doing them, the robot has met its full potential.

Lithium Battery Backpack for Arduino Uno

http://www.jameco.com/webapp/wcs/stores/servlet/Product_10001_10001_2127566_-1

Arduino Line Follower Robot with QRD1114 17

Page 18: Arduino Line Follower Robot With QRD1114

Hooking up and test QRD1114 line sensor

The QRD1114 line sensor (reflectance sensor) is easy to use. Personally, I can recommend it. This is a very popular infrared line/object sensor used in many robot designs. We use several of them in our own Sumovore Mini-sumo robot kit. These sensors have an effective range of approximately 6mm (1/4"), which make the ideal for line detection.

Where should we go first? Right, datasheet. We will need to dig some information out of it:

Pinout Continious forward current of emitter = 50 mA Forward voltage of emitter = 1.7 V (max)

Arduino Line Follower Robot with QRD1114 18

Page 19: Arduino Line Follower Robot With QRD1114

Last two values are necessary for calculating resistor (see end of article).The basic circuit for this sensor should look something like this:

QRD 1114 Test Schematic

Warning!!! Pins on schematic are not arranged as phisical ones! Refer to drawing and datasheet!

You will connect your power supply across V0 and V+. Out will go to ADC input of microcontroller. Depending on supply voltage and configuration of your emitters (if, say, you will use 3 line sensors, you might want to connect them in series or in parallel) you will need to recalculate/rearrange resistor which is in series with emitter IR LED.

The QRD1114 is an unfocused Infrared Reflective Photo Detector. It's used when you need to detect a surface transition without touching it. This sensor has a wider range of detection than most focused sensors making it better suited to simple 'absence/presence' tasks. Typical applications include line following and edge detection.

Features Include:

Phototransistor Output Non-contact surface sensing Unfocused for sensing diffused surfaces Small Size (4.6 x 6.1 x 4.4 mm) Daylight filter on sensor

Arduino Line Follower Robot with QRD1114 19

Page 20: Arduino Line Follower Robot With QRD1114

Using the QRD1114

Object / Surface Detection Object or surfaces must be within 0.5cm. This circuit will not distinguish between white and black objects but it will let you know when you are at the edge of a surface.

Color Differences White / Black If you want to detect the difference between white or black surfaces, in the circuit above the Input pin on the Micro should be an analog to digital converter or some other device that can utilize variable voltage levels. A black surface will give a voltage some where between 0V and 5V and white surfaces will give a voltage of 5V. Determining the voltage level for your black and white surfaces will require experimentation.

Arduino Line Follower Robot with QRD1114 20