UPDATE FEB 2012: This guide is featured on Adafruit's blog

This is Faz3a II, my first line-following robot, which I also hope to use for maze-solving. I used the Arduino Uno, Adafruit motor shield, Pololu's QTR-8RC line sensors and motors. You can build a cheaper and lighter version of this robot using the Atmel Atmega328 and the L293D h-bridge. This robot weighs about 300gm and costs about $90 USD.

For my previous robot projects, I used an empty external hard disk enclosure as the robot platform. But for this robot, I am using a DVD case. All in all, I found the round shape of the DVD case a better choice for maneuverability.  Not to mention the low cost of DVD cases and ease of stacking layers to hold more parts, with the help of long screws and nuts.

For this project, my task was simplified by the availability of software libraries from Pololu and Adafruit for the sensor and motor shield respectively.

Of course I could have bought a robot kit but I want to be able to take my robot apart anytime to build another one or use the parts in a different project. So if your passion is robotics, consider getting a ready robot kit. 


Arduino Uno
Adafruit motor shield
Pololu QTR-8RC line sensor. I used only 5 sensors.
Pololu 30:1 micro meta gearmotor X 2
7.2V (6 X AA NiMh batteries)

DVD case
Wire straps
3 long screws and about a dozen nuts to act as raisers. The length of the screws depends on the height of the battery brick. One thing to keep in mind when choosing the screws is to get the ones with smooth curved heads and not the flat ones. I use the screw heads as casters. 

Arduino IDE 1.0
Adafruit motor shield library
Pololu QTR-8RC Arduino library

I wrote a guide no how to use and test the Pololu QTR-8RC line sensor.


Step 1: Making the robot's plaform from a DVD case

I used a DVD case for the platform. I marked this DVD case with an erasable pen first. It's a rough sketch made with a small ruler. I know this design won't be winning any engineering awards, but it works. Then I used a plain cutter and drill bits to carve out the design on the DVD into the final platform ready for attaching parts. Make sure you drill holes big enough for the wire straps.

Step 2: Assembling the robot's components

The assembly process is straight forward. I found it very useful to keep notes tracking which motor goes to which motor shield terminal pin and which sensor pin goes into which Arduino pin. Without this, it's easy to make mistakes that consume long debugging hours.

If you have everything in order, this project should take max 10 hours at a very leisurely pace. The first time I built it, I ran into all sorts of issues because I did not keep track of my wires and because I did not have all the necessary connectors at hand so I did plenty of soldering to connect the sensors to the Arduino.  But once I overcame all the wiring hurdles, rebuilding the robot was a matter of 3-4 hour. 

The coding and debugging took another few hours the first time around.  The second time I rebuilt this robot, it was simply a matter of load and run.

Be mindful of your robot's left and right motors. Label the motors and the wires as well as the terminals they are connected to and that should spare you needless debugging time. One telltale sign of reversed motors or reversed variable signs is if your robot spins towards the wrong direction constantly.

I powered the robot via the Adafruit motor shield external power pins. I did not need to power the motors separately from the Arduino. The 7.2V power brick did a fine job of powering the whole robot. I had no resets or erratic performance.

As for front and back casters, the soft semi-spherical tip of the screws was good enough to allow smooth movement. No special casters were used. I am sure the screws created extra friction but it did not degrade performance by much. I could have used an LED for caster just as well. 

Step 3: The Arduino 1.0 code

//  Remixed by Hazim Bitar/Techbitar
//  Date:Feb 12, 2012
//  Based on sample code provided by Pololu.com
//  Contact: techbitar at gmail dot com

#include <PololuQTRSensors.h>
#include <AFMotor.h>

AF_DCMotor motor1(1, MOTOR12_8KHZ ); // PIN 11 - create motor #1 pwm
AF_DCMotor motor2(2, MOTOR12_8KHZ ); // PIN 3 - create motor #2 pwm

