DIY Bi-Directional Counter Using Arduino & IR Sensors

by Yogeshwaran in Circuits > Arduino

50 Views, 0 Favorites, 0 Comments

DIY Bi-Directional Counter Using Arduino & IR Sensors

(Bi-Directional Counter) 600x500.png

Have you ever wondered how malls, parking lots, or event venues keep track of people or cars? You might think they use fancy AI-powered cameras, but in most cases, a simple sensor-based system does the job efficiently!

By placing IR sensors at an entry/exit point, the system can detect movement direction and accurately count how many people or objects pass through, No need for complex technology.

Want to Build Your Own Bidirectional Counter?

With just an Arduino and a pair of IR sensors, you can create a real-time counter that:

  1. Tracks people entering and exiting.
  2. Displays the current count and history of count on an LCD screen.

What You’ll Learn:

  1. How to use IR sensors for motion detection.
  2. Simple hardware and coding tricks to make it work smoothly.


Ready to Get Started?

Follow our, Bidirectional Visitor Counter using Arduino tutorial to get know more details.

Supplies

Components.jpg

Here is the list of components needed to build a Bi-Directional Counter Project.

  1. Arduino UNO R3 board
  2. Two IR sensors
  3. 16x2 I2C LCD display
  4. Breadboard
  5. Connecting wires
  6. 12V barrel jack power adapter

Tip:

Before jumping into the circuit diagram, it’s good to first understand, Block diagram of Bi-directional visitor counter. This will help you grasp how the system works more easily.

Designing the Circuit for the Bidirectional Counter

Circuit-Diagram.jpg

Here’s the circuit diagram for the Bidirectional Counter using Arduino. It shows how the Arduino, two IR sensors, and the LCD display are connected to form a working system.

Why Two IR Sensors?

Using two sensors is necessary to track direction accurately. If you only use one sensor, you won’t be able to tell whether a person is entering or exiting.

Still Confused?

No problem! Check out this detailed guide on "How does IR Sensor detect the direction of Visitor's motion". It explains the concept in a simple and clear way, perfect for beginners!

Building the Hardware Setup

Parts-Marking.jpg
How to Build Bidirectional Visitor Counter using Arduino and IR Sensors | DIY People Counter Project

The image above shows the hardware setup of the Bidirectional Counter using Arduino and IR sensors, based on the circuit diagram.

Power Supply:

To power the entire setup, I used a 12V DC adapter connected through a barrel jack.

Need More Help?

Check out this Youtube video to build a Bi-directional Counter using Arduino.

Reduce Interference With an IR Sensor Hack

IR sensor.jpg

When using IR sensors in your Bidirectional Counter, they might sometimes pick up unwanted movements from the sides, causing incorrect counts. You can fix this with a simple trick.

How to Do It:

  1. Cut a small piece of black electrical tape.
  2. Stick the tape around the sides of the IR sensor.
  3. Make sure to leave the front open for proper detection.

How It Works:

The black tape blocks signals from the sides, preventing interference from nearby objects or reflections. This makes the sensor more accurate and reliable by only detecting movement directly in front of it.

If you want to know more hack tips, check out this, Tips to avoid false trigger on IR sensor due to environmental conditions.

Write the Code for Your Arduino

Now, let’s move on to the coding part!

#include <LiquidCrystal_I2C.h>
// Initialize the LCD with I2C address 0x27
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int irPin1 = 2; // IR sensor 1(Entrance Detector) connected to digital pin 2
const int irPin2 = 3; // IR sensor 2(Exit Detector) connected to digital pin 3
int in_count = 0; // Variable to store the number of people went inside
int out_count = 0; // Variable to store the number of people went outside
int current_count = 0; // Variable to store the current number of people present inside
const unsigned long timeout = 50; // Object detection Timeout period in milliseconds
void setup() {
/* Initialise LCD Display */
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("IN: 0 OUT: 0");
lcd.setCursor(0, 1);
lcd.print("Current: 0");
/* Pull up the input pins */
pinMode(irPin1, INPUT_PULLUP);
pinMode(irPin2, INPUT_PULLUP);
}
void loop() {
/* Check if the first sensor is triggered */
if (digitalRead(irPin1) == LOW) {
unsigned long startTime = millis();
while ((millis() - startTime) < timeout) {
if(digitalRead(irPin2) == LOW){
++in_count;
updateDisplay();
break;
}
}
//wait until both sensors return to a normal state
while(!digitalRead(irPin1) || !digitalRead(irPin2));
}
/* Check if the second sensor is triggered */
else if (digitalRead(irPin2) == LOW) {
unsigned long startTime = millis();
while ((millis() - startTime) < timeout) {
if(digitalRead(irPin1) == LOW){
if(out_count < in_count){
++out_count;
updateDisplay();
break;
}
}
}
//wait until both sensors return to a normal state
while(!digitalRead(irPin1) || !digitalRead(irPin2));
}
}
void updateDisplay() {
lcd.setCursor(4, 0);
lcd.print(" ");
lcd.setCursor(4, 0);
lcd.print(in_count);
lcd.setCursor(13, 0);
lcd.print(" ");
lcd.setCursor(13, 0);
lcd.print(out_count);
current_count = in_count - out_count;
lcd.setCursor(9, 1);
lcd.print(" ");
lcd.setCursor(9, 1);
lcd.print(current_count);
}

How the Code Works:

  1. The Arduino keeps checking both IR sensors to see which one is triggered first.
  2. It then verifies if the second sensor is triggered within a set time limit.
  3. Based on the order of triggers, it decides if someone is entering or exiting.
  4. The visitor count is updated and shown on the LCD screen.

Want a Detailed Code Breakdown?

For a complete step-by-step code explanation, Feel free to watch out this, Arduino code for Bidirectional Counter article.

Upload the Code to Your Arduino

board select.jpg
upload.jpg

To get your Bidirectional Counter working, you need to upload the code to the Arduino.

How to Do It:

  1. Open the Arduino IDE on your computer.
  2. Connect the Arduino board using a USB cable.
  3. Select the correct board and COM port in the IDE.
  4. Click the Upload button to send the code to the board.

Having Trouble Uploading?

If you face issues, check out this Article "10 most Common mistakes while using Arduino" to troubleshoot the problem.

Test Your Build

download-0000.jpg

Now comes the most exciting part, testing your project!

What to Expect:

  1. If everything is connected and coded properly, you’ll see the Bidirectional Counter in action.
  2. The display will show the current number of people or objects inside.
  3. It will also keep track of the entry and exit history, showing how many have gone in and out.

Want to See It in Action?

Watch this ,Arduino based Bidirectional Counter working Demonstration


Want to discover more cool projects? Dive into our collection of Arduino IoT Projects | Arduino Robotics Projects | Arduino AI Projects | Arduino Home Automation Projects | Raspberry Pi Projects | ESP32 Projects to inspire your next build!