digitalRead() always 1 with IR Breakbeam Sensor

Hello, I have an IR Breakbeam Sensor and am attempting to create a simple sketch to detect when the beam has been broken. I'm following the tutorial here https://learn.adafruit.com/ir-breakbeam-sensors/arduino (though this is not where I got my sensor from). digitalRead() returns 1 all the time and I am not sure why. I can confirm with my cellphone camera that the transmitter is working. I am not sure how to verify the receiver works. I've tried different pins. I've tried pinMode() of INPUT and INPUT_PULLUP. I've powered the sensor with both 3.3V and 5V. I've made sure the sensors are properly aligned and within range of each other (even tried them touching). Any tips on this? Below is the complete sketch:

Code:
const int LEDPIN = 13;
const int SENSORPIN = 6;

void setup() {
  pinMode(LEDPIN, OUTPUT);      
  pinMode(SENSORPIN, INPUT_PULLUP);     
  digitalWrite(SENSORPIN, HIGH);
  Serial.begin(9600);
}

void loop() {
  if(digitalRead(SENSORPIN)) {
    Serial.println("Broken");
    digitalWrite(LEDPIN, HIGH);
  } else {
    //never gets here
    Serial.println("Unbroken");
    digitalWrite(LEDPIN, LOW);
  }
  delay(1000);
}
Thanks
 
How long is the break in the beam?

This line: delay(1000);

Will only allow one INSTANT check every second.

The SENSORPIN is set PULLUP - so that assume the sensor when triggered will drop the pin to LOW?

One quick test rewrite would be:
Code:
void loop() {
  if(digitalRead(SENSORPIN)) {
    Serial.println("Broken");
    digitalWrite(LEDPIN, HIGH);
    delay(1000);
  } else {
    //never gets here
    Serial.println("Unbroken");
    digitalWrite(LEDPIN, LOW);
    delay(1000);
  }
}
 
Hello defragster. The break in the beam was long enough for me to see the Serial Monitor update several times with "Broken". When I attempted to break the bean, I placed an object in the way or even moved the sensors so they weren't facing each other. When I attempted to have an uninterrupted beam, I set the sensors close together (and even touching at one point). I never got an "Unbroken" prompt.

You are correct that when the beam is broken, it will trigger HIGH and when it is unbroken it will trigger LOW.

I tried your suggested code but resulted in the same output and didn't detect when the beam was unbroken.

For a little more info. I am using a teensy 4.0 and power it via USB. I have the IR beam transmitter and receiver connected to the power rail on the breadboard and then the white wire of the receiver connected to pin 6 on teensy. Here is the wiring diagram of the sensor. Untitled.png
 
Last edited:
I was able to do further testing. when the beam is broken, the white line has about 3.3 volts. When the beam is not broken, it drops to 0 volts. So I am able to confirm that the sensor works, my problem is that teensy is not detecting the change. I'm confused at this point and it seems like I must be missing something obvious.
 
I assume that you have a common ground between the teensy and the sensor.
Did you try using a different pin for the sensor input? You haven't said which teensy model you are using. Not all teensy models are 5V tolerant and you may have fried the pin when you were testing the sensor with 5V.
 
Hello thebigg. I am using teensy 4.0. My common ground had become disconnected on the breadboard. Gosh, I can't believe I overlooked that. Thank you both for taking the time to review my post here.
 
Back
Top