0

Need help with beam breakers!
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Need help with beam breakers!

by Neuro_Yanko on Sun May 22, 2022 11:36 pm

Hey there! I've been trying a long while to make the beam breakers https://learn.adafruit.com/ir-breakbeam-sensors/overview work but didn't succeed.
I use them with a feather NRF52832 board, red wires connected to 3.3 V pins, black wires to ground, a yellow wire to physical pin 4 (0.27). I use the following script and it loops at "print 1111" stage.
How can I bring it to "print unbroken" state?

Code: Select all | TOGGLE FULL SIZE
/*
  IR Breakbeam sensor demo!
*/

#define LEDPIN 31

#define SENSORPIN 4

// variables will change:
int sensorState = 0, lastState=0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(LEDPIN, OUTPUT);     
  // initialize the sensor pin as an input:
  pinMode(SENSORPIN, INPUT);     
  digitalWrite(SENSORPIN, HIGH); // turn on the pullup
 
  Serial.begin(9600);
}

void loop(){
  // read the state of the pushbutton value:
  sensorState = digitalRead(SENSORPIN);

  // check if the sensor beam is broken
  // if it is, the sensorState is LOW:
  if (sensorState == LOW) {     
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);
    Serial.println("1111");
  }
  else {
    // turn LED off:
    digitalWrite(LEDPIN, LOW);
    Serial.println("2222");
  }
 
  if (sensorState && !lastState) {
    Serial.println("Unbroken");
  }
  if (!sensorState && lastState) {
    Serial.println("Broken");
  }
  lastState = sensorState;
}

Neuro_Yanko
 
Posts: 2
Joined: Sun May 22, 2022 11:23 pm

Re: Need help with beam breakers!

by adafruit_support_bill on Mon May 23, 2022 7:17 am

Code: Select all | TOGGLE FULL SIZE
      pinMode(SENSORPIN, INPUT);     
      digitalWrite(SENSORPIN, HIGH); // turn on the pullup

This trick works with the original Atmega-based Arduinos. It does not work with some of the other processor architectures. Use INPUT_PULLUP in your pinmode statement instead.

https://www.arduino.cc/reference/en/lan ... o/pinmode/

adafruit_support_bill
 
Posts: 84020
Joined: Sat Feb 07, 2009 10:11 am

Re: Need help with beam breakers!

by Neuro_Yanko on Thu May 26, 2022 2:58 pm

Hello, thank you!
Unfortunately, it didn't help (I might behave done it wrong), could you please have a look at this?
Now it only prints "sensorState"

Code: Select all | TOGGLE FULL SIZE
/*
  IR Breakbeam sensor demo!
*/

#define LEDPIN 13
 
#define SENSORPIN 4

// variables will change:
int sensorState = 0, lastState=0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(LEDPIN, OUTPUT);     
  // initialize the sensor pin as an input:
  pinMode(SENSORPIN, INPUT_PULLUP);     
  digitalWrite(SENSORPIN, HIGH); // turn on the pullup
 
  Serial.begin(9600);
  sensorState = 0;
}

void loop(){
  // read the state of the pushbutton value:
  sensorState = digitalRead(SENSORPIN);

  // check if the sensor beam is broken
  // if it is, the sensorState is LOW:
  if (sensorState == LOW) {     
    // turn LED on:
    digitalWrite(LEDPIN, HIGH); 
    Serial.println(sensorState);
  }
  else {
    // turn LED off:
    digitalWrite(LEDPIN, LOW);
    Serial.println(sensorState);
  }
 
  if (sensorState && !lastState) {
    Serial.println("Unbroken");
  }
  if (!sensorState && lastState) {
    Serial.println("Broken");
  }
  lastState = sensorState;
  delay(1000);
}

Neuro_Yanko
 
Posts: 2
Joined: Sun May 22, 2022 11:23 pm

Re: Need help with beam breakers!

by adafruit_support_bill on Thu May 26, 2022 3:05 pm

You only need the pinMode command with INPUT_PULLUP. No need to explicitly set the pin HIGH.

With some processors, any write will set the pin back to output mode.

So delete this line:
Code: Select all | TOGGLE FULL SIZE
  digitalWrite(SENSORPIN, HIGH); // turn on the pullup

adafruit_support_bill
 
Posts: 84020
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.