PololuQTRSensor trouble

Well, I’ve fairly extensively tested the QTR library and it works fine for me. Note that the modification date of the QTR library I’m using is 8/26/08 and it comes as part of the Pololu AVR library package that you can download here.

I wrote the following two programs using the Arduino IDE and programmed them onto a mega168 that has pin 0 (PD0) connected to the sensor’s LEDON (emitter) pin and analog inputs 0 - 3 (PC0 - PC3) connected to sensor outputs 1 - 4 as labeled on the array’s PCB:

Test of raw sensor output:

#include <OrangutanLCD.h>
#include <PololuQTRSensors.h>  

#define NUMSENSORS 4
#define SAMPLES 3
#define EMITTERPIN 0

unsigned int sensors[8]; 
long  cnt = 0;
unsigned int line = 0;
OrangutanLCD lcd;

PololuQTRSensorsAnalog qtra((unsigned char[]) {0, 1, 2, 3}, NUMSENSORS, SAMPLES, EMITTERPIN);  

void setup()
{  
}

void loop() 
{
  int i;
  
  qtra.read(sensors, QTR_EMITTERS_ON);
  lcd.gotoXY(0, 0);
  for (i = 0; i < 4; i++)
  {
    lcd.print(sensors[i] / 103);
    lcd.print(' ');
  }
  
  lcd.gotoXY(0, 1);    
  qtra.read(sensors, QTR_EMITTERS_OFF);
  for (i = 0; i < 4; i++)
  {
    lcd.print(sensors[i] / 103);
    lcd.print(' ');
  }

  delay(100);                  // stop the program for some time
}

Test of calibrated sensor output:

#include <OrangutanLCD.h>
#include <PololuQTRSensors.h>  

#define NUMSENSORS 4
#define SAMPLES 3
#define EMITTERPIN 0

unsigned int sensors[8]; 
long  cnt = 0;
unsigned int line = 0;
OrangutanLCD lcd;

PololuQTRSensorsAnalog qtra((unsigned char[]) {0, 1, 2, 3}, NUMSENSORS, SAMPLES, EMITTERPIN);  

void setup()
{  
  int i;
  lcd.clear();
  lcd.print("...");
  delay(3000);
  lcd.clear();
  lcd.print("calib.");
  for (i = 0; i < 250; i++)
  {
    qtra.calibrate();
    delay(20); 
  }
}

void loop() 
{
  int i;
  
  int position = qtra.readLine(sensors, QTR_EMITTERS_ON);
  lcd.clear();
  lcd.print("pos=");
  lcd.print(position);
  lcd.gotoXY(0, 1);
  for (i = 0; i < 4; i++)
  {
    lcd.print(sensors[i] / 101);
    lcd.print(' ');
  }

  delay(200);                  // stop the program for some time
}

You should be able to make this code work on your end by replacing the lcd methods with serial output calls. I think you either still have an old version of the library or something very strange is going on, because every aspect of these programs works as expected for me.

- Ben

Hello Ben,

I started working with QTR-8RC on Arduino and experienced the same problem.
I do calibration (moving the sensor back and forth close over a white sheet with
a black line), but the values in the array sensor stay zero.
when I try using qtrrc.readLine(sensor); the same with the return value.

However, once I do qtrrc.read(sensor) there are non-zeros in this
array.

Next time you call qtrrc.readLine(sensor) you get the same values.
They change only when you call qtrrc.read(sensor).
It looks like the values are done only locally and not returned.

I have downloaded and installed the
library from Pololu in October, the file PololuQTRSensor.cpp ist of 26.Aug.08 and
the file PololuQTRSensor.h as well. Currently I have employed only
the first five sensors. Here is my full Arduino program:

–Hannes


/*
 Pololu QTR-8RC Reflectance Sensor Array testing
  Hannes Hassler Oct 2008

*/


#include <PololuQTRSensors.h>

#define ledActivatePin 2

#define  no_of_sensors 5

byte sensorPin[] = {3, 4, 5, 6, 7};



unsigned int sensor[no_of_sensors];

// create an object for six QTR-xRC sensors on digital pins 2 to 7    
PololuQTRSensorsRC qtrrc((unsigned char[]) {3, 4, 5, 6, 7}, no_of_sensors);  


