Pololu Robotics & Electronics
My account Comments or questions? About Pololu Contact Ordering information Distributors

Interfacing QTR-8A with Arduino


ive spent a good portion of my day mounting and interfacing one of my robots with a recently purchased QTR-8A wich i have reduced to the six sensor version ( arduino only has 6 analog inputs any ways ) and after messing with it for a while ive come across a problem that is driving me nuts. When i run the example code i found in the Arduino Library Reference i can not get any value out of the readline statement. i have it dumping the value of “position” to my Serial port and all i get is 0. i do have the emitters turned on and if i read from each sensor independantly (with out the library… just doing an AnalogRead ) i get correct values ( from 33 to about 800 ) . but when i use the library and the readline statement i get nothing… this is esential for my robot to work… i dont know what im doing wrong… maybe Some one here can help me… ( and yes i am sliding the line under the sensors during the calibration mode )


[code]#include <PololuQTRSensors.h>

PololuQTRSensorsAnalog qtr((unsigned char[]) {0, 1, 2, 3, 4, 5}, 6);
void setup()
digitalWrite(2, HIGH);
// optional: wait for some input from the user, such as a button press
// then start calibration phase and move the sensors over both
// reflectance extremes they will encounter in your application:
int i;
for (i = 0; i < 250; i++) // make the calibration take about 5 seconds
// optional: signal that the calibration phase is now over and wait for further
// input from the user, such as a button press
Serial.println(“done calibrating”);

void loop()
unsigned int sensors[6];
// get calibrated sensor values returned in the sensors array, along with the line position

int position = qtr.readLine(sensors);
Serial.println(position, DEC);

// if all three sensors see very low reflectance, take some appropriate action for this situation
if (sensors[0] > 750 && sensors[1] > 750 && sensors[2] > 750)
// do something. Maybe this means we’re at the edge of a course or about to fall off a table,
// in which case, we might want to stop moving, back up, and turn around.
// compute our “error” from the line position. We will make it so that the error is zero when
// the middle sensor is over the line, because this is our goal. Error will range from
// -1000 to +1000. If we have sensor 0 on the left and sensor 2 on the right, a reading of -1000
// means that we see the line on the left and a reading of +1000 means we see the line on
// the right.
// int error = position - 1000;

// int leftMotorSpeed = 100;
// int rightMotorSpeed = 100;
// if (error < -500) // the line is on the left
// leftMotorSpeed = 0; // turn left
//if (error > 500) // the line is on the right
//rightMotorSpeed = 0; // turn right
// set motor speeds using the two motor speed variables above



I’ve heard of people having trouble with the QTR libraries for the Arduino, but we haven’t had time to look into it yet. Though we plan on updating our entire Pololu AVR library for use with the Arduino environment soon. If you want to get the library working now, it looks like you can make a simple change to the library code. Check out this post for more information:

- Ben


Thank you so much… i thought i was losing my mind… … and i thought i was going to get some sleep tonight… but not now… the work must go own… robots dont build themselves… well… not yet…