// Change the values below to suit your robot's motors, weight, wheel type, etc.
#define KP .2
#define KD 5
#define M1_DEFAULT_SPEED 50
#define M2_DEFAULT_SPEED 50
#define M1_MAX_SPEED 70
#define M2_MAX_SPEED 70
#define NUM_SENSORS  5      // number of sensors used
#define TIMEOUT       2500  // waits for 2500 us for sensor outputs to go low
#define EMITTER_PIN   2     // emitter is controlled by digital pin 2
#define DEBUG 0 // set to 1 if serial debug output needed

PololuQTRSensorsRC qtrrc((unsigned char[]) {  18,17,16,15,14} ,NUM_SENSORS, TIMEOUT, EMITTER_PIN);

unsigned int sensorValues[NUM_SENSORS];

void setup()

int lastError = 0;
int  last_proportional = 0;
int integral = 0;

void loop()
  unsigned int sensors[5];
  int position = qtrrc.readLine(sensors);
  int error = position - 2000;

  int motorSpeed = KP * error + KD * (error - lastError);
  lastError = error;

  int leftMotorSpeed = M1_DEFAULT_SPEED + motorSpeed;
  int rightMotorSpeed = M2_DEFAULT_SPEED - motorSpeed;

  // set motor speeds using the two motor speed variables above
  set_motors(leftMotorSpeed, rightMotorSpeed);

void set_motors(int motor1speed, int motor2speed)
  if (motor1speed > M1_MAX_SPEED ) motor1speed = M1_MAX_SPEED; // limit top speed
  if (motor2speed > M2_MAX_SPEED ) motor2speed = M2_MAX_SPEED; // limit top speed
  if (motor1speed < 0) motor1speed = 0; // keep motor above 0
  if (motor2speed < 0) motor2speed = 0; // keep motor speed above 0
  motor1.setSpeed(motor1speed);     // set motor speed
  motor2.setSpeed(motor2speed);     // set motor speed

