QTR-8RC -- It´s not working

Hello, I am jmagro.

I bought several circuits of QTR-8RC Reflectance Sensor Array, but I have a problem. I want read only the first sensor of this board. I have connected the first sensor to the B0 pin of my microcontroller.
So,I cannot read. Always the first sensor is 3,2v and never the I/O line go to low !

I use the chip PIC16F84A.
I program this chip in C language.
I use CCS compiler.

This is my code:

      long int read_sensor_B0(void){
                            long int microsegundos=0;
    
                            set_tris_b(0xFE);// B0 --> Output
                            output_high(PIN_B0);// Output --> high

                            delay_us(20);// Allow at least 10 us for the 10 nF capacitor to charge

                           set_tris_b(0xFF);// B0 --> Input (high impedance)
         
                           while(input_state(PIN_B0)==1){// Measure the time for the capacitor to discharge
                                       delay_us(1);
                                       microsegundos++;
                           }

                           return(microsegundos);

       }// END

I think that the steps are ok but it´s not working.

Help me!

Thank you, very much.

Hello.

Did you use a multimeter to determine that the sensor is always reading 3.2 V? What do you read if you disconnect the sensor and just measure the voltage on the PIC’s input pin? I’m not familiar with PIC programming so please forgive me if this question does not apply, but is it possible you have your input pin’s internal pull-up resistor enabled?

- Ben

Hi Ben.

This was the error! I am very happy!
Thank you, very much.

This is a very simple code for test this sensors:

//testQTR-8RC.c
// ---------------------------------------------------------------------------
// Each QTR-1RC and QTR-8RC reflectance sensor phototransistor output is tied
// to a capacitor discharge circuit as shown on the right, which allows a
// digital I/O line on a microcontroller to take an analog reflectance reading
// by measuring the discharge time of the capacitor. When you have a
// microcontroller’s digital I/O connected to a sensor output, the typical
// sequence for reading that sensor is:
//
//    Set the I/O line to an output and drive it high
//    Allow at least 10 us for the 10 nF capacitor to charge
//    Make the I/O line an input (high impedance)
//    Measure the time for the capacitor to discharge by waiting for
//    the I/O line to go low.
// ---------------------------------------------------------------------------
#include <16F84A.h>
#fuses XT,NOWDT
#use delay(clock=4000000)


#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOPROTECT              //Code not protected from reading
#FUSES PUT                          //Power Up Timer



#define SENSOR1  PIN_B0
#define SENSOR2  PIN_B1
#define SENSOR3  PIN_B2
#define SENSOR4  PIN_B3
#define SENSOR5  PIN_B4
#define SENSOR6  PIN_B5
#define SENSOR7  PIN_B6
#define SENSOR8  PIN_B7

#define SERVO_MOTOR    PIN_A0



#define ON output_high
#define OFF output_low

//Tipos y Prototipos
long int read_sensors(void);

void main()
{

   long int microsegundos=0;

   set_tris_a(0x00);//xxx43210 --> xxx00000 --> Puerto A: Salidas --> SERVOMOTOR: PIN_A0

   port_b_pullups(FALSE);// ¡VIP!

   //apagamos servomotor
   OFF(SERVO_MOTOR);

   while (true)
   {


      //////////////////////
      //  RASTREO NORMAL  //
      //////////////////////
      microsegundos=read_sensors();
      if (microsegundos <700){ // --> Blanco
            ON(SERVO_MOTOR);
      }
      else{// --> Negro
            OFF(SERVO_MOTOR);
      }


   }//END WHILE

}//END MAIN

//                              F U N C I O N E S                             //

long int read_sensors(void){ // OJO el B0 solamente
   long int microsegundos=0;
         port_b_pullups(FALSE);// ¡VIP!
         
         set_tris_b(0xFE);//76543210 --> 11111110 --> Puerto B: Salida y Entradas de 8 Sensores
         ON(SENSOR1);//output --> high

         delay_us(20);//Allow at least 10 us for the 10 nF capacitor to charge

         set_tris_b(0xFF);//76543210 --> 11111111 --> Puerto B: Entradas de 8 Sensores.
                                //Make the I/O line an input (high impedance)
         
         while(input_state(SENSOR1)==1){
                           //Measure the time for the capacitor to discharge by waiting for the I/O line to go low
            delay_us(1);
            microsegundos++;
         }


   return(microsegundos);

}

I’m very glad to hear you got it working. Thank you for the sample code!

- Ben