Detecting an IR Beam Break with the Arduino IR Library

One reader asked how to use my Arduino Infrared Library to detect breakage of an IR beam. The answer was too long for the comments section, so I've extended into a blog post. One straightforward way is to use the library to modulate an IR LED at 38kHz, and use a standard IR detector module to detect the signal. The output from the module can be simply read as a digital input.

Here is a simple sketch to do that. The IR LED is connected to pin 3 through a 100 ohm resistor, the detector is connected to pin 2, and a status LED is connected to pin 13 (if your Arduino board doesn't have it already.) To create the modulated output, simply call irsend.enableIROut(38); to set the PWM to 38kHz, and then irsend.mark(0) to send a mark (i.e. turn the output on). The loop simply reads the input from the detector, inverts it (since the detector is active-low), and writes it to the status LED.

#include <IRremote.h>

#define PIN_IR 3
#define PIN_DETECT 2
#define PIN_STATUS 13

IRsend irsend;
void setup()
{
  pinMode(PIN_DETECT, INPUT);
  pinMode(PIN_STATUS, OUTPUT);
  irsend.enableIROut(38);
  irsend.mark(0);
}

void loop() {
  digitalWrite(PIN_STATUS, !digitalRead(PIN_DETECT));
}
You should see the pin 13 LED light up when the IR receiver detects an infrared signal, and go dark when the receiver does not detect an infrared signal. The circuit is basically trivial, but there's a schematic at my original article if you need one. The following picture shows the detector setup. Note the illuminated status LED.
IR detector circuit
If the circuit doesn't work, first use a cell phone camera to verify that the infrared LED is lit. If the LED is not lit, try reversing it. Here's my cellphone's view of the illuminated LED:
illuminated infrared LED
Next, point a TV remote control at the detector and make sure the status LED flashes. If the status LED doesn't come on, make sure you wired the detector properly, and connected to input 2 of the Arduino. If the status LED won't turn off when you break the beam, you probably aren't blocking the IR signal well enough. The detector is very sensitive, and IR bounces off many things, so you may need to separate the LED and detector by several feet and make sure you're fully blocking the IR beam. Note that the library assumes you're using a ATmega168/328, so you're on your own if you have a different processor.

The tricky part of this is the optics - figuring out how to set up the LED and receiver so the beam gets interrupted when you want it to. Also, you'll probably want to modify the detection logic to do something more interesting than just set the status LED, but that's left as an exercise for the reader :-)

34 comments:

Ioan said...

Thank you!
Best regards.

Anton said...

This technique can alsa be used for non-contact collision detection for robots. The Boe-Bot for example does just that, send out a 38kHz IR beam and check if it's reflected.

Ioan said...

Yesterday I received the 38kHz IR detectors from Digi-Key and I was able to test the IR Library. Worked perfect from the first try. Thank you!

lukethomastaylor said...

Is there any way to do this "PWM style" break beam with a simple IR detector (i.e. not one that automatically detects a 38kHz modulated IR signal, just a transistor)?

Sergio said...

Hi, This is a great library and circuit. Thanks for sharing.
I'm having a problem with my detector, as I could figure out, I'm using TSOP1838 with basically rejects continuos 38Khz frequency, see the datasheet at:
http://www.datasheetcatalog.org/datasheets/134/301155_DS.pdf

I'm wondering what sensor can I use that does not supress continuous frequencies.

Thank you.

Anonymous said...

I have built this circuit and during testing, I noticed that when I cut the beam, the light on my arduino board (L or pin 13).

In the write up it says the light should shine always, and shut off when the beam is interrupted.

Is this a typo, or is there something strange on my end?

Also, I noticed that if I bring the detector and emitter very close, the pin 13 light goes on, but nothing happens when I break the beam.

Any idea what I have gotten wrong?

Iain Smith said...

Great tutorial, works a treat!

Also that's a good tip (Anton) for the non-contact object detection, my guess is that this will use alot less power than something like the PING board (ultrasonic).

You can also use it for a really cool non-contact "switch" (for example to enable something)

regarding the comment from Anon did you remember the "!" to invert the status?

Marc said...

Here is a fix for the 838 and any others that reject a continuous beam.

void loop() {
irsend.space(0);
delay(1);
irsend.mark(0);
digitalWrite(PIN_STATUS, !digitalRead(PIN_DETECT));
delay(1);
}

Declan said...

I know this is an old post, but...

Is it possible to use two emitters and two detectors without them interfering?

Is it just a case of giving them a different frequency?

If so, do you think these will do?: http://www.sparkfun.com/products/241

Thanks in advance,

