Beam-break sensor with reflector over 15m

Alright, update time...

I just uploaded the sketch IRrelay on an Arduino pro Mini 3.3V and hooked it up with a VS1838B IR receiver. I won't paste the code right here but essentially it decodes incoming IR signals and outputs it in some format to the Serial Monitor. When pointing a random IR remote to it it reads:
Could not decode message
Raw (70): -15298 150 -50 8550 -4400 550 -600 550 -500 600 -500 550 -600 500 -550 550 -550 550 -550 550 -550 550 -1650 550 -1650 550 -1650 550 -1650 550 -1650 550 -1650 550 -1650 550 -1650 500 -1650 600 -550 500 -1650 600 -1650 500 -550 550 -550 550 -550 550 -550 550 -550 550 -1650 550 -550 550 -550 550 -1600 600 -1650 550 -1650 550 -1650 500

So far so good.

I then uploaded the following code to an Arduino Uno 5v with an IR led hooked up through pin D11 along with a 150ohm resistor.
According to another post i found this should modulate the output to 38 khz although I don't understand much of it:

const byte LED = 11; // Timer 2 "A" output: OC2A

void setup() {
pinMode (LED, OUTPUT);

// set up Timer 2
TCCR2A = _BV (COM2A0) | _BV(WGM21); // CTC, toggle OC2A on Compare Match
TCCR2B = _BV (CS20); // No prescaler
OCR2A = 209; // compare A register value (210 * clock speed)
// = 13.125 nS , so frequency is 1 / (2 * 13.125) = 38095

} // end of setup

void loop() { }

Now when pointing the IR led to the IR receiver it reads:

Could not decode message
Raw (10): -15530 50 -300 100 -250 350 -150 100 -100 50864
Could not decode message
Raw (44): -3052 100 -400 100 -1050 350 -1800 100 -950 100 -200 600 -300 100 -250 100 -150 100 -200 100 -200 100 -200 100 -150 100 -300 100 -100 24406 -50 250 -200 100 -500 100 -1600 100 -250 100 -1550 50 -1400 100
FFFFFFFF (0 bits)
Raw (28): -6800 100 -1700 100 -150 100 -4350 100 -1000 100 -750 100 -1550 100 -2150 100 -1700 400 -2550 100 -2050 100 -150 100 -800 100 -3450 100

So something IS getting through.
Now I just need to figure out how to make the receiver act as a "beam-break" and do something when there's no input.
Next step is upscaling the IR LEDs for more power and testing with reflector over different distances.

Any inputs as to how I should code the receiver?

If you can make the reflector shaded from other lights, you don't need to modulate your lights. If you narrow the detector view to only the reflector and keep ambient light off the reflector then day or night will not affect the design.

The corner reflector will send light back where it came from, a bit to the side. If your emitter(s) have no other light sources nearby then the only light reflected back will be from them. If brightness comes from behind the reflector, block it with a dark backboard.
3 square pieces of flat mirror and some superglue could make a corner reflector, getting the 90 degree angles right is crucial though.

An AVR IO pin should not source > 25mA continuous to a led. A red led with 220R resistor draws about 10mA, an IR led with lower forward V will draw more. You're very likely safe powering 6 of those from an AVR chip Arduino.

The signal of an IR remote control is double modulated.
Once with a 38kHz signal and once more with data.

Modulated IR (data) could save (battery) power, but slows down reaction time (if important).
Continuous IR (just 38kHz) does not need demodulating.
The 3-pin sensor already does that for you and just outputs a low/high with signal/no signal.

Note that some 3-pin IR sensors don't like continuous IR.
Leo..

Wawa:
Note that some 3-pin IR sensors don't like continuous IR.
Leo..

I saw this somewhere else also.
Now I've ordered some Vishay TSSP4038 ir receivers that seem to like continuous waves and some 940nm or leds with a half-angle of 5 degrees.
I'm hoping these receivers will just output high when illuminated to make programming easier.

They output a LOW when illuminated.
Leo..

Wawa:
They output a LOW when illuminated.
Leo..

