3Pi Projects‎ > ‎

Project 6 - Simple IR Sensor

/*  
   ############################################################################ 
   ##                                                                        ## 
   ##                    Project 6                                           ## 
   ##                    Control of a single IR Sensor                       ## 
   ##                    https://sites.google.com/site/3piprojects/          ## 
   ##                                                                        ## 
   ##                    Daniel J. Sullivan                                  ## 
   ##                    August 05, 2011                                     ## 
   ##                                                                        ## 
   ############################################################################ 

This is a code example for operating an IR sensor without calling 
the PololuQTRSensors Library. The code essentially follows the typical 
sequence for reading the sensor that is listed on the QTR-1RC page:
  
    1. Set the I/O line to an output and drive it high
    2. Allow at least 10 us for the 2.2 nF capacitor to charge
    3. Make the I/O line an input (high impedance)
    4. Measure the time for the capacitor to discharge by waiting for the
       I/O line to go low

The circuit for the sensor is shown on the right. The IR LED is always on; 
the five IR LEDs on the 3Pi are connected in series and are powered by Vboost.

When the sensor pin is set to output, pinMode(pin, OUTPUT),and driven high,
digitalWrite(pin, HIGH),the 2.2 nF capacitor is charged to a voltage of 5 V. 
The RC time constant (R*C) is 0.5 us so the delay of 10 us provides plenty of time
for the capacitor to charge to 5V.

The sensor pin is then set as an input, pinMode(pin, INPUT)and is then driven low,
digitalWrite(pin, LOW) allowing the capacitor to discharge 
through the IR phototransistor
at a rate that is proportional to the amount of IR light that is falling on it. 
So the discharge rate is fast if the sensor is over an IR reflective surface such as a
white piece of paper and slow when over an IR absorbing surface such as a piece of
black tape. 

A timer, that begins when the command digitalWrite(pin, LOW)is called, is used to 
record the time required for the capacitor voltage to falls below approximately
2.3 V causing the pin to read LOW. 
 
It will usually be more practical and efficient to use the library 
when writing programs that require the five IR sensors on the 3Pi, this code
is primarily intended to show how the steps of the process work.
The program shows that the capacitor takes about 100 microseconds to discharge 
through the phototransistor when it is over a white piece of paper and about 
1000 microseconds (1 millisecond) when it is over a piece of black electrical tape.



*/

#include <OrangutanLCD.h>
#include <OrangutanPushbuttons.h>

OrangutanLCD lcd;
OrangutanPushbuttons buttons;

/*
The following assign the pins to button B and the middle IR sensor.
Noting that these are the pins numbers using the Arduino IDE
*/

const int buttonB = 12;
const int sensor2 = 16;

// Variables used to calculate the time for the IR sensor pin to go low.

unsigned long startTime; 
unsigned long elapsedTime;

unsigned char button;   // Define a button variable
  
void setup(){
  
// Print message to LCD and wait for Button B to be pressed.

  button =buttons.waitForPress(buttonB);
  lcd.clear();
  lcd.print("IR 2");
  lcd.gotoXY(0,1);
  lcd.print("Push B!");
  button =buttons.waitForPress(BUTTON_B);
  
}


void loop()
  
//************* Read One Sensor and Display Value on LCD ****************** 

  pinMode(sensor2, OUTPUT);       // Set pin sensor2 to output
  digitalWrite(sensor2, HIGH);    // Set the pin voltage to +5V
  delayMicroseconds(10);          // Charge capacitor for 10 usec
  pinMode(sensor2,INPUT);         // Disable internal pull-up on pin sensor2
  digitalWrite(sensor2, LOW);     // Drive pin sensor2 LOW allowing the capacitor to dicharge through phototransistor.
  startTime = micros();           // micros() is the number of microseconds since the program began. 
  while(digitalRead(sensor2) == HIGH)
  {       
    // Loop until capacitor has discharged below ~2.3V. 
  }
  elapsedTime = micros() - startTime; // Calculate the amount of time for capacitor to discharge.
  
  // Print result to LCD. Pause 150 milliseconds and repeat.
  
  lcd.clear();
  lcd.print("sensor 2");
  lcd.gotoXY(0,1);
  lcd.print(elapsedTime);
  delay(150);
}
  

Comments