PIR sensors to count people

I'm trying to make a project where two PIR sensors will be placed 15 cm apart (bounded by a wall). When people enter the room, their movements will be detected by the first PIR sensor and then the second PIR sensor. When I try it, if the reading of the first sensor is high then the second sensor is low then the detection will return to first sensor. The program I made is like this:

#include <Adafruit_MLX90614.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

// Temperature Sensor MLX90614
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
const byte degree_sym = B11011111;

// Ultrasonic Sensor HC-SR04
int trigPin = 8;
int echoPin = 9;
float duration;
float distance;

// PIR Sensor
int pir1 = 7;
int pir2 = 6;
int N_PIR1 = 0;
int N_PIR2 = 0;
int K_PIR1 = LOW;
int K_PIR2 = LOW;
int visitors = 0;
int max_visitors = 10;

long x = 0;

// IR Sensor
int varIR = 0;
const int pinIR = 4;

// LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  mlx.begin();
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  if (visitors < 1) {
    visitors = 0;
  }
  condition1();
  ultrasonic();
  if(distance <= 10) {
    if(visitors < max_visitors) {
      temp_check(); 
    }
    else {
      lcd.clear();
      condition2();
    }
  }
  IR_check();
}

  void condition1() {                   
    lcd.setCursor(0,0);
    lcd.print("Please Check");
    lcd.setCursor(0,1);
    lcd.print("Your Body Temp"); 
  }

   void condition2() {
    lcd.setCursor(0,0);
    lcd.print("The Room");
    lcd.setCursor(0,1);
    lcd.print("is Full");
    delay(2000);
    lcd.clear(); 
  }
  
  void ultrasonic() {
    digitalWrite(trigPin, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration/2)/29.1;
    Serial.print(distance);
    Serial.print(" cm      ");
    Serial.print(duration);
    Serial.print(" ms");
    Serial.println();
  }

  void temp_check() {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Temp Checking");
    delay(2000);
    lcd.clear();
    float body_temp = mlx.readObjectTempC();
    Serial.print(body_temp);
    Serial.print(degree_sym);
    Serial.print("C");
    Serial.println();
    lcd.setCursor(0,0);
    lcd.print("Temp : ");
    lcd.print(body_temp);
    lcd.write(degree_sym);
    lcd.print("C");
    delay(1000);
    lcd.clear();
    if(body_temp<25.50){
      lcd.setCursor(0,0);
      lcd.print("Check Temperature");
      lcd.setCursor(0,1);
      lcd.print("Again");
      delay(2000);
    }
    if(body_temp>25.49&&body_temp<37.51){
      lcd.setCursor(0,0);
      lcd.print("Normal Temp");
      lcd.setCursor(0,1);
      lcd.print("WELCOME");
      for(x = 0; x < 500;x ++) {
        visitors_in();
      }
      delay(3000);
    }
    if(body_temp>37.50) {
      lcd.setCursor(0,0);
      lcd.print("High Temp");
      lcd.setCursor(0,1);
      lcd.print("No Entry");
      delay(3000);
    }
    lcd.clear();
  }
  
  void visitors_in() {
    N_PIR1 = digitalRead(pir1);
    Serial.println("PIR1");
    Serial.println(N_PIR1);
    if(N_PIR1 == HIGH) {
      if(K_PIR1 == LOW) {
        Serial.println("Motion Detected");
        N_PIR2 = digitalRead(pir2);
        Serial.println("PIR2");
        Serial.println(N_PIR2);
        if(N_PIR2 == HIGH) {
          if(K_PIR2 == LOW) {
            Serial.println("Visitors In");
            visitors++;
            Serial.println("Visitors : ");
            Serial.println(visitors);
            delay(3000);
            x = 600;
            N_PIR1 = 0;
          }
        }
      }
    }
    else{
      if(K_PIR1 == HIGH) {
        Serial.println("Motion Stopped");
        K_PIR1 = LOW;
        if(K_PIR2 == HIGH) {
          Serial.println("Motion Stopped");
          K_PIR2 = LOW;
        }
      }
    }
  }  
  
  void visitors_out() {
    delay(2000);
    N_PIR2 = digitalRead(pir2);
    Serial.println("PIR2");
    Serial.println(N_PIR2);
    if(N_PIR2 == HIGH) {
      delay(100);
      if(K_PIR2 == LOW) {
        Serial.println("Motion detected");
        N_PIR1 = digitalRead(pir1);
        delay(3000);
        Serial.println("PIR1");
        Serial.println(N_PIR1);
        if(N_PIR1 == HIGH) {
          if(K_PIR1 == LOW) {
            Serial.println("Visitors Out");
            visitors--;
            Serial.println("Visitors : ");
            Serial.println(visitors);
            delay(3000);
            x = 600;
            N_PIR2 = 0;
          }
        }
      }
    }
    else{
      if(K_PIR2 == HIGH) {
        Serial.println("Motion Stopped");
        K_PIR2 = LOW;
        if(K_PIR1 == HIGH) {
          Serial.println("Motion Stopped");
          K_PIR1 = LOW;
        }
      }
    }
  }  
  
  void IR_check() {
    varIR = digitalRead(pinIR);
    delay(200);
    if(varIR == LOW) {
      Serial.println("Door Open");
      for(x = 0; x < 400;x ++) {
        visitors_out();
      }
    }
  }

