2. Library Installation

If you are using version 1.6.2 or later of the Arduino software, you can use the Library Manager to install this library:

In the Arduino IDE, open the “Sketch” menu, select “Include Library”, then “Manage Libraries…”.

  1. Search for “QTRSensors”.
  2. Click the QTRSensors entry in the list.
  3. Click “Install”.

If this does not work, you can manually install the library:

  1. Download the latest release archive from GitHub and decompress it.
  2. Rename the folder “qtr-sensors-arduino-xxxx” to “QTRSensors”.
  3. Drag the “QTRSensors” folder into the “libraries” directory inside your Arduino sketchbook directory. You can view your sketchbook location by opening the “File” menu and selecting “Preferences” in the Arduino IDE. If there is not already a “libraries” folder in that location, you should make the folder yourself.
  4. After installing the library, restart the Arduino IDE.

You should now be able to use these libraries in your sketches by selecting Sketch > Import Library > QTRSensors from your Arduino IDE (or simply type #include <QTRSensors.h> at the top of your sketch). Note that you might need to restart your Arduino IDE before it sees the new libraries.

Once this is done, you can create a QTRSensorsAnalog object for your QTR-xA sensors and a QTRSensorsRC object for your QTR-xRC sensors:

// create an object for three QTR-xA sensors on analog inputs 0, 2, and 6
QTRSensorsAnalog 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)
QTRSensorsRC 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 QTRSensorsAnalog constructor is an array of analog input pins (0 – 7) while the first argument to the QTRSensorsRC constructor is an array of digital pins (0 – 19). Note that analog inputs 0 – 5 can be used as digital pins 14 – 19. For more details, see Section 3.

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 3 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.

Several example sketches are available to help you get started. To view the example sketches, open the Arduino IDE and navigate to:

File > Examples > QTRSensors

As a first step, we recommend using QTRARawValuesExample (for QTR-xA sensors) or QTRRCRawValuesExample (for the QTR-xRC sensors). These examples will simply print the raw readings from the sensors to the serial monitor, using 9600 baud. Once that is working, you might want to try one of the more advanced examples, QTRAExample or QTRRCExample, which incorporate calibration and also estimate the position of a line.

New Products

ACS72981KLRATR-150B3 Current Sensor Large Carrier -150A to +150A, 3.3V
A5984 Stepper Motor Driver Carrier, Fixed 1A@5V / 660mA@3.3V, Blue Edition
ACS72981KLRATR-150B5 Current Sensor Large Carrier -150A to +150A, 5V
CT433-HSWF30DR TMR Current Sensor Compact Carrier 0A to 30A, 3.3V
1.4-7V, 3.8A Fine-Adjust Step-Down Voltage Regulator w/ Adjustable Low-Voltage Cutoff D30V33MALCMA
CT432-HSWF30MR TMR Current Sensor Compact Carrier -30A to +30A, 5V
A5984 Stepper Motor Driver Carrier, Fixed 1.5A@5V / 1A@3.3V, Blue Edition (Soldered Header Pins)
CT432-HSWF20DR TMR Current Sensor Compact Carrier 0A to 20A, 5V
Pololu Reverse Voltage Protector, 4-60V, 12A
ACS72981LLRATR-100B3 Current Sensor Compact Carrier -100A to +100A, 3.3V
Log In
Pololu Robotics & Electronics
Shopping cart
(702) 262-6648
Same-day shipping, worldwide
Menu
Shop Blog Forum Support
My account Comments or questions? About Pololu Contact Ordering information Distributors