Build An Arduino Line Following Robot – Super Simple and Cheap


Build An Arduino Line Following Robot Body

 

How to Build An Arduino Line Following Robot

Line following robots are often come in a robot kit. Although it’s easier and fun to play with, but it could also be very expensive. In this tutorial I will show you how to Build An Arduino Line Following Robot.

This is a very simple and cheap line following robot, anyone can build it. It’s built from a CD case, and the software programming is very simple too by using libraries provided by Pololu and Adafruit for the sensor and motor shield.

 

Arduino Line Following Robot Required Parts

Build An Arduino Line Following Robot - All parts

 

From top left to bottom right:

  • DVD case
  • Male headers – I break them up into smaller headers and use them to connect wires and modules
  • Pololu QTR-8RC line sensor – This model comes with 8 sensors. I removed 2. And in this robot, I only activate 5 sensors from the remaining 6.
  • Screws, nuts, and washers are used as raisers – The length of the screws will depend on your needs
  • Wire straps – to tie the motors and the battery pack to the platform
  • Drill bits – to make the holes in the DVD case
  • Pololu 30:1 micro metal motor with gearbox (6V)
  • Arduino Uno
  • Adafruit Motor Shield
  • 7.2V Battery (6 X 1.2V AA NiMh)

This robot weighs about 300gm and costs about $90 USD. You can build a cheaper and lighter version of this robot using the Atmel Atmega328 and the L293D h-bridge.

 

Make the line following robot’s base from a CD case

Build An Arduino Line Following Robot - body

 

We Draw on the CD case first and then cut it and drill the holes. This is not the best design i am sure you can do better 🙂 Make sure the holes are big enough for the wire straps. Building a line following robot is not very hard is it? 😉

Assembling Parts

Build An Arduino Line Following Robot - Body with arduino

Build An Arduino Line Following Robot - assembing

Build An Arduino Line Following Robot - all most ready

Build An Arduino Line Following Robot - connecting

The assembly is simple. It is useful to write note about which motor goes to which motor shield terminal pin and which sensor pin goes into which Arduino pin. This project should take no more than 10 hours. Just make sure you don’t get confused with the wiring.

Be careful 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.

 

The Software Programming For the Line Following Robot

//  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();
  }
}

Line Following Robot Ready!

I powered up the robot using the motor shield power pins. The 7.2V battery is good enough powering the whole robot.

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.

Building a line following robot is just that easy… 😉 Enjoy!

Share Button

Leave a Reply