Professional Documents
Culture Documents
IR Light Beams
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
http://www.me.umn.edu/courses/me2011/arduino/technotes/irbeam/irbeam.html
1/3
10/27/2014
IR Light Beams
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);
http://www.me.umn.edu/courses/me2011/arduino/technotes/irbeam/irbeam.html
2/3
10/27/2014
IR Light Beams
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.
http://www.me.umn.edu/courses/me2011/arduino/technotes/irbeam/irbeam.html
3/3