void manual_calibration() {

  int i;
  for (i = 0; i < 250; i++)  // the calibration will take a few seconds

  if (DEBUG) { // if true, generate sensor dats via serial output
    for (int i = 0; i < NUM_SENSORS; i++)
      Serial.print(' ');

    for (int i = 0; i < NUM_SENSORS; i++)
      Serial.print(' ');

<p>The line tracking algorythem was a huge help in getting my car to work properly! Tnx</p>
<p>Hi,</p><p>I am currently making a line following car using Intel Edison with Arduino compatible board. I chose Pololu QTR-8RC sensor array as well. However, the sensor array cannot work with my board. Could you tell me how you tested the sensor? I can't find the guide you wrote in the reference. Thank you very much!</p>
i have a question about the sensor output: <br> <br>how do you calculate the output of the sensors? <br> <br>0*value0 + 1000*value1 + 2000*value2 + ... <br>-------------------------------------------- <br> value0 + value1 + value2 + ... <br> <br>is that correct?
<p>yes..... if you are(/were) not using pololu's library, then this is how you do it.... </p>
Does same code works for white path over black background??
<p>is the circuit diagram connections available in the &quot;go pro&quot;? I already bought the components, but just in need of the connections.</p>
<p>Any explanation of the coding will be very much helpfull :) thanks in advance :)</p>
<p>could u plz give the circuit diagram........plz......i cant understnd the connection.......thnq.....</p>
<p>could u plz give the circuit diagram........plz......i cant understnd the connection.......thnq.....</p>
<p>what is the value 2000 in line :- int error = position - 2000;</p>
hey <br>i also wannna do a similar stuff. i want to design a robot for surveillance where the robot will be fully autonomous and it wil move for point A to point B autonomously. <br>i want u to help me in designing the autonomous motion planning system to go form one given place to the other using sensors n stuff...i m stuck pls help <br> <br>i have bought the rover system from here just for making prototype of the actual system since i got it at a good price... <br>http://danuc.com/index.php?main_page=index&amp;cPath=6 <br>http://danuc.com/index.php?main_page=index&amp;cPath=4 <br>
Can you add a pinout to where to hook everything up?
Hello Mr. techbitar <br> <br>could you please help me to interface QTR-8RC sensor with an arduino Atmega 2560 <br> <br>In fact I downloaded the library of QTRSensors from the website of Pololu and it works, but the sensor does not work or even no LED is ON. <br> <br>I have connected the 8 pins of the sensors to the digital pins in arduino and i connect the Vcc and Gnd to a battery of 5 V. Also, I have connected the LEDON pin in the sensor to a digital pin in arduino.
Hi do you know if the qtr-8rc reflectance sensor array can be used in PIC microcontroller? It's very important, contact me
Could you still post a pin layout. Im having issues finding it in the code and the pictures dont really help. thanks
Did you used a voltage regulator? I used a 9.6V battery pack and when I put my 2 motors on the adafruit motor shield and The Pololu QTR-8RC sensor to each digital pin and the vcc with the 5 v on my MS, the power is removed.
hi can you add detailed instructions of the connections thanks <br>i really like that project sorry for my english i am from mexico <br>and i already have/bought the piezes can you give me detailled instrcucions of how to construct thanks
My English is not so good but I do my best! <br> <br>I find you tutorial very good but I have a question <br> <br>How do you connect your battery pack with your motor shield, because I can't see it on your pictures?
How do you wide the motors and line sensor?
If you review my code you will see which pins are doing what.
What exactly assigns or in what units are<br>#define KP .2<br>#define KD 5<br>Got a little lost right there O: BTW excellent robot! Very fast
These are constants you can experiment with for best PID results. There are plenty of excellent tutorials out there to explain PID controllers. See link below to a site had in my fav list but there are plenty of PID guides that can explain with varying degrees of detail. It's not necessary to understand the theory behind PID, just how to apply it for optimum results. For my robot, the K values are typically tweaked via experimentation. <br><br>http://www.chibots.org/index.php?q=node/339
Thank you very much, that link was of great assistance for my project.
super :)
Hi, <br> I have arduino UNO, Pololu QTR 8 RC sensor array, MOTOR DRIVER 1A dual tb6612fng, two geared motor can u help me with procedure and coding for line follower.
You will find plenty of useful info in this guide.
Nice work! But I don't see how you kept the Arduino from resting with only 7.2V supplied? O.o
Thanks. <br><br>As for resetting concerns, let's analyze the robot:<br><br>-- Arduino Uno: 5V @ 500 mA (max)<br><br>-- Motors: 6V @ 40 mA free run / 360 mA stall X 2 = 720 mA (max)<br><br>500 + 720 = 1220 mA<br><br>I don't know how much the motor shield draws but when you consider I am using 6 X 2500 mAh NiMH 1.2 V AA batteries, that leaves me with 1280 mA. This is assuming max power draw from Arduino and the motors. There's plenty of power in the battery brick to power this robot. <br><br>
The Uno really wants 9V, it's in the specs. The Uno's AVR chip wants 5V but the Uno's board has a regulator on it that expects to see about 9V (but can handle as much as 20V). I have built robots sending the Uno less then 9V but I normally run into resetting issues, like when the batteries start running down or the motors stall. If you aren't hitting any issues that's cool but you might be very close to the limit such that if you add a senor you could run into issues. Also, you could run the batteries longer if you had more headroom. Thanks for the feed back! :D
http://arduino.cc/en/Main/ArduinoBoardUno<br><br>Arduino Uno Summary<br>Microcontroller ATmega328<br>Operating Voltage 5V<br>Input Voltage (recommended) 7-12V<br>Input Voltage (limits) 6-20V<br><br>
Yeap, 7-12V recommended, your 7.2V is at the low side assuming you are using the Vin pin and not the 5V pin. I assume you have some big caps on your lines to stop any spikes.
you will find the this links instructive as to the specs of the motor shield and the different ways it gets its power. hope this helps<br><br>http://www.ladyada.net/make/mshield/parts.html
Also, most of my uses for the robot is demos/races lasting a few minutes so that's why I don't run into reset issues when using a fully charged power brick. But I am sure if I need to use the robot for extended periods, I will use a voltage booster to be on the safe side.

About This Instructable


54 favorites


Bio: Did I unplug the solder iron?
More by techbitar: IR Remote Control Station for Android - TURN THE TV DOWN SensoDuino: Turn Your Android Phone into a Wireless Sensors Hub for Arduino Modify The HC-05 Bluetooth Module Defaults Using AT Commands
Add instructable to: