In this project, let’s see how to make a simple bidirectional people counter using IR Sensors and Arduino Uno. This project will help learner to understand the working and application of IR sensor. The IR sensor is simple and powerful with wide variety of applications. This project will also help the user to understand logic and program Arduino Uno.
How Arduino Bidirectional People Counter works?
The project uses 2 IR sensors to detect when people are entering the room or exiting the room. The 2 IR sensors are placed at the door rim or at entrance of any room. The project says whether people are entering the room or exiting along with the exact people count in the room.
Components Required
You can find the following components at Quartzcomponents.com
- Arduino Uno (with cable) x1
- IR sensor Module x2
- Breadboard x1
- Jumper Cables (male to male)
Working of IR Sensor
IR sensor consists of two parts: IR transmitter and IR receiver. IR transmitter is also called as IR LED and IR receiver is also called as Photodiode. As the name indicates, IR transmitter sends IR rays and when the rays hit any obstacle and reflect back, that is captured by the receiver. The resistance of the Photodiode will vary according the reception of reflected IR rays. This will result in voltage drop. This voltage variation is made use by the Arduino Uno. In a nutshell, if any object blocks the IR sensor, the voltage across DATA pin will be close to GND pin. If there’s no block in the IR rays path, the voltage will be close to 5V. This property is made use in the coding.
Circuit Connections for Bidirectional People Counter
The circuit Connections for the following project are as shown in the circuit diagram. Connect VCC pin of both IR sensors to 5V pin of Arduino Uno. Connect GND pin of both IR sensors to GND pin of Arduino Uno. Finally, Connect DATA pins of both IR sensors to Arduino pins 7 and 8 respectively.
Code Explanation
int irPin1=7;
int irPin2=8;
This initializes the Arduino pins 7 and 8.
int count=0;
boolean state1 = true;
boolean state2 = true;
boolean insideState = false;
boolean outsideIr=false;
boolean isPeopleExiting=false;
int i=1;
void setup() {
Serial.begin(9600);
pinMode(irPin1, INPUT);
pinMode(irPin2, INPUT);
}
Baud rate is set to 9600 and Serial.begin function is used to display the output in the serial monitor. And both the IR pins are declared as input.
void loop() {
if (!digitalRead(irPin1) && i==1 && state1){
outsideIr=true;
delay(100);
i++;
state1 = false;
}
In void loop, this part of the code checks if the first IR sensor is blocked by the user (if it’s is, then the voltage across the DATA pin will be close to 0 or Digital LOW). If it is, then the i value is incremented by 1. This is done to keep tack if someone is trying to enter the room or exit the room.
else if (!digitalRead(irPin2) && i==2 && state2){
Serial.println("Entering inside the room");
outsideIr=true;
delay(100);
i = 1 ;
count++;
Serial.print("No. of people inside room: ");
Serial.println(count);
state2 = false;
}
This part of the code checks if the second IR sensor is blocked after the first one was blocked. If that is the case, then the count is incremented by 1 and it says that a person is entering the room and also prints the number of people in the room.
else if (!digitalRead(irPin2) && i==1 && state2 ){
outsideIr=true;
delay(100);
i = 2 ;
state2 = false;
}
This part of the code reads the data from IR sensor 2 to check if it’s blocked first. This changes the i value to 2, which indicates that someone is exiting the room.
else if (!digitalRead(irPin1) && i==2 && state1 ){
Serial.println("Exiting from room");
outsideIr=true;
delay(100);
count--;
Serial.print("No. of people inside room: ");
Serial.println(count);
i = 1;
state1 = false;
}
This part of the code checks if IR sensor 1 is blocked after IR sensor 2 being blocked. If that’s the case, then it decrements the counter and displays that someone has exited the room along with the people count.
if (digitalRead(irPin1)){
state1 = true;
}
if (digitalRead(irPin2)){
state2 = true;
}
These two if statements are executed when the IR sensor is not blocked.
Complete Code
int irPin1=7; int irPin2=8; int count=0; boolean state1 = true; boolean state2 = true; boolean insideState = false; boolean outsideIr=false; boolean isPeopleExiting=false; int i=1; void setup() { Serial.begin(9600); pinMode(irPin1, INPUT); pinMode(irPin2, INPUT); } void loop() { if (!digitalRead(irPin1) && i==1 && state1){ outsideIr=true; delay(100); i++; state1 = false; } else if (!digitalRead(irPin2) && i==2 && state2){ Serial.println("Entering inside the room"); outsideIr=true; delay(100); i = 1 ; count++; Serial.print("No. of people inside room: "); Serial.println(count); state2 = false; } else if (!digitalRead(irPin2) && i==1 && state2 ){ outsideIr=true; delay(100); i = 2 ; state2 = false; } else if (!digitalRead(irPin1) && i==2 && state1 ){ Serial.println("Exiting from room"); outsideIr=true; delay(100); count--; Serial.print("No. of people inside room: "); Serial.println(count); i = 1; state1 = false; } if (digitalRead(irPin1)){ state1 = true; } if (digitalRead(irPin2)){ state2 = true; } }
Output of the Project
The visitor counter project counts the number of people in the number of people in the room along with making a note of how many people entered and exited the room.
Conclusion
This project enables the learner to have fun with IR sensor and understand its working. Develop logic to count the number of people entering, exiting a room. Learning to code Arduino using Arduino IDE for a beginner is always a plus point! It’s a functional and fun project.
For more such projects, visit Quartzcomponents.com