Arduino-based Line Follower Robot Using Pololu QTR-8RC Line Sensor

131,789

67

43

Published

Introduction: Arduino-based Line Follower Robot Using Pololu QTR-8RC Line Sensor

About: Did I unplug the solder iron?
UPDATE FEB 2012: This guide is featured on Adafruit's blog
http://www.adafruit.com/blog/2012/02/14/arduino-based-line-follower-robot/

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. 





PARTS LIST

ELECTRONICS
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)

MISC
DVD case
Wires
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. 

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

REFERENCES
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 MIDDLE_SENSOR 3
#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()
{
  delay(1000);
  manual_calibration(); 
  set_motors(0,0);
}

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
  motor1.run(FORWARD);  
  motor2.run(FORWARD);
}


void manual_calibration() {

  int i;
  for (i = 0; i < 250; i++)  // the calibration will take a few seconds
  {
    qtrrc.calibrate(QTR_EMITTERS_ON);
    delay(20);
  }

  if (DEBUG) { // if true, generate sensor dats via serial output
    Serial.begin(9600);
    for (int i = 0; i < NUM_SENSORS; i++)
    {
      Serial.print(qtrrc.calibratedMinimumOn[i]);
      Serial.print(' ');
    }
    Serial.println();

    for (int i = 0; i < NUM_SENSORS; i++)
    {
      Serial.print(qtrrc.calibratedMaximumOn[i]);
      Serial.print(' ');
    }
    Serial.println();
    Serial.println();
  }
}



Share

Recommendations

  • Creative Misuse Contest

    Creative Misuse Contest
  • Clocks Contest

    Clocks Contest
  • Oil Contest

    Oil Contest

43 Discussions

How did you make the wheels that are attached to the motors?

I wanna track white line on black surface...

please help me in coding

Can you show us the track please?

Can u please show me a connection procedure for L293D with QTR?

Hi, I also used TCRT-5000 Sensor instead Pololu QTR Sensor. could you advice me for the code?

Yes Yes code not working. I am disappointed.

What is love? Baby dont hurt me, on more... :))

0
user
oz9n1

2 years ago

Code not working. help me ?

Hi,
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.

2 replies

I can't find pololu sensor in my country so I just bought Arduino KY-033 Hunt sensor module. Can you show me the code if I use 2 Arduino KY-033 Hunt sensor module,please? Thanks

i have a problem regarding the middle sensor, i am using all 8 sensor pairs of pololu and made the middle sensor to 5 but it is always alligning to the 3rd sensor.....can someone help me sort out this problem

0
user
QiW1

2 years ago

Hi,

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!

i have a question about the sensor output:

how do you calculate the output of the sensors?

0*value0 + 1000*value1 + 2000*value2 + ...
--------------------------------------------
value0 + value1 + value2 + ...

is that correct?

1 reply

yes..... if you are(/were) not using pololu's library, then this is how you do it....

Does same code works for white path over black background??

is the circuit diagram connections available in the "go pro"? I already bought the components, but just in need of the connections.

Any explanation of the coding will be very much helpfull :) thanks in advance :)