IR Break Beam Sensors

Hello All.
I am working on a project with break beam sensors.
I have the sketch for one sensor (pair) and is working fine. However i need to use 12 sensors in my project, and i need help to edit the below sketch accordingly;

#define LEDPIN 4
  // Pin 4: Arduino has an LED connected on pin 4

  #define SENSORPIN 2

// 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);  
  } 
  else {
    // turn LED off:
    digitalWrite(LEDPIN, LOW); 
  }
  
  if (sensorState && !lastState) {
    Serial.println("Unbroken");
  } 
  if (!sensorState && lastState) {
    Serial.println("Broken");
  }
  lastState = sensorState;
}

Packe lastState und sensorState in eine struct und erstelle ein Array davon. Mit einem Beispiel kann ich leider nicht dienen, mir ist die C/C++ Syntax zu fremd.

Oder ein Array aus jeder Variablen:
int sensorState[12], lastState[12];
Dann alle Sensoren in einer Schleife abarbeiten.

Are you going to use 12 leds with the 12 sensors ?
You may have to deal with IR reflections causing spurious results. Possible solutions are either by the design of the physical enclosure or in code by switching the leds / sensors in sequence.

So what have you tried so far? Where are you stuck?

Before getting to code, have you thought about a plan for how to make this work?

Hello casinocip

Welcome to the worldbest Arduino forum ever.

Consider:

//https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
// make variables
constexpr uint8_t Inputs[] {A0,A1,A2};
constexpr uint8_t Outputs[] {9,10,11};
void setup()
{
  for (auto Input:Inputs) pinMode(Input,INPUT_PULLUP);
  for (auto Output:Outputs) pinMode(Output,OUTPUT);
}
void loop()
{
  uint8_t element=0;
  for (auto &Output:Outputs) digitalWrite(Output,digitalRead(Inputs[element++])?LOW:HIGH);
}

Have a nice day and enjoy coding in C++.

1 Like

Hello Awneil,

The code attached works and does what i need but is only for one input. I am stuck on adding 11 more sensors as input.
Basically what i am aiming here, is to have 12 beams around and object. Whenever one of the beams is interrupted, a relay is activated and trigger and alarm in the Milestone VMS.
Again, everything works as described but only with one beam

Yes, I got that.

So what have you tried so far in extending it to 12 sensors?
Where are you stuck in extending it to 12 sensors?

Before getting to code, have you thought about a plan for how to make this work?

Coding should be the final part - before you code, you need a design...

The what??

IR radiation is emitted in a wide angle and is heavily reflected by many materials, as you may know from your TV... remote control. Breaking one such "beam" may be undetected.

Thank you for the feedback DrDiettrich. I will place the sensors 8cm apart (as required in my project) and see if there are any interferences. Then i can test the beams one by one with the code i have. Just don't know enough about coding to enable 12 different input pins

Again, before getting to coding, you need to have a design - a plan - for how you are going to do this.
So what is your plan?

Start with the design (plan), then think about how to implement that in code...

Thank you!

Hello again,
After taking in consideration your advices and reading all related posts, i managed to figure this out for the most part.
All sensors working and detecting. The only inconvenience now is: If a beam is interrupted, the serial monitor is continuously displaying the "Broken" message. In my first code it would only print the message once.

int SENSORPIN [12] = {2,3,4,5,6,7,8,9,10,11,12,13};

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

void setup() {
  for (int eachSensor = 0; eachSensor <= 11; eachSensor++) {
 pinMode(SENSORPIN [eachSensor] ,INPUT_PULLUP);
  }
digitalWrite(SENSORPIN, HIGH); // turn on the pullup

 Serial.begin(9600);

  }

void loop() {
  // check the sensor array.
  for(int s = 0; s <= 11; s++) { 
   
    sensorState = digitalRead(SENSORPIN[s]);
 
  // if it is, the sensorState is LOW:
  if (sensorState == LOW) {  
  }    
   
  if (!sensorState && lastState) {
    Serial.println("Broken ,");
  }
  lastState = sensorState;}
}


You have to have multiple lastState variables, one for each pin. It is best to use and array here.

1 Like

Hi 6v6gt,
Can you please give me a hint on how to do this? This is literally the first time i am using Arduino

Of course (untested) :

int SENSORPIN [12] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int lastState[ 12 ] = { 0 } ;

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

void setup() {
  for (int eachSensor = 0; eachSensor <= 11; eachSensor++) {
    pinMode(SENSORPIN [eachSensor] , INPUT_PULLUP);
  }
  //  digitalWrite(SENSORPIN, HIGH); // turn on the pullup // redundant

  Serial.begin(9600);
}

void loop() {
  // check the sensor array.
  for (int s = 0; s <= 11; s++) {   // please use < 12 instead of <= 11
     // yes, it has the same effect but much nicer to look at when the array
     // is specified as arrayXX[ 12 ] 
	 
    sensorState = digitalRead(SENSORPIN[s]);

    // if it is, the sensorState is LOW:
    if (sensorState == LOW) {
		// this isn't doing much
    }

    if (!sensorState && lastState[ s ] ) {
      Serial.println("Broken ,");
    }
    lastState[ s ] = sensorState;
  }
}

Rather than having 12 as a Magic Number appearing in multiple places, you could (should?) make it a constant; eg,

#define NUMBER_OF_SENSORS 12
int SENSORPIN [NUMBER_OF_SENSORS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int lastState [NUMBER_OF_SENSORS] = { 0 } ;

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

void setup() {
  for (int eachSensor = 0; eachSensor < NUMBER_OF_SENSORS; eachSensor++) {
    pinMode(SENSORPIN [eachSensor] , INPUT_PULLUP);
  }
  //  digitalWrite(SENSORPIN, HIGH); // turn on the pullup // redundant

  Serial.begin(9600);
}

void loop() {
  // check the sensor array.
  for (int s = 0; s < NUMBER_OF_SENSORS; s++) {  

  :
  :

EDIT

Note also that it's conventional to reserve ALL UPPERCASE names for #defines - so:

#define NUMBER_OF_SENSORS 12
int sensor_pin[NUMBER_OF_SENSORS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int lastState [NUMBER_OF_SENSORS] = { 0 } ;

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

void setup() {
  for (int eachSensor = 0; eachSensor < NUMBER_OF_SENSORS; eachSensor++) {
    pinMode( sensor_pin[eachSensor] , INPUT_PULLUP );
  }
  //  digitalWrite( sensor_pin, HIGH ); // turn on the pullup // redundant

  Serial.begin(9600);
}

void loop() {
  // check the sensor array.
  for (int s = 0; s < NUMBER_OF_SENSORS; s++) {  

  :
  :
1 Like

Thank you so much! It works :slightly_smiling_face:

Thank you Awneil! Tested your suggestion and this also works as intended :pray:

functionally it's identical to @6v6gt's - just a couple of style changes.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.