How to program the second sensor to continue detecting (without returning to the first sensor) for a certain time?

Welcome to the forum

I have not looked at the sketch in detail but it sounds like you need to detect when a PIR becomes triggered rather than when one is triggered. See the StateChangeDetection example in the IDE

you haven't even declared the pin of PIR in void setup.

Did you mean "didn't set the pinMode in the setup function"?

I wouldn't depend on it (I haven't seen it documented in a reference) but pins are inputs by default.

Hi @rubyy10 ,

welcome to the arduino-forum.
Well done posting your code as a code-section in your first posting.

It is pretty normal that a newcomer simply starts writing code or modifying code from something found in the internet. You have shown own effort with this.

You will proceed faster towards finishing your project if you take the following advice to your heart.

You should start with a small testcode that does nothing more than detecting a single PIR-sensr and build up from there.

You might think that you want to continue with the code you have posted, because "it is almost working"

So here is what I want to say in very clear words to be effective:
The code you have posted is a big mess under multiple aspects.
In your description you write about two PIR-sensors and that's it.

The code you have posted contains a lot of lines related to an ulrasonic sensor.
This means: either you didn't write that you are additionally using an ultrasonic sensor
or

you did take some code from somewhere without understanding which line of code does what adding your PIR-code to it

or
you took this code because the code works with an LC-Display which you have too and you were too lazy to delete the ultrasonic-sensor code

I'm pretty sure if you google for "Arduino PIR-Sensor" that you will find code especcially for that.

Again: You are welcome to the forum. You will finish your project faster if you take this advice to your heart and start new from scratch or with a way smaller code for one single PIR-sensor and work up from there.

best regards Stefan

Oh I was unaware of that.

Still there are more errors like

The K_PIR1 and K_PIR2 will always be low.

What are those (k_pir) for ?

I can see that pir1 and pir2 pins are not declared in the setup function. These two pins should be declared

pinMode(pir1,INPUT);
pinMode(pir2,INPUT);

Maybe there are many other things missing in the code. These two caught my eyes.

By the way, here is another visitor counter project that you may take a look at:

Before you start trying to code a people counter based on these sensors, I recommend that you get to know them a little bit.

Wire the sensors and some LEDs on output pins (with series current limiting resistors of course) and…

..simply read the sensors, turn off or on an LED for each sensor showing what it is saying in the moment.

You may find that the raw data from the sensors is a bit of a jumble, and may present a challenge for "decoding" down to an ability to count people or other warm moving things.

void loop()
{
    digitalWrite(ledPinA, digitalRead(pirSensorA));
    digitalWrite(ledPinB, digitalRead(pirSensorB));
}

Of course you could do that with transistors and a few resistors, no Arduino, but a sketch is easier, and allows you to confirm your thinking and wiring.

HTH

a7

There would be no point declaring them in setup, because then they'd be local to setup, and so not usable elsewhere.

This part of the code is not the declaration. It is the configuration of IO-pins as input

The declarations are the lines

// PIR Sensor
int pir1 = 7;
int pir2 = 6;

best regards Stefan

I meant to say declaring as input or output. I could not put my words properly. The exact word is 'configuring as input or output'. StephanL38 made it clear.

Thanks a lot. These are the exact words. This is what I was trying to say. It is not written anywhere in the code whether pir1 and pir2 are input pins or output pins.

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