void setup()  {
  // define pin modes for tx, rx, led pins:
  
  pinMode(ledActivatePin, OUTPUT);
  digitalWrite(ledActivatePin, HIGH);
  
  for (int i=0;i<no_of_sensors;i++) {
    pinMode(sensorPin[i], INPUT);
  }
  
  
  Serial.begin(9600);  
  
  calibrateSensorPlusComment(qtrrc); 
 
  
}



void calibrateSensor(PololuQTRSensorsRC qtr) {
  // during calibration phase move the sensors over both  
 // reflectance extremes they will encounter in your application:   
   for (int i = 0; i < 250; i++)  {// make the calibration take about 5 seconds     
     qtr.calibrate();  
     delay(20);  
   }  
}



void calibrateSensorPlusComment(PololuQTRSensorsRC qtr) {
  Serial.println("calibration (5 secs)");
  calibrateSensor(qtr);     
  Serial.println("calibration finished"); 
}


int readPosition(PololuQTRSensorsRC qtr) {
  return qtr.readLine(sensor); 
}

void rawRead(PololuQTRSensorsRC qtr) {
  qtr.read(sensor); 
}

void loop() {
  
  
  static int v = 0;  
  static char ch;
  
 
  if (Serial.available()) {    
    
      ch = Serial.read();
    

    switch(ch) {
      case '0'...'9':
        v = v * 10 + ch - '0';
        break;
      case 's':
       Serial.print("sensor:");
       Serial.println(v);
       v = 0;
       break;
       case 'c':       
       calibrateSensorPlusComment(qtrrc); 
       break;
       case 'L':
        digitalWrite(ledActivatePin, LOW);
       break;
       case 'H':
        digitalWrite(ledActivatePin, HIGH);
       break;
       case 'r':
       rawRead(qtrrc);        
       showSensorVals();
       break; 
       case 'l':
       //rawRead(qtrrc);
       int position = qtrrc.readLine(sensor);   
       showPos(position);      
       showSensorVals();
       break; 
       
    } 
  }  
       

}


void showPos(int position) {  
  Serial.print("position:");
  Serial.println(position);
}

void showSensorVals() {
  
  Serial.print("sensor: ");
  for (int i=0;i<no_of_sensors;i++) { 
         Serial.print("\t");    
         Serial.print(i);
         Serial.print("=");
         Serial.print(sensor[i]);
       }
   Serial.println(" ");
  
}

Hi Hannes,

I gave up on it, but if you guys get it working, I might come back and take a look :slight_smile:

Phil

Hello,
Can you try putting in explicit values for the white_line and readMode parameters to both readLine and calibrate? I am worried that somehow these parameters are getting switched in your version of the code. Hopefully we can track down this problem!

Thanks,
-Paul

I have modified my output so that you can see the code how it is called
(in Arduino environment) in the quote window below.
The call-parts I have marked with ::
After the call part you can see the values you get in the sensor array, respectively.

#define  no_of_sensors 8

unsigned int sensor[no_of_sensors];

// create an object for six QTR-xRC sensors on digital pins 3 to 10
PololuQTRSensorsRC qtrrc((unsigned char[]) {3, 4, 5, 6, 7, 8, 9, 10}, no_of_sensors);  

It is the same variable used both for the call
qtrrc.readLine(sensor); and for
qtrrc.read(sensor);

But the contents in it are quite different after these calls.
After the first call of qtrrc.readLine(sensor) you got all values
zero. After the first qtrrc.read(sensor); you get values.
After the second call of qtrrc.readLine(sensor) you get some values too,
but obviously they are just the same ones.
They change only after another call of qtrrc.read(sensor);

Finally I made a call of qtrrc.read(sensor) and put the sensor on the
black line. You notice the high value at sensor 5.

Let me add, for my purposes I am completely content
with the .read(sensor) method and do not need readLine().
I also really like this sensor-array,
because I have build my own with normal
LEDs and fototransistors. I never got so good black-white results
as with Pololu QTR-8RC

Well, if you want to debug this further, please try it with calibrate(QTR_EMITTERS_ON) and readLine(sensors, QTR_EMITTERS_ON, 0) to see if those parameters are causing you trouble. It would also be nice to know if your IR emitters go on when you are running calibrate() or readLine() repeatedly in a loop.

I’m having a similar problem to the others described in this thread.

The sensor this time is a QTR-1A. Here’s the code I’m using:

