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

Setting the calibration values manually in QTRSensor library


I am building a line following robot using the QTR-8A sensor module, with the end two sensors broken off. The remaining six sensors are connected to a teensy 3.5 on pins 12,13,0,1,6 & 7, with LEDON pin disconnected, an external 3.3v regulator powering the sensors and soldered connectors on the sensors.

The line is printed inkjet black ink on matte white paper. The sensors are mounted parallel to this, at a distance of about 3-4mm, on a custom robot chassis.

All 6 sensors work, using the QTRAraw example sketch in the library, however when I try to use the readLine function demonstrated in the QTRA example sketch the values are not deviating much from the centre line position of 2500. The raw values are clearly detecting the difference between the white and black (black is around 600, white is around 900) but no matter how slowly I move the robot across the line when calibrating, or how many cycles of calibration I perform, the line position never exceeds 2000-3000 no matter where I move the line on the paper, where I expect the full range to be 0-5000. Looking at the serial output’s first lines where it prints the calibration values is has found, the minimum values are very, very low compared to what I experienced with the raw values - anywhere from 24-300.

I believe this to be the problem, in that there is a large amount of noise in the readings as the black is detected as in-between the calibrated white and black.

My question is this - how can I set the calibration values (min for a start, both would be great) that the QTRSensor library uses in the sketch. I am happy to modify the library if necessary, but the changes to the values should be able to be changed on the fly within the sketch.

If this is not possible, what it the best way to incorporate the line position algorithm into my sketch without using the library (I would still use the raw reading function from the library, but would map and constrain the sensor values myself)?

As to my code, it is just the QTRAExample example sketch, with the pin numbers changed and various values for i<400 in the for loop.

My environment is a windows 10 computer with the official teensyduino program using the Arduino 1.8.1 IDE with teensy 3.5, 120MHz, faster, US English selected on the connected COM port.

Thanks is advance!!



Are you using the 3.3V bypass jumper on the QTR board? The difference between your black and white values (600 to 900) seems pretty narrow. If you are using the bypass jumper, could you post pictures here that show your connections, wires, and course? Also, could you post some of the raw and calibrated output here? Could you clarify whether the Teensy pin numbers you referred to are the analog pin numbers or the digital pin numbers?




Thanks for the response! I have managed to get it working. I finally realised that I can call qtra.calibratedMinimumOn[i] = x;
to set the calibration of i sensor to x. The difference has for some reason changed without my intervention, and I now get a range of 20-750 for white to black.

To clarify for others - I was not using the 3.3v bypass as far as I know (was soldered, but cut it later because it washed out the black) (might be shorted accidentally…). The course I was testing on was the Robocup junior australia rescue tile set at http://www.robocupjunior.org.au/sites/default/files/Practice_tiles_and_map.zip. The teensy pin numbers were analogue pin references. Some example raw and calibrated data once manually calibrated is (raw first, limits set to 20 and 750 each sensor)

1000 675 47 55 61 172
798 557 55 59 66 213

782 889 48 57 69 291
594 727 56 60 72 295

97 84 441 991 98 252
73 69 357 714 97 268

Thanks again for your help!