Two PIR sensors to count the movement

I am trying to make a project where two PIR sensors will be place at a distance of 1 feet. When movement occurs the first PIR sensors to catch the movement and second one two remain off at that time. The opposite will happen when the second one will catch at first.

Is there any way to do this with out a multiplexer ?

I have connected the input pins from the two sensors to digital pins. Until now they are working fine. Except during the count both remain active and they detect the motion together.

It would be great if you suggest me a tweak in code .

If I have to use multiplexer then can u please help me with the code and connection.

Thanks in Advance.

Here is the code:

[code]int ledPin = 13;                // choose the pin for the LED A
int inputPinA = 2;               // choose the input pin (for PIR sensor A)
int pirStateA = LOW;             // we start, assuming no motion detected by A
int valA = 0;                    // variable for reading the pin status for A

int inputPinB = 3;               // choose the input pin (for PIR sensor B)
int pirStateB = LOW;             // we start, assuming no motion detected by B
int valB = 0;                    // variable for reading the pin status for B

int count = 0;

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPinA, INPUT);     // declare sensor as input
  pinMode(inputPinB, INPUT);     // declare sensor as input

  Serial.begin(9600);
  
}

void loop() {
OptimusPrime: 
/// Program for sensor A///

   valA = digitalRead(inputPinA);  // read input value
  if (valA == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);
    if (pirStateA == LOW) {
       Serial.println("People Have Entered");
       count ++;
       Serial.println("Number of people:");
      Serial.println(count);
      // we have just turned on
      // We only want to print on the output change, not state
      pirStateA = HIGH;
    }
  } 

   else {
    //digitalWrite(ledPin, LOW); // now led remain on
    if (pirStateA == HIGH){
      // we have just turned of
      Serial.println(" A Sensor ended!");
     
      // We only want to print on the output change, not state
      pirStateA = LOW;
    }

  }

///Program for sensor B ///

 valB = digitalRead(inputPinB);  // read input value
  if (valB == HIGH) {            // check if the input is HIGH
    if (pirStateB == LOW) {
       Serial.println("People Left");
       count -- ;
       Serial.println("Number of people remained:");
      Serial.println(count);
      // we have just turned on
      // We only want to print on the output change, not state
      pirStateB = HIGH;
    }
  } 
   else {
    if (pirStateB == HIGH){
      // we have just turned of
      Serial.println(" B Sensor ended!");
      pirStateB = LOW;
    }
  }

///Program Counter///

 if (count <= 0)
 {digitalWrite(ledPin, LOW);}

 else{
  digitalWrite(ledPin, HIGH);
 }
 
}

[/code]

test7.ino (2.2 KB)

Is there any way to do this with out a multiplexer ?

Yes. In fact I can't see how you could use a multiplexer.

The problem I assume you are trying to solve is to count people in and out of a room / door. Understand that this is a very old problem that it not easily solved basically because people do not behave constantly. They cross in the doorway, they come part way in and then go out without triggering the second sensor.

Basically you have to ignore the second sensor once the first has been triggered until the first sensor is no longer being triggered. Then you have to delay until the second sensor has stopped being triggered. Only then can you count one. But as I said it doesn't always work.

If sensor A is triggered, you want to ignore any input from sensor B for a certain period of time.
If sensor B is triggered, you want to ignore any input from sensor A for a certain period of time.
Is that it ?
How long do you wish to wait before allowing input from the "other" sensor ?

Edit:
Grumpy Mike got there first-

@sakib501, please do not cross-post. Other thread removed.

Hi,
Any reason you need to use PIR, why not IR beam and reflector.
I find PIR to be slow.

Tom... :slight_smile:
Have you setup a couple of PIRs insitu and see how they perform by observing their signal lights?