#include <PololuQTRSensors.h>

int aPin = 0;
int ledPin = 13; 
PololuQTRSensorsAnalog qtr1((unsigned char[]) {0}, 1, 3, 0);

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
}

void loop()
{
  unsigned int sensors[3];
  int val = 0;
  
  qtr1.read(sensors, QTR_EMITTERS_ON);
  val = analogRead(aPin);
  
  Serial.print("s:");
  Serial.print(sensors[0]);
  Serial.print(":");
  Serial.println(val);
  delay(500);
}

Here’s some console output:

s:1023:328
s:1023:329
s:1023:334
s:1023:328
s:1023:998
s:1023:985
s:1023:794
s:1023:468
s:1023:17
s:1023:19
s:1023:18
s:1023:17
s:1023:247
s:1023:242

The low (<20) values are the sensor over white paper, and the high readings are over a black line. This tells me that the sensor is hooked up correctly. Inspection with a cellphone camera reveals that the IR LED is on constantly, which would also make sense, given that we’re dealing with a QTR-1A.

I’m planning on using a QTR-8RC so will eventually want to move up to using readLine. I figured I’d start with these analog sensors and get them to work first.

I’m running this on an Arduino NG, if that’s relevant. I’m also using arduino-012 software. Is there a chance that the Pololu libraries are not compatible with this version? I’m downloading 011 now, and will post my results with that.

EDIT: Same behavior with arduino-011.

EDIT2: I fired up my QTR-8RC today, and was able to get the read, readCalibrated and readLine methods to work as expected. So, I can make the PololuQTRSensorsRC object work, but not the PololuQTRSensorsAnalog. Good enough for my immediate application, but now that I’ve found this weirdness, I’ve gotta track it down!

Thanks for any help or insight anyone might be able to provide!

Hello.

I’m very glad to hear that you got the RC sensors working under the Arduino environment. I don’t have an Arduino to test on, but I have used the Arduino environment to program the QTR sensor libraries onto mega168s and both the analog and RC libraries have functioned as expected. If you manage to figure out why the analog version isn’t working on your Arduino, please let us know!

- Ben

Had the same problem. Fast and easy way to get things going is to change sources of the library, replacing function PololuQTRSensorsAnalog::readPrivate with following:

void PololuQTRSensorsAnalog::readPrivate(unsigned int *sensor_values)
{
	unsigned char i, j;
	for (j = 0; j < _numSamplesPerSensor; j++)
	{
		for (i = 0; i < _numSensors; i++)
		{
			sensor_values[i] += analogRead(_analogPins[i]);
		}
	}
	
	for (i = 0; i < _numSensors; i++)
		sensor_values[i] = (sensor_values[i] + (_numSamplesPerSensor >> 1)) /
			_numSamplesPerSensor;


}

After that it will work, but only on arduino =)

Hello All,

I am new to this forum and to this area of work (sensors, robotics). I want to use the QTR8RC sensors for a personal project. I am using a PSOC Microcontroller in which pins are limited.

Anyway, I want to be able to hook up the output of the sensors to a hardware shift register so I could have 8 parallel inputs and one output to my microcontroller.

However, I am looking at the instructiions for use and I am a little confused.
The directions are:

  1. Make the I/O an ouput
  2. Delay 10us to let charge
  3. Make the I/O an input
  4. Read Values
  5. and loop this for continuous read.

However, my problem arises in that the hardware shift register has 8 parellel inputs and one output. I am not sure if I will be able to change the Output/Input on the sensors.

Am I reading this correct or will this work without problems? I was under the assumption that I could just power the sensors and create a loop to delay before reading the sensors. Is this not correct? I thought that when the sensor saw black or white would determine if the state of the pin is high/low and then this value would be read and could be shifted through the hardware shift register. Because the shift register is 8bit, it would take 8 counts to shift values through and then this cycle would loop.

Any ideas?

Thanks all and please help me work through this.

Hello,

I don’t think your approach will work. You really need your pins to first charge the capacitor and then time the discharge; that timing won’t really be doable with your setup, either. You could maybe make something happen with the analog (not RC) version of the sensor, but if you just connect to a digital input, you won’t have any say over the thresholds and you won’t get to calibrate for individual sensor variation.

- Jan

A post was split to a new topic: QTR sensor array center point