Declan

scottyjr said...

Thanks for the library. I'd like to change the pin assignment for the emitter. I tried by changing it in the code but had no success so I suspect it needs to be changed elsewhere also. How can I do this? Thanks

solaron99 said...

Hi Ken,

my sketch currently uses pin3, I attempted to change the variable declaration from your sketch:

#define PIN_IR 3 to #define PIN_IR 5

... it doesn't work,
I want to use my Arduino Uno's pin 5 for the IR emitter.

Do I have to modify some code in the library?
Any other PWM I declare does not work either except pin 3.

Please help!

Thx!

Abinand said...

thanks a ton!!!!!!!!!

Kalium said...

Well, the continuously lit led is a problem, concerning distances, I need a long range device, so, for better performance the led must flash at the same frequency (and must be synch) as the detector... Could you help me?

Chris Ault said...

Could this pick up a paintball ( 16 mm ) moving at a speed of 91.44 mm / millisecond (300FT/Sec) ?

Chris Ault said...

The ball is moving 16mm in 150 MICRO seconds. I know it's fast but the Arduino is fast too.. Thanks for any input!

Chris Ault said...

9 uS per mm if the beam is the width of the LED(5mm) then that's 9uS/mm*5mm = 45uS initial beam crossing time. Then to fully pass the beam it's one ball width off so I calculate total time in front of the beam to be 195uS or .195milli seconds... I know the arduino can measure in Micros().

It's this or $60 for a chronograph .

Scott said...

I have a project where I want to use an IR beam to detect snow levels. My plan is to have 24 receivers (Digikey 425-2528-ND) 1/2 inch apart so they will measure up to 12 inches of snow. Since there are so many inputs, I'll use the Arduino Mega to handle all if it. For the emitters, I'm using 10 wide angle (60 degree) IR LEDs (Digikey 67-1001-ND). I'll use a MOSFET to power the LEDs from the pin 9. Do you foresee any issues with your library and this project?

Jakub Górzyński said...

Just wondering if it would be possible to detect more than one breaking line (on different digital pin).

I would need 3 of them to detect which gear is engaged in my car by looking on the lever (-:

charlton rovers reds said...

thanks for the post,
In my project i am using the break beam to detect water level in a pipe, as part of the electronics i have added and extra receiver and an extra transmitter so that i can test if any component is not working. However for this to work I need to turn of the output signal on one transmitter and turn the other one on, but i can not figure out a way to do this in the programme. Any help would be greatly appreciated.

Nick said...

Hi Ken,
My LED on pin 13 remains constantly ON. I do not see the IR beam coming when viewed from a phone camera. Any indications?

Anonymous said...

Also having problems with the continuously-lit IR LED. Solution suggested by Marc didn't work for me. I verified that the detector (AX1838HS) works using a TV remote. I verified that the IR LED is emitting using my cell phone. Any suggestions would be appreciated.

skjegg|pt said...

hi i cannot get the emitter to be lit, tried with a normal led to see but nothing, the reciever is working.

I have the arduino mega2560 and set up on pin 3

skjegg|pt said...

hi i cannot get the emitter to be lit, tried with a normal led to see but nothing, the reciever is working.

I have the arduino mega2560 and set up on pin 3

Domingo Jordan said...

have any one make it work, using a mega 2560? my does not work.

Dan said...

You need to use pin 9 on the mega 2560

Anonymous said...

check the tsop type.
I tested with tsop 1138 & tsop 1838
- datasheet of 1138 says "Max. Envelope Duty Cycle vs. Burstlength"
- datasheet of 1838 says "The data format should not make a continuous
signal transmission. There must be a Signal Gap Time
(longer than 15ms) at least each 90ms (see Figure A)."
- datasheet of tsop 1738 says : "Continuous data transmission possible
(1200 bit/s)"
this ir beam seems to work only with tsop 17xx

Takis said...

This works great. TSOP38238 used.
Thanks!

David Sanz Kirbis said...

Just in case someone else is having trouble with the continuous beam rejection, this worked for me:

/*
IR beam break / obstacle detection using 38khz sensors Vishay VS 1838 and the like (TL1838, etc).

Example modified to avoid continuous beam rejection. Original code and connection info here:

http://www.righto.com/2010/03/detecting-ir-beam-break-with-arduino-ir.html

See comments here:

http://www.righto.com/2010/03/detecting-ir-beam-break-with-arduino-ir.html?showComment=1325625910001#c1092560206530145093


For obstacle detection make sure the IR sender points to the same direction as the
receiver, but is not in its field of view (i.e. put a pipe or tape around the led to
avoid reflections or light going back). You can tune the detection distance with a
variable resistor, decreasing the led's beam intensity.

---------------------------

May 2015, David Sanz Kirbis
www.therandomlab.com

*/
#include

#define PIN_IR 3
#define PIN_DETECT 7
#define PIN_STATUS 13
// ms that beam will be disabled
#define BEAM_OFF_TIME 60


IRsend irsend;

int count;
boolean detected = false;

unsigned long timeStamp = 0;

void setup()
{
Serial.begin(57600);
pinMode(PIN_IR, OUTPUT);
pinMode(PIN_DETECT, INPUT);
pinMode(PIN_STATUS, OUTPUT);
irsend.enableIROut(38);
}

void loop() {

if (!detected) {
irsend.space(0);
delay(1);
irsend.mark(0);
delay(1);
detected = !digitalRead(PIN_DETECT);
if (detected) {
digitalWrite(PIN_IR, LOW);
}
digitalWrite(PIN_STATUS, detected);
Serial.println(detected);
timeStamp = millis();
} else if (millis()-timeStamp > BEAM_OFF_TIME) {
detected = false;
irsend.enableIROut(38);
}
}

David Sanz Kirbis said...

even better :)

