Trouble with Pololu QTR sensor library

Status
Not open for further replies.

mwomack

Well-known member
I am trying to use the stock Pololu QTR Sensor library (https://github.com/pololu/qtr-sensors-arduino) with my Teensy 3.5. When it reads the IR sensor, all it returns is 0 or 1, not the range of values it is supposed to return. I wrote some simple sample code and it works fine using my Arduino Uno, but the same code only returns 0 or 1 on the Teensy. The only difference is the pin number used for the input (12 on Uno, 2 on Teensy).

Any ideas what might be causing this? Has anyone had success using the Pololu sensors on Teensy?

Sample code:

#include <QTRSensors.h>

// Edge sensors
const int NUM_EDGE_SENSORS(1);
const int EDGE_TIMEOUT(500); // waits for 500 microseconds for sensor outputs to go low

QTRSensorsRC edgeSensors((unsigned char[]) {12}, NUM_EDGE_SENSORS, EDGE_TIMEOUT, QTR_NO_EMITTER_PIN);
unsigned int edgeSensorValues[NUM_EDGE_SENSORS];

void setup() {
Serial.begin(9600);
}

void loop() {
if (millis() % 100 == 0) {
readEdgeSensors();
}
}

void readEdgeSensors(void) {
// read raw sensor values
edgeSensors.read(edgeSensorValues);

// print the sensor values as numbers from 0 to 500, where 0 means maximum reflectance and
// 500 means minimum reflectance
for (unsigned char i = 0; i < NUM_EDGE_SENSORS; i++)
{
Serial.print(edgeSensorValues);
Serial.print('\t'); // tab to format the raw data into columns in the Serial monitor
}
Serial.println();
}
 
FYI - it helps if you post code using code tags: (advanced page or initial page) you can use the # button...
So code formatted by pasting into Arduino and run the format function..
Code:
#include <QTRSensors.h>

// Edge sensors
const int NUM_EDGE_SENSORS(1);
const int EDGE_TIMEOUT(500); // waits for 500 microseconds for sensor outputs to go low

QTRSensorsRC edgeSensors((unsigned char[]) {
  12
}, NUM_EDGE_SENSORS, EDGE_TIMEOUT, QTR_NO_EMITTER_PIN);
unsigned int edgeSensorValues[NUM_EDGE_SENSORS];

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (millis() % 100 == 0) {
    readEdgeSensors();
  }
}

void readEdgeSensors(void) {
  // read raw sensor values
  edgeSensors.read(edgeSensorValues);

  // print the sensor values as numbers from 0 to 500, where 0 means maximum reflectance and
  // 500 means minimum reflectance
  for (unsigned char i = 0; i < NUM_EDGE_SENSORS; i++)
  {
    Serial.print(edgeSensorValues[i]);
    Serial.print('\t'); // tab to format the raw data into columns in the Serial monitor
  }
  Serial.println();
}
Note: I have not looked at the sensors library, but wonder... You say:
The only difference is the pin number used for the input (12 on Uno, 2 on Teensy).
And I see in the above code the Edgesensors shown with a 12 in it... Wonder if that needed to be changed to a 2?
 
Thanks, Kurt. But I did have it set to 2 when trying to run the same code on the Teensy 3.5.

I looked at the Pololu library briefly, but did not see anything obvious at first glace. It looked like typical Arduino code around reading the pin.

-Mark
 
I took a quick look on github and did not see anything totally obvious.

There are things like: the usage of int and unsigned int, where on the Arduino UNO int are 16 bits and on Teensy they are 32 bits. Again it may not be the issue, if the sizes are consistent between your code and the library, but that is one thing I would probably look at.

Edit: when you say pin 2, I believe that these are Analog pins. So I assume you are connecting up to A2 (digital pin 16?)
 
Last edited:
KurtE,

No, I am pretty sure it is supposed to be digital pin 2, not analog pin A2.

I did some more digging, and there was a post on the Pololu forum from 2015 that talked about the same issue. In the readPrivate method in the file QTRSensors.cpp, around line 433, the existing code:
Code:
        sensor_values[i] = _maxValue;
        digitalWrite(_pins[i], HIGH);   // make sensor line an output
        pinMode(_pins[i], OUTPUT);      // drive sensor line high
The digitalWrite and pinMode lines need to be swapped. After that, everything works on the Teensy. And going back to the Uno, it still works there as well.

Yeah!

-Mark
 
Thank you Mark!

I had the same issue, and now it's working. :)


KurtE,

No, I am pretty sure it is supposed to be digital pin 2, not analog pin A2.

I did some more digging, and there was a post on the Pololu forum from 2015 that talked about the same issue. In the readPrivate method in the file QTRSensors.cpp, around line 433, the existing code:
Code:
        sensor_values[i] = _maxValue;
        digitalWrite(_pins[i], HIGH);   // make sensor line an output
        pinMode(_pins[i], OUTPUT);      // drive sensor line high
The digitalWrite and pinMode lines need to be swapped. After that, everything works on the Teensy. And going back to the Uno, it still works there as well.

Yeah!

-Mark
 
Status
Not open for further replies.
Back
Top