Bi-Directional Visitor Counter using single ultrasonic sensor with LCD

Bi-Directional Visitor Counter using single ultrasonic sensor with LCD

Posted Leave a commentPosted in Arduino

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 🙂

 

How to make Bi-Directional Visitor Counter

Arduino Tutorial 32# How to make Bi-Directional Visitor Counter

Posted Leave a commentPosted in Arduino

In this tutorial we will see how to make bi directional visitor counter by using 7 segment display and ultrasonic sensor. It will count how many person/object are in the system and subtract, which left from the system. So let’s get started.

 

For this you will need,

  1. Arduino,
  2. 7 Segment display,
  3. 2 Ultrasonic sensor (HC-SR04)
  4. Shift register 8 bit (SN74HC595),
  5. 220 Ohms resistor,
  6. Jumper wires,
  7. Breadboard.

you will like other counter tutorial

  1. PIR sensor counter with LCD,
  2. Ultrasonic sensor counter with LCD,

We are using shift register, which will save input/output pins on Arduino. In this tutorial we are using two ultrasonic sensor, which will not only count how many objects are entering in the system but also subtract how many object exit from the system. Sensor 1 will detect some object is entering in system, but it has to pass from sensor 2 also, so it will add 1 in counter. If object exit from system means, it has to go from sensor 2 first then sensor 1. Therefore, It will subtract 1 in counter.

Do connection as shown in diagram.

 

Circuit for Bi-Directional Visitor Counter
Circuit for Bi-Directional Visitor Counter
Sketch for Bi-Directional Visitor Counter
const int dataPin = 3; // to 74HC595 pin 14
const int latchPin = 2; // to 74HC595 pin 12
const int clockPin = 4; // to 74HC595 pin 11
 #define trigPin 13
 #define echoPin 12
 #define trigPin2 8
 #define echoPin2 9
/* uncomment one of the following lines that describes your display
 * and comment out the line that does not describe your display */
const char common = 'a'; // common anode
//const char common = 'c'; // common cathode
int button = 7;
int i = 0;
int currentState = 0;
int previousState = 0;
int currentState2 = 0;
int previousState2 = 0;
 
byte bits;
bool decPt = true; // decimal point display flag
byte myfnNumToBits(int someNumber) {
 switch (someNumber) {
 case 0:
 return B11111100;
 break;
 case 1:
 return B01100000;
 break;
 case 2:
 return B11011010;
 break;
 case 3:
 return B11110010;
 break;
 case 4:
 return B01100110;
 break;
 case 5:
 return B10110110;
 break;
 case 6:
 return B10111110;
 break;
 case 7:
 return B11100000;
 break;
 case 8:
 return B11111110;
 break;
 case 9:
 return B11110110;
 break; 
 default:
 return B10010010; // Error condition, displays three vertical bars
 break; 
 }
}
void myfnUpdateDisplay(byte eightBits) {
 if (common == 'a') { // using a common anonde display?
 eightBits = eightBits ^ B11111111; // then flip all bits using XOR 
 }
 digitalWrite(latchPin, LOW); // prepare shift register for data
 shiftOut(dataPin, clockPin, LSBFIRST, eightBits); // send data
 digitalWrite(latchPin, HIGH); // update display
}


 
void setup() {
 // initialize I/O pins
 pinMode(dataPin, OUTPUT);
 pinMode(latchPin, OUTPUT);
 pinMode(clockPin, OUTPUT);
 pinMode(button, INPUT);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(trigPin2, OUTPUT);
 pinMode(echoPin2, INPUT);
}

void loop() {
 decPt = !decPt; // display decimal point every other pass through loop
 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;
 long duration2, distance2;
 digitalWrite(trigPin2, LOW); 
 delayMicroseconds(2); 
 digitalWrite(trigPin2, HIGH);
 delayMicroseconds(10); 
 digitalWrite(trigPin2, LOW);
 duration2 = pulseIn(echoPin2, HIGH);
 distance2 = (duration2/2) / 29.1;
 // object entering in the system
 if (distance <= 10){
 currentState = 1;
 }
 else {
 currentState = 0;
 }
 //delay(100);
 if(currentState != previousState){
 while(currentState == 1) {
 long duration2, distance2;
 digitalWrite(trigPin2, LOW); 
 delayMicroseconds(2); 
 digitalWrite(trigPin2, HIGH);
 delayMicroseconds(10); 
 digitalWrite(trigPin2, LOW);
 duration2 = pulseIn(echoPin2, HIGH);
 distance2 = (duration2/2) / 29.1;
 if (distance2 <= 10){
 currentState2 = 1;
 }
 else {
 currentState2 = 0;
 }
 //delay(100);
 if(currentState2 != previousState2){
 if(currentState2 == 1) {
 i = i+1;
 byte bits = myfnNumToBits(i);
 myfnUpdateDisplay(bits); // display numeric digit
 delay(1000); // pause for 1/2 second
 }
 else {
 byte bits = myfnNumToBits(i);
 myfnUpdateDisplay(bits); // display numeric digit
 }
 return;
 
 }
 }
 }
 // object exit from the system 
if (distance2 <= 10){
 currentState2 = 1;
 }
 else {
 currentState2 = 0;
 }
 delay(100);
 if(currentState2 != previousState2){
 while (currentState2 == 1) {
 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 <= 10){
 currentState = 1;
 }
 else {
 currentState = 0;
 }
 delay(100);
 if(currentState != previousState){
 if(currentState == 1) {
 i = i-1; 
 byte bits = myfnNumToBits(i);
 myfnUpdateDisplay(bits); // display alphanumeric digit
 delay(1000); // pause for 1/2 second
 }
 else {
 byte bits = myfnNumToBits(i);
 myfnUpdateDisplay(bits); // display alphanumeric digit
 }
 return;
 }
 }
 }
}

Lets come to the programming part. Define shift register pins and ultrasonic sensor pins.

byte myfnNumToBits(int someNumber)

This function will display what number to be shown on 7 segment display by individually On/Off particular segment of LED.

void myfnUpdateDisplay(byte eightBits)

There are two type of connection to 7 segment display one is common anode and second is common cathode. For this tutorial we are using common anode. This function will flip all the bits using XOR and update the display .

If distance is less than 10cm on sensor 1, currentstate will become 1 and it will wait for object to pass in front of sensor 2. When it passes in front of sensor 2 it will count 1. Similarly, if object exit from system, it will pass in front of sensor 2 first, and currentstate2 will become 1, which will wait for object to pass in front of sensor 1. When it passes in front of sensor 1. It will subtract 1 from counting value. Delay time can be adjust depend upon project requirement.

As you can see when box is passing in front of sensor it is adding 1. When I passed this box in opposite direction it will simply subtract 1 from counting value.

LIST OF COMPONENT BUY ONLINE:

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

http://amzn.to/2oPTKhB (7 segment display)

http://amzn.to/2oNcQ81 (SN74HC595N 8-Bit Shift Registers)

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

http://amzn.to/2vmSK8l  (Resistor)

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

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

TILL THEN KEEP LEARNING KEEP MAKING 🙂