Ahh. My bad. Still feasible though

In code it is only which one you look for. Fix with keyboard.

You can run 2, maybe 3 IR leds in series with 1 resistor on 5V but the light is steady. 12V can run 3 white leds or maybe 5 or 6 IR leds. You don't need a controller for that.

Okay, I've got it working... somewhat.
See attached schematics.

I've used this code:

const byte IR_LED = 11; // IR transmitter LED with 100ohm (minimum) CL resistor
const byte IR_Receiver = 8; // from receiver output
const byte onboard_LED = 13; // onboard indicator LED
boolean receiverState;

void setup() {
  pinMode (onboard_LED, OUTPUT);
  pinMode (IR_LED, OUTPUT);
  pinMode (IR_Receiver, INPUT);
  // from Nick Gammon
  TCCR2A = _BV (COM2A0) | _BV(WGM21);
  TCCR2B = _BV (CS20);
  OCR2A =  209; // ~209 = ~38kHz | ~219 = ~36kHz
}

void loop() {
  receiverState = digitalRead (IR_Receiver);
  if (receiverState == HIGH) { // beam interrupted
    digitalWrite(onboard_LED, HIGH); // green onboard LED off
    delay(1000);
  }
  else { // beam detected
    digitalWrite(onboard_LED, LOW); // green LED on
  }
}

At first it would only "work" when I wiggled the setup. The status LED would come on but turn off again. Then I tried putting in the C1 capacitor (0.1uF) and now the setup works on my prototyping board. When pointing the LED at the sensor the LED turns off. I actually successfully had it working with a retroreflector over a distance of 3m.

Then I soldered some cables to both the IR LED and the three legs on the VSSP4038 so that I could put them in an enclosure a bit away from the Arduino. Now it doesn't work! The status LED is always off indicating that the beam is always visible. I thought I burned the sensor with my soldering iron, replaced it, no change. When I unplug the signal pin from the sensor the status LED turns back on so it must be the sensor that now always pulls the pin 8 low. Can it really be 50cm of wire that "confuses" the setup here?
What am I missing? How can I fix this?

Btw I don't know if the pull-up resistor on the signal pin is necessary but it didn't seem to make a difference on the working setup

Link to sensor specs

Okay, update time again.

It seems I'm having some noise issues.

The setup is working as stated above but I've discovered that sensor and LEDs need to be physically separated. If I cover up one or the other and bring them close together the sensor "detects" a beam. Even when holding the sensor in my closed fist where it can't see anything it still "detects" the LED when I bring my fist close to it.

I've put a 0.1uf capacitor on the sensor and tried one on the LED as well. No change.

I've even tried having emitter and receiver on two different Arduinos with separate power supplies. Still the sensor and LEDs can't get close to one another.

What is this black magic?

BlueSails:
Even when holding the sensor in my closed fist where it can't see anything it still "detects" the LED when I bring my fist close to it.
What is this black magic?

Ever held a torch under the palm of your hand in complete darkness?
The light shines right through.
Magic!!!

You might have to put the 3-pin receiver in a tiny metal enclosure, grounded to the ground pin of the sensor, if shielding the light with small tubes on transmitter and receiver is not enough.

You have just discovered that more IR power could create more problems than solutions.
Leo..

So put the detector in a tube and the illuminators around and behind or in front and out of view from in the tube.

Roll up a piece of card to fit the detector bulb and tape it. Bulb inside will only detect what's in view of that hole.

The view of the detector should be narrowed to the target spot.
Does your IR reflect off what is supposed to block the beam? Reflected IR is just that.

I have mounted LEDs and sensor like this:

Do you really think it is light travelling from one to another?

Alright, I took it apart again and covered sensor and LEDs in Sugru like this

I'm glad to say that it now works although I have to admit I spent WAY to long trying many other solutions...

You don't need to restrict the leds, just the detector. I have simple 5mm black bulb 2 wire IR detectors, they can fit in pen barrels or roll your own tubes with 5mm inside dia. Tube inside made flat black works best. Ballpoint pen barrels make good DIY scanner apertures.