Counting persons coming in or going out of a Room / Building

We all are aware of Home Automation and love the idea of actions to be performed automatically in our Homes without hurting our Economy.

Have you ever wished that the lights should automatically switches on while entering the room and vice versa? Off course you do. Ummm, One might suggest using mobile phone presence for this event to occur? Yea, It’s a good idea but lets have a look on this option. We may use WiFi or Bluetooth but If We enter our living room and it may turn our bedroom lights on or off. And if we add specific WiFi access point or Bluetooth modules in all rooms? Let’s suppose We do practice this idea, It would be less accurate due to intermingled WiFi or Bluetooth signals. Moreover, not every person enters the room may have smart phone or If we forget our smart phone in our bedroom and go to living room then? Oops!.

Hey, If we register the entry or exit of a person in or out of the room, then We can make out system more accurate, is not it? So here it comes the idea of counting person. I am so sorry for heavy weight start 😛 , so lets get started  😉 .

Hardware Needed:

Hardware Connections:

GP2Y0A21YK0F Infrared Distance Sensor comes with connector which has three wires, Red, Black and Yellow. Connect Red wire to +5V, Black to Ground and Yellow wire goes to analog pin of Arduino Nano. Connect both of sensors to Arduino Nano on Analog Pin A0 and A1 as shown in picture above. Don’t forget to add decoupling capacitors of at least 10µF across sensor power supply somewhere near to the sensors to get stable reading out of the sensors.

Software:

A very basic sketch to have an idea to accomplish above mentioned task is as follows.

Door_Sensor.ino

boolean right_sensor_tripped = false;
boolean left_sensor_tripped = false;
unsigned long right_sensor_time = 0;
unsigned long left_sensor_time = 0;
int movement;

void setup() {
Serial.begin(115200);
Serial.println("System is booting");

// A test to know if your connection are working
//Just move your hand in front of each sensor to complete this test
//Check your serial debug to get the status

boolean right_sensor_test = false;
boolean left_sensor_test = false;
Serial.println("Performing sensor test");
while((!right_sensor_test) || (!left_sensor_test)) {
  int right_sensor = analogRead(A0);
  int left_sensor = analogRead(A1);
  if ((right_sensor > 500) && (!right_sensor_test)){
    Serial.println("Right ok");
    right_sensor_test = true;
  }
  if ((left_sensor > 500) && (!left_sensor_test)) {
    Serial.println("Left ok");
    left_sensor_test = true;
  }
}
Serial.println("Sensor is Ready");
}

void loop() {
  int right_sensor = 0;
  int left_sensor = 0;
  int mean_right_sensor;
  int mean_left_sensor;
  
  //Taking three reading average
  for (int i=0; i<3; i++) {
    int right_sensor_reading = (analogRead(A0) + right_sensor);
    right_sensor = right_sensor_reading;
  }
  mean_right_sensor = right_sensor / 3;

  for (int i=0; i<3; i++) {
    int left_sensor_reading = (analogRead(A1) + left_sensor);
    left_sensor = left_sensor_reading;
  }
  mean_left_sensor = left_sensor /3;

  //Change "300" with any value suitable for your need
  // Use Door_Sensor_Serial_Debug.ino to get suitable reading for your particular purpose
  //Install the sensor on the door
  //Adjust the value untill you have no output on serial debug
  //If an object is far than 3-4 feet of the sensor, you may get the value 500
  //That's why I suggest you to install the sensor on door
  //Delay of 1000 Ms is necessary for accuracy
  
  if((mean_right_sensor > 300) && ((millis() - right_sensor_time) > 1000)) {
    right_sensor_time = millis();
    right_sensor_tripped = true;
    if(!left_sensor_tripped) {
      movement = 0;
    }
  }
  if((mean_left_sensor > 500) && ((millis() - left_sensor_time) > 1000)) {
    left_sensor_time = millis();
    left_sensor_tripped = true;
    if(!right_sensor_tripped) {
      movement = 1;
    }
  }

  if ((right_sensor_tripped) && (left_sensor_tripped)) {
    if(movement == 0) {
      Serial.println("Right");
    } else if(movement == 1) {
      Serial.println("Left");
    }
    movement = 5;
    right_sensor_tripped = false;
    left_sensor_tripped = false;
    delay(1000);
  }
}

Door_Sensor_Serial_Debug.ino

void setup() {
  Serial.begin(115200);
  Serial.println("System is booting");

}

void loop() {
  //Printing the output value to serial debug
  Serial.println(analogRead(A0));
  delay(1000);

}

A more comprehensive and applicable version of this sensor is coming soon, Stay tuned!!

4 1 vote
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Denis
Denis
5 years ago

Hi. This is a very good idea.

Can you comment on the sensor performance? It seems that the post is two to three years old. Is it still working? Is it fast enough to count 3 to 4 people going through the door within a few seconds of each other?

1
0
Would love your thoughts, please comment.x
()
x