QTR 8RC Example sketch not working


I have the QTR 8RC sensor array and I am able to get raw values but am unable to get values from the readLine function. Also, post calibration the min and max calibrated values are 2500. I am using 5 sensors and have connected the sensors from 3 till 7 pins + VCC and GND.

If I use the read function it works i.e. I am able to get some variation in the values read.

The following is the example sketch that I am using.

#include <QTRSensors.h>

// This example is designed for use with eight QTR-1RC sensors or the eight sensors of a
// QTR-8RC module.  These reflectance sensors should be connected to digital inputs 3 to 10.
// The emitter control pin can optionally be connected to digital pin 2, or you can leave
// it disconnected and change the EMITTER_PIN #define below from 2 to QTR_NO_EMITTER_PIN.

// The setup phase of this example calibrates the sensor for ten seconds and turns on
// the pin 13 LED while calibration is going on.  During this phase, you should expose each
// reflectance sensor ot the lightest and darkest readings they will encounter.  For
// example, if you are making a line follower, you should slide the sensors across the
// line during the calibration phase so that each sensor can get a reading of how dark the
// line is and how light the ground is.  Improper calibration will result in poor readings.
// If you want to skip the calibration phase, you can get the raw sensor readings
// (pulse times from 0 to 2500 us) by calling qtra.read(sensorValues) instead of
// qtra.readLine(sensorValues).

// The main loop of the example reads the calibrated sensor values and uses them to
// estimate the position of a line.  You can test this by taping a piece of 3/4" black
// electrical tape to a piece of white paper and sliding the sensor across it.  It
// prints the sensor values to the serial monitor as numbers from 0 (maximum reflectance) 
// to 9 (minimum reflectance) followed by the estimated location of the line as a number
// from 0 to 5000.  1000 means the line is directly under sensor 1, 2000 means directly
// under sensor 2, etc.  0 means the line is directly under sensor 0 or was last seen by
// sensor 0 before being lost.  5000 means the line is directly under sensor 5 or was
// last seen by sensor 5 before being lost.

#define NUM_SENSORS   5     // number of sensors used
#define TIMEOUT       2500  // waits for 2500 us for sensor outputs to go low
#define EMITTER_PIN   QTR_NO_EMITTER_PIN     // emitter is controlled by digital pin 2

// sensors 0 through 7 are connected to digital pins 3 through 10, respectively
QTRSensorsRC qtrrc((unsigned char[]) {3, 4, 5, 6, 7, 8, 9, 10},
unsigned int sensorValues[NUM_SENSORS];

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

  // print the calibration minimum values measured when emitters were on
  for (i = 0; i < NUM_SENSORS; i++)
    Serial.print(' ');
  // print the calibration maximum values measured when emitters were on
  for (i = 0; i < NUM_SENSORS; i++)
    Serial.print(' ');

void loop()
  // read calibrated sensor values and obtain a measure of the line position
  // from 0 to 5000, where 0 means directly under sensor 0 or the line was lost
  // past sensor 0, 1000 means directly under sensor 1, 200 means directly under sensor 2, etc.
  // Note: the values returned will be incorrect if the sensors have not been properly
  // calibrated during the calibration phase.  To get raw sensor values, call:
  //  qtra.read(sensorValues);
  unsigned int position = qtrrc.readLine(sensorValues);

  // print the sensor values as numbers from 0 to 9, where 0 means maximum reflectance and
  // 9 means minimum reflectance, followed by the line position
  unsigned char i;
  for (i = 0; i < NUM_SENSORS; i++)
    Serial.print(sensorValues[i] * 10 / 1001);
    Serial.print(' ');
  Serial.print("    ");

Try to remove 8,9,10 from this line PololuQTRSensorsRC qtrrc((unsigned char[]) {3, 4, 5, 6, 7, 8, 9, 10}. How do you calibrate them holding in your hand and moving across a piece of paper?

I basically have mounted it on a bread board and i move a piece of paper on top of it while it is calibrating.


Can you post a picture of how you have it mounted? Also, can you print out the calibration values when the calibration routine is done? Basically, if your raw sensor values look okay and your calibrated ones don’t, there is something wrong with how you are calibrating the sensor.

- Ben

I had the same problem. I tried to calibrate the sensors in the same way unfortunately with no succes. I have a suggestion for you: tape a piece of 3/4" black electrical tape to a piece of white paper and sliding the sensor across it so that all of your sensors can see the white and the black surface. Try to build something that can hold the sensors at 4-5mm to that piece of paper (modifying the distance will affect the calibrating process). Succes!

Thanks for the suggestion. One thing to keep in mind is that the point of the calibration process is to expose your sensors to the range of readings you expect to encounter while your bot is running. If you expose them to values outside that range or to only a subset of the expected range during the calibration process, you will likely not get good results when using calibrated readings while actually running on the course.

- Ben

Very good point! I hope he will succeed in calibrating his sensors.