IR Light Beam Circuits

Note: Some of the code be

Use an IR LED and phototransistor pair to create a light beam switch. Point the components at each other to turn the switch on, then break the beam to turn the switch off. Use to detect when of your machine passes by a particular point. Or, bounce the light from the diode off a part to reflect back onto the detector. If the part is there, light will reach the detector and the signal can be passed to your Stamp. An IR LED/detector pair is exactly how your TV remote works. You can control your TV from across the room because the diode is pulsed briefly at a much higher current which gives off much more light.

The ME2011 robot store sells an IR LED/phototransistor pair, or you can purchase elsewhere.

IR LED: Radio Shack 276-143 ($1.69) or Jameco 106526 ($0.45). Connect like an ordinary LED using a 330 ohm series resistor to the +5 supply (or to a Stamp pin if you want to switch the source on and off). Current draw is about 11 mA with a 330 ohm resistor. Current runs from anode to cathode. Flat on the case marks the cathode.

IR Phototransistor Radio Shack 276-145 ($0.99) or Jameco 112168 ($0.45). A phototransistor is just like a regular transistor except the base lead is disabled or absent and light activates base current. The flat on the case marks the collector, the other lead is the emitter. Connect the collector to one end of a 10K resistor and connect the other end of the resistor to a +5V supply (you can use the +5 pin on the Stamp). Connect the emitter to ground. To view output, measure the voltage at the collector with your DVM. The voltage should start out at +5V. When pointing the IR diode at the phototransistor, the voltage should drop down to near zero. To interface with the Stamp, make a second connection from the collector to a Stamp pin (for the example below use Pin 4).

Telling them apart The IR LED and the IR phototransistor look alike. If you got them from the robot store, chances are you can look closely, tell them apart, and infer which leads go where:

However, this may not always work. If you don't know which is which, you can try them in the test circuit described above, and swap the pair and flip the orientation of each until it works. There are only four possible combinations so it should not take very long. The cathode of the LED (the side that connects to ground) has a flat on the rim of the case. IR light can be seen by most digital cameras, so another way to tell if the LED is working is to point your cell phone camera at what you think is the LED and look for the beam.

Connections Use the figure below as a guide for connecting the sensor/detector pair. To test, point them directly at each other separated by 2-4 inches.

Test Program

Load this Arduino program which you can use to test the operation of the IR sensor
void setup() {
  pinMode(4,INPUT);
  Serial.begin(9600);
}
void loop() {
  Serial.println(digitalRead(4));
  delay(100);
}

This is the simplest program you can use to test the operation of your IR sensor. Watch the numbers on the screen change between 1 and 0 as you block and unblock the sensor. If they don't change, measure the voltage at Pin 4 while you block and unblock the sensor. If the voltage does not change, disconnect the wire going to Pin 4 and measure the voltage on the wire (not on the pin). If it doesn't change, there is a wiring error, or the sensor or emmitter is in backwards or the sensor or emitter is bad. Note that the IRLED and phototransistor should be no more than two or three inches apart for this test.

Usage Example

This is the demo shown by Durfee in the ME2011 lecture. Wire an LED through a 330 ohm resistor to Pin 2 in the standard manner. Connect the IR beam switch to Pin 4 as described above.

Load this program into your Arduino:

/*---------------------------
IR beam demo
----------------------------*/
#define LED 2
#define BEAM 4
byte i;
void setup()
{
  pinMode(LED,OUTPUT);
  pinMode(BEAM,INPUT);
  digitalWrite(LED,LOW);
}
void loop()
{
  digitalWrite(LED,digitalRead(BEAM));
}

Hold the IR diode 2 to 3 inches from the phototransistor. The LED should light. Put your hand or a card between the diode and phototransistor and watch the LED turn off. For a slightly longer distance, drive the LED with more current. (A 100 ohm resistor will result in 38 mA, but be sure to hook it up to a separate +5 supply rather than a Stamp pin. Or, calculate the appropriate resistor and connect to a +12 supply.) Circuit is sensitive to ambient lighting. Can put each component in a dark tube to mask. Warning, if the circuit works perfectly in your room, it may or may not work in another location because the room lighting is different.

Additional Tips

You can use the sensor pair to detect light and dark surfaces, for example in a line following robot. Point both down to the floor and then tilt towards each other slightly. Range is about two inches but one inch is better. Sensor will output 1 for no IR and 0 for IR. For alignment, see http://imgur.com/TZkyR.