void loop() {

if (millis()-timeStamp > BEAM_OFF_TIME) {
detected = false;
irsend.enableIROut(38);
}
if (!detected) {
irsend.space(0);
delay(1);
irsend.mark(0);
delay(1);
detected = !digitalRead(PIN_DETECT);
if (detected) {
digitalWrite(PIN_IR, LOW);
}
digitalWrite(PIN_STATUS, detected);
Serial.println(detected);
timeStamp = millis();
}

}

Anonymous said...

hi
i would like to know how to you stop other 38khz infrared's from triggering your beam

Stéphane Deniaud said...
This comment has been removed by the author.
Stéphane Deniaud said...

#include <IRremote.h>

#define PIN_DETECT 2
IRsend irsend;

void setup()
{
    pinMode(PIN_DETECT, INPUT);

    Serial.begin(115200);
    Serial.println("READY");

    attachInterrupt(digitalPinToInterrupt(PIN_DETECT), checkIRBeamBreak, RISING);

    // Note : the IR emitter is on PIN #3
    irsend.enableIROut(38);
    IREmitterOn();
}

// Use this function instead of delay() as delay() does not work in interrupt callbacks
void pause(int ms) {
    // We need a loop as the largest value for delayMicroseconds that will produce an accurate delay is 16383
    for (int i = 0; i < ms; i++) {
        delayMicroseconds(1000);
    }
}

void IREmitterOff() {
    irsend.space(0);
    pause(60); // 60 ms is OK for my TSOP but should be tuned
}

void IREmitterOn() {
    irsend.mark(0);
    pause(10);
}

void switchOffOnIREmitter() {
    IREmitterOff();
    IREmitterOn();
}

void checkIRBeamBreak() {
    int val = digitalRead(PIN_DETECT);
    // LOW : no beam break
    // HIGH : beam break
    if (val == LOW)
        return;

    detachInterrupt(digitalPinToInterrupt(PIN_DETECT));
    switchOffOnIREmitter();
    if (digitalRead(PIN_DETECT) == HIGH) {
        Serial.println("IR BEAM BREAK !!!!");
    }
    attachInterrupt(digitalPinToInterrupt(PIN_DETECT), checkIRBeamBreak, RISING);
}


void loop() {

}

Stéphane Deniaud said...

Sorry, I forgot the explanations in my previous post :)
When dealing with IR Beam break detection, I prefer interrupts : if the IR beam is used for an automatic gate for instance, you want to react quickly, no matter what is being processed in the loop function.

I have a TSOP that does not allow continuous signal.

With such a TSOP, when the IR emitter is ON (with a 38kHz frequency), the TSOP will detect the IR Beam for only a specific (short) time, then, it will no longer detect anything (it's blocked). So, You have to switch off your IR emitter periodically to 're-initialize' the TSOP.
That's the trick with the previous sketch, when the interrupt is fired :
- I switch Off/On the IR emitter with a pause (60 ms for me but should be tuned to match your TSOP's specs)
- I immediately check the 'out' of the TSOP (digitalRead) :
        - if the IR beam is really broken, digitalRead will return HIGH
        - if not, the TSOP was just in a blocked state. Having switched off the IR emitter has re-initialized it and it's now able to detect the IR beam. Here, digitalRead() will return LOW.

David Sanz Kirbis said...

@Stéphane Deniaud

Very nice method!