Need help with beam breakers!

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Neuro_Yanko
 
Posts: 2
Joined: Sun May 22, 2022 11:23 pm

Need help with beam breakers!

Post by Neuro_Yanko »

Hey there! I've been trying a long while to make the beam breakers https://learn.adafruit.com/ir-breakbeam ... s/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

/* 
  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;
}

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: Need help with beam breakers!

Post by adafruit_support_bill »

Code: Select all

      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/

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

Re: Need help with beam breakers!

Post by Neuro_Yanko »

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

/* 
  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);
}

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: Need help with beam breakers!

Post by adafruit_support_bill »

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

  digitalWrite(SENSORPIN, HIGH); // turn on the pullup

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”