Line Follower Robot



In this tutorial, you will make line follower robot. This tutorial involves building a line follower robot with an QTR-8RC Reflectance Sensor Array.

-----------------------------------------------------------

A) Hardware Required :

- Arduino Board
- Motor Shield (Adafruit)
- QTR-8RC Reflectance Sensor Array (Pololu)
- DC Motor x2
- Wheel x2
- Bovine Wheel
- Chassis plate
- Battery 9V
- Battery Buckle
- Wires
- Button
- Glue Gun

-----------------------------------------------------------

B) Create of the Chassis:

1) The DC motors attach to the chassis
2) The Arduino attach to the chassis
3) The Motor Shield attach to the Arduino.

About the Motor Shield:
- The motor controllers on this shield are designed to run from 4.5V to 25V.
- There are two places you can get your motor 'high voltage supply' from. One is the DC jack on the Arduino board and the other is the 2-terminal block on the shield that is labeled EXT_PWR.
- To connect a motor, simply solder two wires to the terminals and then connect them to either the M1, M2, M3, or M4.

4) The QTR-8RC Reflectance Sensor attach to the chassis.

-----------------------------------------------------------

C) Connections :

1) DC Motors connect to the Motor Shield
2) Left Motors connect to M1
3) Right Motors connect to M2
4) The QTR-8RC Reflectance Sensor Connections

- The Sensor VCC connect to the Arduino +5V
- The Sensor GND connect to the Arduino Ground
- The Sensor pin 2 connect to the Arduino Analog 0
- The Sensor pin 3 connect to the Arduino Analog 1
- The Sensor pin 4 connect to the Arduino Analog 2
- The Sensor pin 5 connect to the Arduino Analog 3
- The Sensor pin 6 connect to the Arduino Analog 4

5) Battery and on / off button connection

- The (+) from the battery is connect to a leg of the button
- Connect a cable to the other legs of the button. This cable is for Motor Shield (+)

6) Attach wheels

-----------------------------------------------------------

D) Copy the Code and install the Arduino Library:

1) Open the new Arduino IDE page and Delete everything on the page
2) Get the code from my blog page and Paste empty Arduino IDE page
3) First Install the Arduino Libraries
3.1) Install the AF_Motor Arduino library

- Close the Arduino IDE
- Download the library
  https://github.com/adafruit/Adafruit-Motor-Shield-library
- Uncompress the ZIP file onto your desktop
- Place the AFMotor folder into your arduinosketchfolder/libraries folder
- Rename the uncompressed folder AFMotor

3.2) Install the QTRSensors library

- Download the library
  https://github.com/pololu/qtr-sensors-arduino/releases
- Rename the folder “qtr-sensors-arduino-xxxx” to “QTRSensors”
- Drag the “QTRSensors” folder into the “libraries” directory inside your Arduino sketchbook directory

4) After installing the library, restart the Arduino IDE
5) Now you can see the libraries you have installed
6) When you verify, you will not see any errors

Get the Arduino Code :
#include <AFMotor.h>    //Adafruit Motor Shield Library. First you must download and install AFMotor library
#include <QTRSensors.h> //Pololu QTR Sensor Library. First you must download and install QTRSensors library
  
AF_DCMotor motor1(1, MOTOR12_1KHZ ); //create motor #1 using M1 output on Motor Drive Shield, set to 1kHz PWM frequency
AF_DCMotor motor2(2, MOTOR12_1KHZ ); //create motor #2 using M2 output on Motor Drive Shield, set to 1kHz PWM frequency
  
#define KP 2 //experiment to determine this, start by something small that just makes your bot follow the line at a slow speed
#define KD 5 //experiment to determine this, slowly increase the speeds and adjust this value. ( Note: Kp < Kd) 
#define M1_minumum_speed 150  //minimum speed of the Motor1
#define M2_minumum_speed 150  //minimum speed of the Motor2
#define M1_maksimum_speed 250 //max. speed of the Motor1
#define M2_maksimum_speed 250 //max. speed of the Motor2
#define MIDDLE_SENSOR 4       //number of middle sensor used
#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         //emitterPin is the Arduino digital pin that controls whether the IR LEDs are on or off. Emitter is controlled by digital pin 2
#define DEBUG 0

//sensors 0 through 5 are connected to analog inputs 0 through 5, respectively
QTRSensorsRC qtrrc((unsigned char[]) { A4,A3,A2,A1,A0} ,NUM_SENSORS, TIMEOUT, EMITTER_PIN);
  
unsigned int sensorValues[NUM_SENSORS];
  
void setup()
{
delay(1500);
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); //get calibrated readings along with the line position, refer to the QTR Sensors Arduino Library for more details on line position.
int error = position - 2000;
  
int motorSpeed = KP * error + KD * (error - lastError);
lastError = error;
  
int leftMotorSpeed = M1_minumum_speed + motorSpeed;
int rightMotorSpeed = M2_minumum_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_maksimum_speed ) motor1speed = M1_maksimum_speed;
if (motor2speed > M2_maksimum_speed ) motor2speed = M2_maksimum_speed;
if (motor1speed < 0) motor1speed = 0; 
if (motor2speed < 0) motor2speed = 0; 
motor1.setSpeed(motor1speed); 
motor2.setSpeed(motor2speed);
motor1.run(FORWARD); 
motor2.run(FORWARD);
}

//calibrate for sometime by sliding the sensors across the line, or you may use auto-calibration instead
void manual_calibration() {
  
int i;
for (i = 0; i < 250; i++)
{
qtrrc.calibrate(QTR_EMITTERS_ON);
delay(20);
}
  
if (DEBUG) {
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();
}
}