Skip to content

QTR Reflectance Sensor Array (Line sensor)

Dario Jimenez edited this page May 6, 2022 · 10 revisions

QTR Reflectance Sensor Array:

What does it look like?

QTR-8A Sensor

QTR-3A Sensor

What Is it?

the QTR Reflectance sensor array is an array (board with a line of sensors equally spread out) of sensors that output an analog voltage based on the reflectance of the object directly in front of it. There is the QTR-8A reflectance sensor which contains 8 analog reflectance sensors and the QTR-3A reflectance sensor which contains 3 analog reflectance sensors.

Specifications

  • Operating voltage: 3.3-5.0 V (QTR-8A) or 5V (QTR-3A)
  • Supply current: 100 mA (QTR-8A) or 5 mA (QTR-3A)
  • Output format: 8 analog voltages (QTR-8A) or 3 analof voltages (QTR-3A)
  • Output voltage range: 0 V to supplied voltage
  • Optimal sensing distance: 0.125" (3 mm)
  • Maximum recommended sensing distance: 0.25" (6 mm)
  • It is strongly recommended to use a 5V, 5A regulator at the power input.

How does it work?

The sensor will have either 8 or 3 infrared LED/phototransistor pairs (black chips mounted on sensor board) that are capable of turning the reflectance of an object to an analog voltage from 0V to 3.3V. A white object (which reflects a high amount of light) will be detected by the sensor as high reflectance and hence output an analog voltage close to 0V. On the other hand, a black object (which reflects a small amount of light) will be detected by the sensor as low reflectance and output an analog voltage close to +3.3V. Any other color between black and white will have a voltage of anywhere between 0V and +3.3V.

What can it be used for?

Sensor is mainly designed to be used as a line sensor by placing the sensor in such a way that you can detect changes in color along a path. i.e, if robot has to go through a path marked by black tape where the floor is white, the QTR reflectance sensor can be placed across the width of the robot (could be placed some other way as well) such that some of the sensors detect when the left/right side of the robot is on top of the black tape; this way, you know your robot has to turn left or right depending on which side the black tape was detected. The way you could make this work, is by constantly checking the values of your leftmost/rightmost sensors until they detect a low reflectance color (black) and have the software trigger a function (which you would have to write/code) that turns the robot left or right (something along the lines of "robot_TurnLeft" or "Robot_TurnRight").

The sensor can also be used as a general proximity sensor using the fact that the sensors only detect the reflectance of objects at a very short distance (distance). You have to find out what value it outputs whenever there is no object near it and when there is an object near it. of course, the value the sensor will output when an object is near will depend on the color of the object, so keep in mind that it will only work with objects of certain colors. you can make it do something specific whenever the sensor's value changes to a value that is not the default value when there is no object near it. this can be useful to avoid having the robot crash into a wall.

It can also be used as a simple reflectance sensor by figuring out what value the sensor outputs for each colored object. For example, a red object might make the sensor output a value of 3000 while blue might make it output a value of 1000, so you can use that fact to distinguish between different color objects. This might be helpful if the robot has pick up or avoid a certain colored object.

How to Interface with Sensors

In order to read the output analog voltages from sensors, you must use a microcontroller's analog-to-digital converter (ADC). You can use the "ADC" library to initialize the TM4C's ADC and analog pins to read the output voltages from the sensors. The TM4C can read analog voltages of up to +3.3V, so when using the TM4C's ADC you will get a value ranging from 0-4095. a value of 0 indicates a analog voltage close to 0V while a value of 4095 indicates a voltage close to +3.3V. Any value in between indicates a corresponding voltage between 0V and +3.3v with a step size of approximately 0.8 mV (smallest change in voltage detectable by the ADC).

In order to interface with the sensors, follow these couple of steps:

  1. the pins of the desired sensors must each be connected to valid analog pins in the TM4C (look at the ADC library for a full list of valid pins). Connect VCC pin to +3.3V (you can connect it to 5V but TM4C will only read analog voltages of up to +3.3V) and the GND pin to ground.

  2. Create an instance of a line sensor by declaring a global variable of type "LineSensorData_t" as shown below. This allows us to use the line sensor object wherever in our code. Store the name of the analog pins that you will use in sensor.pins field of declared instance. You must include the "LineSensor" library by using the "#include" macro

#include <lib/LineSensor/LineSensor.h>

LineSensorData_t sensor;

/* Choose microcontroller pins for the QTR Sensor */
sensor.pins[0] = PD3;
sensor.pins[1] = PD2;
    
int main(void){
    //other code...
}

  1. Initialize the TM4C's analog-to-digital converter (ADC) by calling the "ADC_Init" function from the ADC library. You must import the library by using the "#include" macro as shown in the code snippet below.
#include <lib/ADC/ADC.h>

int main(void){

    ADC_Init(); //initialize ADC

}
  1. Initialize the necessary GPIO pins for the desired amount of sensors as analog, with alternate mode on and alternate function 8 as shown below. Look at the GPIO library for more details on GPIO initialization.
#include <lib/GPIO/GPIO.h>
#include <lib/ADC/ADC.h>

int main(void){

    /* Initialize PD3 as a GPIO analog input with alternate function 8 (ADC) */
    GPIOConfig_t PD3Config = {PIN_D3, NONE, false, true, 8, true};

    /* Initialization of GPIO pins */
    GPIOInit(PD3Config); 
}
  1. Initialize the line sensor by passing the previously declared LineSensorData_t variable/instance as a pointer (simply put a '&' in front of he variable name) as the first parameter and the number of pins used as the second parameter as shown below.
#include <lib/LineSensor/LineSensor.h>

LineSensorData_t sensor;

int main(void){

    LineSensor_Init(&sensor, 3);    
}

  1. Use one of the two methods in LineSensor library to read/update data from sensors. the two methods are: LineSensor_GetIntegerArray & LineSensor_GetBoolArray. There will be more information about them below, but you can always check the LineSensor library for more details.

LineSensor_GetIntegerArray:

  • This method updates the SensorValues field from your declared instance of LineSensorData_t with the raw value of analog voltage from sensors
#include <lib/LineSensor/LineSensor.h>

LineSensorData_t sensor;

int main(void){

   while(1){
 
        /* Read raw data (0-4095) from the two sensors and have it stored in "sensor.SensorValues" array */
	LineSensor_GetIntegerArray(&sensor);
   }
}

LineSensor_GetBoolArray:

  • This method updates the SensorValues field from your declared instance of LineSensorData_t with boolean values (1 or 0) depending if the analog values from sensors passed the set threshold passed as the first parameter.
#include <lib/LineSensor/LineSensor.h>

LineSensorData_t sensor;

int main(void){

   while(1){
 
        //Read from the QTR Reflectance sensors and get boolean array using a threshold of 2048
	LineSensor_GetBoolArray(2048, &sensor);
   }
}
  1. Finally, use the data from sensors to make the robot do something: the possibilities are endless! Below is an example of one way the data can be used and how to access it within the declared instance of "LineSensorData_t sensor".
#include <lib/LineSensor/LineSensor.h>

LineSensorData_t sensor;

int main(void){

   while(1){
 
	if(sensor.SensorValues[0] > 2048){
            DoSomething();
        } 
   }
}

More Resources:

User's Guide: https://www.pololu.com/docs/pdf/0J12/QTR-8x.pdf

QRE1113GR SMT Reflective Object Sensor datasheet: https://www.pololu.com/file/0J117/QRE1113GR.pdf