Hello guys, in this tutorial we are going to see how to make Bi-Directional visitor counter using single ultrasonic sensor with LCD. So let’s get started.

For this you will need

  1. Arduino,
  2. Ultrasonic sensor,
  3. LCD display 16X2,
  4. I2C,
  5. Breadboard,
  6. Jumper Wire.

We are using single ultrasonic sensor for bi-directional visitor counter. Within 9 cm range it will count how many visitor enter in the room.

Bidirectional counter entry

From 9 to 18cm range it will count how many visitor left from the room.

Bidirectional counter exit

You can also read my previous tutorial on bi-direction counter with seven segment LED.

Do connection as shown in diagram.

How to make Bi-Directional visitor counter using single ultrasonic sensor with LCD.

Sketch:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define trigPin 13
#define echoPin 12
// Find LCD address for I2C in this tutorial it is 0x3f
LiquidCrystal_I2C lcd(0x3f, 16, 2);
int counter = 0;
int currentState1 = 0;
int previousState1 = 0;
int currentState2 = 0;
int previousState2 = 0;
int inside = 0;
int outside = 0;

void setup()
{
// initialize the LCD
lcd.begin();
//Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop()
{
lcd.setCursor(0, 0);
lcd.print("IN: ");
lcd.setCursor(8, 0);
lcd.print("OUT: ");
lcd.setCursor(0, 1);
lcd.print("Total Inside: ");
long duration, distance;
digitalWrite(trigPin, LOW); 
delayMicroseconds(2); 
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); 
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 9){
currentState1 = 1;
}
else {
currentState1 = 0;
}
delay(100);
if(currentState1 != previousState1){
if(currentState1 == 1){
counter = counter + 1;}
lcd.setCursor(14, 1);
lcd.print(counter);
inside = inside +1;}
lcd.setCursor(4, 0);
lcd.print(inside);
if (distance > 9 && distance <= 18){
currentState2 = 1;
}
else {
currentState2 = 0;
}
delay(100);
if(currentState2 != previousState2){
if(currentState2 == 1){
counter = counter - 1;}
lcd.setCursor(14, 1);
lcd.print(counter);
outside = outside +1;}
lcd.setCursor(13, 0);
lcd.print(outside);
lcd.setCursor(14, 1);
lcd.print(counter);
if (counter > 9 || counter < 0){
lcd.setCursor(14, 1);
lcd.print(counter);
delay(100);
lcd.clear();
 }
}

Download library for I2C from Github  OR you can download it from here because github library may change by the time or it may not work.

Download l2C library Arduino-LiquidCrystal-I2C-library-master.zip

Explanation:

Now lets come to the programming part. Add I2C library and define ultrasonic pins.

If distance is less than 9 cm. counter will count 1, and “inside” which shows overall how many people enter in the room.

If distance is greater than 9 cm and less than 18 cm it will minus 1 from counter. And “outside” which shows overall how many people left from the room.

if (counter > 9 || counter < 0){
lcd.setCursor(14, 1);
lcd.print(counter);
delay(100);
lcd.clear();
}

If we don’t add this line. Character will stuck on LCD after counter value 9. We can see it counts perfectly entering and exiting object.

bidirectional counter using single ultrasonic sensor

Till then keep learning keep making.