pratiktanikellapreetamtanikella
Created August 29, 2020

Building Population Counter

System for monitoring the number of people in a closed area and collecting data to help business owners and customers plan for peak hours.

Building Population Counter

Things used in this project

Hardware components

Arduino Nano 33 BLE
Arduino Nano 33 BLE
×1
Breadboard Power Supply Module (5 pcs)
×1
Micro-SD card module (5 pcs)
×1
Jumper Wires(120 pcs)
×1
16x2 LCD Display Module (2 Pcs)
×1
Ultrasonic Module HC-SR04 Distance Sensor (3 pcs)
×1
HC-SR501 PIR Body Motion Sensor Modules (5 pcs)
×1
Micro-SD card 16 GB (2 pcs)
×1
DS1307 RTC Module (2 pcs)
×1
220 Ohm Resistor (100 pcs)
×1
Standard Breadboard (3 pcs)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
(Optional): Only needed if soldering pins to Arduino nano

Story

Read more

Schematics

Full Schematic for device(Breadboard view)

This is a full schematic on Fritzing showing all the connections between the Arduino, the power source, and the various devices and sensors used.

Fritzing file for device schematic.

Schematic that can be opened in fritzing

Full Schematic for device(Schematic/wire view)

This is the full schematic of the device in the schematic view showing all the wire connections.

Code

Full Code for Device

Arduino
The entire code for the device used to determine the direction of a person's movement, calculate how many are currently in the store, display the real-time count of the number of people in the store, determine the exact time and date, create a file based on the date the device is being used, and store the data on the count and when the count changed on that file.
/*
 * 
 * Code by Preetam and Pratik Tanikella
 * 
 * This code uses a combination of PIR and Ultrasonic sensors to determine the direction of a person's movement
 * 
 * If the code determines that someone has gone into a building, the count of the number of people in the building will increase by one
 * 
 * If the code determins that someone has left a building, the count of the number of people in the building will decrease by one
 * 
 * The code also stores data on the count and the time when the count changed with the SD card and RTC module
 * 
 */
 
 
//Include Library Codes
#include <RTClib.h>
#include <LiquidCrystal.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

//Set up DS1307 RTC module
RTC_DS1307 rtc;

//Set up SD card module
File myFile;

//Define pins, set up LCD display, and define contrast value
const int rs = 8, en = 7, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int contrast = 50;

//Define pins for Ultrasonic sensor, PIR sensors, and SD card module
int trigPin = A6; 
int echoPin = A7;
int PIRpin = 9;
int PIRpin2 = A3;
const int chipSelect = 10;

//Define max capacity of building and doorway length
int maxCapacity = 50;
int doorwayLength = 61;

int pinSwitcher;
int timeSwitch;
int PIRstate = LOW;
float duration;
float distance;
int counter = 0;

//Define variables for time and date
int nowMinute;
int nowSecond;
int nowHour;
int nowDay;
int nowDate;
int nowMonth;

//Define strings to create filename and char filename
String str;
String str2;
String str3;
String str5;
String str4;
char Filename[15];

//Function to find distance using Ultrasonic Sensor
void ultrasonicStart(){ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 
 digitalWrite(trigPin, HIGH); 
 delayMicroseconds(10); 
 digitalWrite(trigPin, LOW); 
 duration = pulseIn(echoPin, HIGH);
 distance = (duration*.0343)/2;
}

//Function to switch which PIR sensor is used
void pirSwitch(){
  pinSwitcher = PIRpin;
  PIRpin = PIRpin2;
  PIRpin2 = pinSwitcher;
  delay(750);
   if(PIRpin == A3){
    digitalWrite(A2,HIGH);
   }
   else if(PIRpin == 9){
    digitalWrite(A2, LOW);
   }
}

//Function to add new count and time when count changed to that days file
void SDset(){
  myFile = SD.open(Filename, FILE_WRITE);
  myFile.print(nowHour);
  myFile.print(":");
  myFile.print(nowMinute);
  myFile.print("\t");
  myFile.println(counter);
  myFile.close();
  Serial.println("Done");
}

//Function to update LCD screen with new count
void lcdSet(){
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(counter);
  lcd.setCursor(0,1);
  lcd.print("out of");
  lcd.setCursor(7,1);
  lcd.print(maxCapacity);
}

void setup() {

 Serial.begin(9600);
  
 //Set pinmode for Ultrasonic sensor, PIR sensors, and power for second PIR sensor
 pinMode(trigPin, OUTPUT); 
 pinMode(echoPin, INPUT); 
 pinMode(PIRpin,INPUT);
 pinMode(PIRpin2,INPUT);
 pinMode(A2,OUTPUT);

 //Set contrast for LCD and start LCD display
 analogWrite(6,contrast);
 lcd.begin(16, 2);
 lcd.print(counter);
 lcd.setCursor(0, 1);
 lcd.print("out of");
 lcd.setCursor(7,1);
 lcd.print(maxCapacity);

 //Set time on RTC module and get date for filename
 rtc.begin();
 rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
 DateTime now = rtc.now();
 nowDate = now.day();
 nowMonth = now.month();

 //Create filename using the date
 str = String(nowDate);
 str2 = String(nowMonth);
 str3 = String("/");
 str4 = String(".txt");
 str5 = str+str3+str2+str4;
 str.toCharArray(Filename, 15);

 //Check to see if SD card module is working properly
 if (!SD.begin(10)) {
    Serial.println("SD not connected");
    while (1);
  }
  Serial.println("working");
 
 //Create file on SD card module using filename
 myFile = SD.open(Filename, FILE_WRITE);
  if (myFile) {
    myFile.print("Time");
    myFile.print("\t");
    myFile.println("Count");
    myFile.close();
    Serial.println("done.");
  }
 } 

void loop() { 
  //Get time and date from RTC module
  DateTime now = rtc.now();
  nowMinute = now.minute();
  nowHour = now.hour();

  //check to see if someone passed ultrasonic sensor then PIR sensor
  ultrasonicStart();
  if(distance>0 && distance<doorwayLength){
    delay(250);
    PIRstate = digitalRead(PIRpin);
    if(PIRstate != LOW){
      //Increase count by one after someone passed ultrasonic then PIR sensor
      counter+=1;
      
      //make sure that count is never negative
      if(counter<0){
        counter = 0;
      }
      //Set LCD screen, Switch which PIR sensor is being used, and add changes to count along with time to that days files
      lcdSet();
      pirSwitch();
      SDset();
       }
  }
  
   //check to see if someone passed PIR sensor then ultrasonic sensor
   PIRstate = digitalRead(PIRpin);
    if(PIRstate!=LOW){
      delay(450);
      ultrasonicStart();
      if(distance>0 && distance<doorwayLength){
        //Decrease count by one after someone passed PIR then ultrasonic sensor
        counter-=1;

        //make sure that count is never negative
        if(counter<0){
          counter = 0;
        }
        
        //Set LCD screen, Switch which PIR sensor is being used, and add changes to count along with time to that days files
        lcdSet();
        pirSwitch();
        SDset();
      }
    }
}
 

Credits

pratiktanikella

pratiktanikella

2 projects • 0 followers
preetamtanikella

preetamtanikella

1 project • 1 follower

Comments