3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

My Arduino Line Following Robot !!! - With Pololu QTR-6A IR Reflectance Sensor Array

video My Arduino Line Following Robot !!! - With Pololu QTR-6A IR Reflectance Sensor Array
I used an Arduino Duemillanove with the ATMega 328. Propulsion is provided by the two Parallax Futaba Continuous Rotation Servos. My sensor is the Pololu QTR-6A IR Reflectance Sensor Array, and it is all powered off 4 rechargeable NiMH Duracell AA Batteries :)

It can follow a dark like, on a light background. In this case i used black tape on a whiteboard. It first calibrates itself for 5 seconds. You move it across the line a few times so it gets used to the difference in reflectance. After the calibration it begins moving foward. I used an algorithm to determine its error off the line. If it determines through the algorithm that it is at an extreme error, it will turn for a longer amount of time. Similarly, if the robot determines it is only a fraction of an inch off the line, it will only turn for a fraction of a second. This reduces over compensation and makes the line following a little smoother and more reliable.

This is the code I used, I started it from scratch and added the MegaServo Library. I'm aware that there is a library for the Pololu IR sensor arrays, but I encountered problems so i decided to start from scratch with the sensor reading as well. I'm using the Analog version of the Pololu sensor array, as opposed to the RC version, which outputs a digital signal. My sensors output an Analog voltage based on the reflectance of the surface. For example, if you are providing 5V to the sensors at Vcc, and you encounter a dark surface, that sensor will output a a voltage closer to 5V. Conversely, if the sensor encounters a very reflective, (white surface) it will output closer to 0V. I can read these 6 Analog outputs from my 6 sensors through the 6 Analog I/O pins on my Arduino.
In addition, my STOP algorithm uses nested if statements to check 3 times if the robot is really at the end, before it stops for 10 seconds, blinking the light. This prevents an accidental stop in the middle of the track due to inaccurate readings or glitches. During calibration, I calculated an average value of reflectance which i use later on to help with the navigation and decision making. I also printed some data to the Serial screen for testing purposes.

Here's my code:

// ==========================================================
// Feel free to change and use this code, but please give me credit.
// Author: Austin Duff - June 24, 2009
// ==========================================================
#include <PololuQTRSensors.h>
#include <Servo.h>
#include <MegaServo.h>
#define NBR_SERVOS 3
#define FIRST_SERVO_PIN 2

Servo left;
Servo right;
Servo tower;

MegaServo Servos[NBR_SERVOS] ;

int pingPin = 7;
int mid = 0;
int mn = 0;
int mx = 0;

void setup()
{

Servos[0].attach(2, 800, 2200); //tower
Servos[1].attach(9, 800, 2200); //left
Servos[2].attach(10, 800, 2200); //right
Serial.begin(9600);
Servos[0].write(65);
digitalWrite(13, LOW);

Servos[2].write(90);
Servos[1].write(90);

for(int i=0; i<5000; i++)
{
digitalWrite(13, HIGH);

int val = 0;
for(int j=0; j<=5; j++)
{
val = analogRead(j);
if(val >= mx)
mx = val;
if(val <= mn)
mn = val;
}
delay(1);
}

mid = ((mx + mn)/2);
digitalWrite(13, LOW);

Servos[2].write(90);
Servos[1].write(90);
}
void loop()
{

int s0 = 0;
int s1 = 0;
int s2 = 0;
int s3 = 0;
int s4 = 0;
int s5 = 0;

s0 = analogRead(0);
s1 = analogRead(1);
s2 = analogRead(2);
s3 = analogRead(3);
s4 = analogRead(4);
s5 = analogRead(5);

Serial.print("Mid: ");
Serial.print(mid);
Serial.print(" ");
Serial.print(s0);
Serial.print(" ");
Serial.print(s1);
Serial.print(" ");
Serial.print(s2);
Serial.print(" ");
Serial.print(s3);
Serial.print(" ");
Serial.print(s4);
Serial.print(" ");
Serial.print(s5);
Serial.print(" ");
Serial.println();

Servos[2].write(180);
Servos[1].write(0);

delay(10);

if((((s0+s1+s2)/3)>(((s3+s4+s5)/3)+250))&&(!((s0 > mid)&&(s5 > mid))))
{
Servos[2].write(180);
Servos[1].write(90);
Serial.print(" RIGHT");
delay(abs((((s5+s4+s3)/3)-((s0+s1+s2)/3))/2));
}
if((((s0+s1+s2)/3)<(((s3+s4+s5)/3)-250))&&(!((s0 > mid)&&(s5 > mid))))
{
Servos[2].write(90);
Servos[1].write(0);
Serial.print(" LEFT");
delay(abs((((s5+s4+s3)/3)-((s0+s1+s2)/3))/2));
}
if((s0 > mid)&&(s5 > mid))
{
Servos[2].write(90);
Servos[1].write(90);
Serial.print(" STOP");
if((s0 > mid)&&(s5 > mid))
{
Servos[2].write(90);
Servos[1].write(90);
Serial.print(" STOP");
if((s0 > mid)&&(s5 > mid))
{
Servos[2].write(90);
Servos[1].write(90);
Serial.print(" STOP");
for(int k=0; k<50; k++)
{
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
delay(5000);
}
}
}

}
10 comments
Feb 21, 2012. 8:19 PMHiggs Boson says:
I noticed the ultrasonic ping sensor on the robot. What exactly is the purpose of adding it to this?
Apr 5, 2012. 10:44 PMkumaran201 says:
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.
Dec 30, 2011. 9:11 PMhardikana says:
can you code me for GRID solving robot??
please help me.. i'll give you credits sureeeeee
Nov 3, 2011. 1:09 PMkfulmer2 says:
I have a Arduino Duemillanove. My problem is I don't really know where to start. I need to make it follow a black line with one or two photoresistors. I am a beginner and programming is not my strong suit, If anyone sees this and thinks they can help my name is Kristen and my email is piratesoc23@yahoo.com. I will be able to fill you in more on the things I need the arduino to be able to do.
Oct 20, 2011. 7:17 PMconnoboarder says:
I was wondering about how you wired it all up...im a beginner and i just got an arduino...Thanks
Sep 12, 2011. 3:45 AMcdru says:
sir, can you please upload the circuit diagram about how to make it with the components required in detail... it would be of great use to me.....
i have doubts about how to create the micro controller and all.....
my mail id: electrochan@gmail.com
thank u!!!!
Sep 27, 2010. 2:09 AMpranjal.gupta says:
i have used atmega 16
sensors IR led
two dc motors
so i need code for it n wants to use pwm help me for that..
Mar 4, 2010. 2:40 AMBinalKamani says:
 I have used  ATMega 16 for Black Line Follower ..So i need code for it..if anyone can help as soon as possible.. 

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
7
Followers
4
Author:DuFFxP93