Using Pololu - QTR-8RC Reflectance Sensor Array

Pololu - QTR-8RC Reflectance Sensor Array


In line following a sensor array is an essential part in the robot. I have used the QTR-8RC array and it works like a charm. With Arduino libraries it is easy to configure them but with PIC micro-controllers you have to handle the hardware configuration part manually.


The trick is we need to charge the capacitors attached to each sensor and see how long it takes to drain them. When the sensor is on a white surface, IR rays will reflect to the photo sensitive diode making a path for the capacitors to drain fast while on a black surface there will be not much IR rays coming back making the drainage duration longer. Depending on that, we can see what color surface that is.

Header file


/*********************************************************************
  This is the header file for IR sensors. There are 8 IR Sensors and
 they will return 1 if they are on a black surface and will return 0
 if they are on a white surface. 
 
 The main functions used here to check the value returned by IRs are

  1. set_IR()

*********************************************************************/

#ifndef IR
#define IR

#define HIGH    1
#define LOW     0

#define ON      1
#define OFF     0

#define INPUT   1
#define OUTPUT  0

#define SensorTRISA                     TRISA
#define SensorTRISE                     TRISE
#define SensorPORTA                     PORTA
#define SensorPORTE                     PORTE
#define SensorLATA                      LATA
#define SensorLATE                      LATE

/********************* Initiating variables *************************/

char sensor1, sensor2, sensor3, sensor4;
char sensor5, sensor6, sensor7, sensor8;
short junction;

/*********** Initiate Sensors PORTs as Digital I/Os *****************/
void init_sensor() {

    // Make all Analog Pins Digital
    ANSEL0 = 0x00;
    ANSEL1 = 0;
}

/********************** Sets sensor pins high ***********************/
void IR_high() {

    SensorTRISA.B0 = OUTPUT;
    SensorTRISA.B1 = OUTPUT;
    SensorTRISA.B2 = OUTPUT;
    SensorTRISA.B3 = OUTPUT;
    SensorTRISA.B4 = OUTPUT;
    SensorTRISA.B5 = OUTPUT;
    SensorTRISE.B0 = OUTPUT;
    SensorTRISE.B1 = OUTPUT;
    SensorLATA.B0 = HIGH;
    SensorLATA.B1 = HIGH;
    SensorLATA.B2 = HIGH;
    SensorLATA.B3 = HIGH;
    SensorLATA.B4 = HIGH;
    SensorLATA.B5 = HIGH;
    SensorLATE.B0 = HIGH;
    SensorLATE.B1 = HIGH;
}

/********************** Sets sensor pins low ************************/
void IR_low() {

    SensorTRISA.B0 = INPUT;
    SensorTRISA.B1 = INPUT;
    SensorTRISA.B2 = INPUT;
    SensorTRISA.B3 = INPUT;
    SensorTRISA.B4 = INPUT;
    SensorTRISA.B5 = INPUT;
    SensorTRISE.B0 = INPUT;
    SensorTRISE.B1 = INPUT;
    SensorLATA.B0 = LOW;
    SensorLATA.B1 = LOW;
    SensorLATA.B2 = LOW;
    SensorLATA.B3 = LOW;
    SensorLATA.B4 = LOW;
    SensorLATA.B5 = LOW;
    SensorLATE.B0 = LOW;
    SensorLATE.B1 = LOW;
}

/************ Assign PORT values to sensor variables ****************/
void sensor_assign() {

    sensor1 = SensorPORTA.B0;
    sensor2 = SensorPORTA.B1;
    sensor3 = SensorPORTA.B2;
    sensor4 = SensorPORTA.B3;
    sensor5 = SensorPORTA.B4;
    sensor6 = SensorPORTA.B5;
    sensor7 = SensorPORTE.B0;
    sensor8 = SensorPORTE.B1;
}

/*************** Trigger IR and get sensor readings *****************/
void set_IR() {

    // First, make all the pins outputs and HIGH them for 10us
    IR_high();

    Delay_us(12);

    // Then make them inputs and clear the PORT register
    IR_low();

    // Now wait for a few microsecond and read the values
    Delay_us(700);

    // Assign each input to defined variables
    sensor_assign();
}
#endif

In this above code, sensorn variable will be either 1 or 0 depending on the surface it is on. Note that this is coded using MikroC IDE. Hope this code might help you with your project.

Comments

Popular posts from this blog

Python Laboratory Excersices

DDR3 vs. DDR3L RAM

Installing TOMCAT