Arduino

Bi-Directional Visitor Counter using single ultrasonic sensor with LCD

Bi-Directional Visitor Counter using single ultrasonic sensor with LCD

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,
  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.

From 9 to 18cm range it will count how many visitor left from the room. You can also watch my previous video on bi-direction counter with seven segment LED.

Do connection as shown in diagram.

Bi-Directional Visitor Counter using single ultrasonic sensor with LCD
Circuit for Bi-Directional Visitor Counter using single ultrasonic sensor with LCD
skecth for Bi-directional visitor counter
#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();
 }
}

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.

LIST OF COMPONENT BUY ONLINE:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2yBk7eT (LCD display)

http://amzn.to/2zg8aeX (I2C)

http://amzn.to/2vmUlLw   (Ultrasonic sensor)

http://amzn.to/2wxPmWz  (Breadboard)

http://amzn.to/2vJ3lvo   (Jumper wire)

TILL THEN KEEP LEARNING KEEP MAKING 🙂

 

Leave a Reply

Your email address will not be published. Required fields are marked *