Arduino

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

How to make Bi-Directional Visitor Counter

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 🙂

Leave a Reply

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