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

Problem to read position with Qtr8A


I try to use Arduino Library for the Pololu QTR Reflectance Sensors. But i could not understand something very well.
Calibrate function is working. OK… i can see minimum and maximum values in serial monitor. when i read position information,it is not like the pdf document. in the pdf document say that
readLine function returns an estimated position of the line. The estimate is made using a weighted average of the sensor indices multiplied by 1000, so that a return value of 0 indicates that the line is directly below sensor 0 (or was last seen by sensor 0 before being lost), a return value of 1000 indicates that the line is directly below sensor 1, 2000 indicates that it’s below sensor 2, etc.

I am using 6 sensors and i read the position like my attachment. how can it possible like this?

Sorry my english, i hope that you can understand my problem?

[code]#include <QTRSensors.h>

#define NUM_SENSORS 6 // number of sensors used
#define NUM_SAMPLES_PER_SENSOR 4 // average 4 analog samples per sensor reading
#define EMITTER_PIN QTR_NO_EMITTER_PIN // emitter is controlled by digital pin 2

// sensors 0 through 5 are connected to analog inputs 0 through 5, respectively
QTRSensorsAnalog qtra((unsigned char[]) {0, 1, 2, 3, 4, 5},NUM_SENSORS, NUM_SAMPLES_PER_SENSOR, EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];

void setup()
pinMode(13, OUTPUT);
digitalWrite(13, HIGH); // turn on Arduino’s LED to indicate we are in calibration mode
for (int i = 0; i < 400; i++) // make the calibration take about 10 seconds
qtra.calibrate(); // reads all sensors 10 times at 2.5 ms per six sensors (i.e. ~25 ms per call)
digitalWrite(13, LOW); // turn off Arduino’s LED to indicate we are through with calibration


void loop()

unsigned int position = qtra.readLine(sensorValues);

for (unsigned char i = 0; i < NUM_SENSORS; i++)





From the output you posted, it looks like you might be using the sensor with an inverted setup (e.g. a white line on a black surface). If that is the case, can you try setting the “whiteLine” argument of the readLine() function to 1 (e.g. unsigned int position = qtra.readLine(sensorValues, 1);) and see if that gives you the results you expect? You can find more detailed information about the readLine() function in the QTRSensors Methods & Usage Notes section of the Arduino Library for the Pololu QTR Reflectance Sensors documentation.

If that does not work, can you describe your calibration process and what you are doing for each of those readings? Also, can you describe your setup in more detail?



Thank you for answer. Yes you are right. When I changed the white line=true, I saw the output between 0 and 5000. But now I have another question. If there are both white line on the black surface and black line on the White surface, how can I change this variable in the library?? Is it possible??


It should be possible to switch between those two cases by just changing the second argument (e.g. using qtra.readLine(sensorValues, 1); when the line is white and qtra.readLine(sensorValues, 0); when the line is black), but you would need to figure out some way to determine what part of the course you are on.


Using QTR-8RC sensor for black and white line following

Hi Brandon again,
i wrote a function to find the surface.My logic is like that.

Black surface:2 sensors on the white line
1000 1000 0 0 1000 1000


white surface:2 sensors on the black line
0 0 1000 1000 0 0


this is my function to check surface…

[code]int checksurface()
long total;
int average;
total = 0;
average = 0;
for (int i = 0; i < NUM_SENSORS; i++)
total += sensorValues[i];
averge = total / NUM_SENSORS;

if (average< 333)//this is white surface
return 0;
else//black surface
return 1;

by the way in the loop I fixed my code like this.

void loop() { . surface=checksurface(); . . unsigned int position = qtra.readLine(sensorValues,surface); . }

when i am on the black surface, my variable surface is 1, when i am on the white surface, my variable is 0. i checked that my function is working.
but position is not true when i was on the white surface. on the black surface everything is ok.

where can be the problem? according to me it is true but when i looked from the serial monitor, my problem continues…Thanks…


It looks like I overlooked the second argument for the readLine() function in my previous post. The second parameter should be the “readMode” (e.g. if the QTR emitters are on or off), and “whiteLine” is the third parameter. You most likely will want to keep the QTR emitters on for all of your readings, so you can just pass a 1 for that. I set up a quick test here and used your code with the readLine() arguments modified to unsigned int position = qtra.readLine(sensorValues, 1, surface), and that seemed to work fine.

By the way, in order to get the black and white surface to detect more reliably, I changed the threshold to 400 (e.g. if(average< 400)), and that worked better for me, but your results could be different based on your setup.