IR Break Beam sensor Issue

Hi,

I'm having an issue with the operation of an IR break beam sensor which I eventually want to move a servo in a material sorting project. To verify correct functionality I'm currently using the on-board LED.

The problem is when there is nothing between the IR emitter and the receiver, the LED doesn't blink (as required). When I move my hand between the emitter and the receiver the LED still doesn't blink, it only blinks when I move my hand away. I need it to blink when the beam is broken (when my hand is between emitter and receiver).

I've changed around the arrangements of whether the sensor state is HIGH or LOW at the start of the loop with no success. I've also done extensive search's online and through the forum for a solution but haven't been able to find an answer.

I'm using an Arduino Uno, the IR emitter is an LED Mfr. P/N TSHA5201 and the receiver is Mfr. P/N TSOP4840.

const int LEDPIN = 13;
const int SENSORPIN = 2;
 
int sensorState = 0, lastState=0;        
 
void setup() {
 
  pinMode(LEDPIN, OUTPUT);      
  pinMode(SENSORPIN, INPUT);     
  digitalWrite(SENSORPIN, HIGH); 
  Serial.begin(9600);
}
 
void loop(){
  sensorState = digitalRead(SENSORPIN);
  
  if (sensorState == LOW) {     
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(LEDPIN, LOW); 
  }
  
  if (sensorState && !lastState) {
    Serial.println("Unbroken");
  } 
  if (!sensorState && lastState) {
    Serial.println("Broken");
  }
  lastState = sensorState;
}

Apologies if the answer is quite simple and a result of my lack of knowledge.
Any help would be greatly appreciated.

The following line enables the internal pullup for the input. I'm not sure you need that with this device. I don't know for sure but it is worth a try to remove it.

digitalWrite(SENSORPIN, HIGH);

It's a simple matter of sensor polarity. Change

  if (sensorState == LOW) {

to

  if (sensorState == HIGH) {

aarg:
It's a simple matter of sensor polarity. Change

Try aarg's suggestion first. I haven't used one of these devices.

Pseudo code:

if(beam_blocked)
   digitalWrite(LEDPIN,bitRead(millis(),7)); // or 8
else
   digitalWrite(LEDPIN,LOW);

Thanks everybody for getting back to me. Unfortunately I still haven't got it to do what I wanted.

ToddL1962 - You were right I removed this line and it still functioned the same as before. So that line was
not needed but it didn't change anything either.

aarg - Didn't work either I'm Afraid. The only difference is the LED turns on at the beginning and
remains on throughout.

JCA34F - No luck either I'm afraid.

I just don't understand why the state only changes to broken when my hand moves away. I've tried using a different sketch where the sensor is treated like a switch and displays a 1 or 0 on the serial monitor. Again the monitor is normally displaying 1. When i put my hand between emitter and receiver, still 1 and then blinks 0 when i move my hand away. I just don't know, I'm suspecting it might be a hardware issue.

Thanks again, really appreciate your reply's and value your feedback.

I'm using an Arduino Uno, the IR emitter is an LED Mfr. P/N TSHA5201 and the receiver is Mfr. P/N TSOP4840.

It appears you have a constant source transmitter, and you are using a modulated burst mode receiver. According to the data sheet, the receiver will suppress a continuous signal.

I think you would do better with an IR photo transistor matched to your sender, or else a receiver more designed for beam break sensing, without the AGC, like the TSSP4038.

https://www.vishay.com/docs/82458/tssp40.pdf

cattledog- sounds like that could be the solution alright. I will get my hands on a TSSP4038 and an IR
photo transistor matched to the sender (may as well try both instead of one failing and waiting
for another to be shipped) and report back.

Thank you very much.