Arduino Library for QTR Reflectance Sensors
Table Of Contents
2. Library Installation
Note: If you currently have an older version of our QTR reflectance sensor libraries, your first step should be
to delete the PololuQTRSensors directory from your arduino-0011/hardware/libraries directory. If you
don’t perform this step, the newer version of the libraries might not get compiled.
Additionally, the PololuQTRSensorsRC object uses Timer2 for sensor pulse timing, so
PololuQTRSensorsRC might conflict with other non-Pololu libraries that use or rely upon Timer2.
During a sensor read, Timer2 is reconfigured for pulse timing, and then it is restored to its previous state once
the read is complete, so you can use Timer2 any way you want while not performing an RC read. Note that the
PololuQTRSensorsAnalog object does not use Timer2 at all, and this entire library is compatible with all
other Pololu libraries, including the Orangutan Arduino libraries.
Download the following archive: PololuArduinoLibraries_080826.zip [http://www.pololu.com/file/download/
PololuArduinoLibraries-080826.zip?file_id=0J127] (80k zip)
and extract it to your arduino-0011/hardware/libraries directory. Once this is complete you should see
PololuQTRSensors as a subdirectory in the libraries directory.
You should now be able to use these libraries in your sketches by selecting Sketch > Import Library >
PololuQTRSensors from your Arduino 0011 IDE (or simply type #include <PololuQTRSensors.h> at the top of your
sketch). Note that you might need to restart your Arduino 0011 IDE before it sees the new libraries.
Once this is done, you can create a PololuQTRSensorsAnalog object for your QTR-xA sensors and a
PololuQTRSensorsRC object for your QTR-xRC sensors:
// create an object for three QTR-xA sensors on analog inputs 0, 2, and 6
PololuQTRSensorsAnalog qtra((unsigned char[]) {0, 2, 6}, 3);
// create an object for four QTR-xRC sensors on digital pins 0 and 9, and on analog
// inputs 1 and 3 (which are being used as digital inputs 15 and 17 in this case)
PololuQTRSensorsRC qtrrc((unsigned char[]) {0, 9, 15, 17}, 4);
This library takes care of the differences between the QTR-xA and QTR-xRC sensors internally, providing you with a
common interface to both sensors. The only external difference is in the constructors, as you can see in the code sample
above. The first argument to the PololuQTRSensorsAnalog constructor is an array of analog input pins (0 – 7) while the
first argument to the PololuQTRSensorsRC constructor is an array of digital pins (0 – 19). Note that analog inputs 0 – 5
can be used as digital pins 14 – 19.
The only other difference you might experience is in the time it takes to read the sensor values. The QTR-xRC sensors
can all be read in parallel, but each requires the timing of a pulse that might take as long as 1 or 2 ms (you can specify
how long the library should time this pulse before timing out and declaring the result full black). The QTR-xA sensors
use the analog-to-digital converter (ADC) and hence must be read sequentially. Additionally, the analog results are
produced by internally averaging a number of samples for each sensor (you can specify the number of samples to
average) to decrease the effect of noise on the results.
Arduino Library for the Pololu QTR Reflectance Sensors © 2001–2009 Pololu Corporation
2. Library Installation Page 3 of 8








