Raspberry Pi Occupancy Counter Project
In this project, we will use the Raspberry Pi and two IR motion sensors to count the number of people entering and exiting a room
- Raspberry Pi (I used a B, Rev 2, so adjust pins used if using something different) https://vilros.com/collections/raspberry-pi-boards/products/raspberry-pi-3-model-b-board
- This project assumes OS is installed, and access to monitor, keyboard, mouse, and power supply is available.
- Raspberry Pi GPIO connector Ribbon https://vilros.com/collections/raspberry-pi-accessories/products/raspberry-pi-t-shape-breakout-kit
- Breadboard https://vilros.com/products/breadboard
- Jumper Wires (x8+) https://vilros.com/products/jumper-wires
- 10 kohm resistors (x2) https://vilros.com/products/10k-resistors
- IR Motion Sensors (x2) https://www.amazon.com/gp/product/B07GJDJV63/ref=oh_aui_detai%20lpage_o00_s00?ie=UTF8&psc=1
These instructions are for testing this on a breadboard by waving hands over the sensors. Once it’s working, you can connect longer leads to the sensors and mount them on either side of a door frame.
- Connect a jumper wire from the 5V pin on the Raspberry Pi to one of the + rails on the breadboard.
- Connect a jumper wire from the GND pin on the Raspberry Pi to the - rail next to the + rail you chose on the breadboard.
- Put your IR sensors in the breadboard spaced out as much as possible.
- Run a jumper wire from the 5V rail on the breadboard to the power pin of the IR sensors (refer to data sheet of your sensor for the correct pin).
- Run a jumper wire from the GND rail on the breadboard to the GND pin of the IR sensors (refer to data sheet of your sensor for the correct pin).
- Place a 10 kohm resistor between the signal pin of the IR sensors and the GND rail of the breadboard (refer to data sheet of your sensor for correct pin).
- Run a jumper wire from GPIO pins 17 and 27 on the Raspberry Pi to the signal pins of the IR sensors.
Overview: There are several ways to program the occupancy counter on the Raspberry Pi. You even have multiple languages you can choose from! I chose to use Python in this project to utilize the GUI library Tkinter.
For this project, I would recommend taking my GUI segment and starting there. If you’ve never done GUI programming, that portion would take up more time than it warrants, since the main part of this project is learning how to use the IR sensors.
- Assuming you’re starting with a program that includes all my library includes, occupancy class, and and the last two lines of my program that launch the counting function and the GUI, we’ll start with setting up the GPIO pins.
- We are using the RPi. GPIO library for the GPIO. We first need to setup our pins using “GPIO.setmode(GPIO.BCM)” This sets the board type to Broadcom.
- Next, we need to tell the pins we’re using what their function is. Use GPIO.setup() to tell GPIO pins 17 and 27 that they’re inputs with pull down resistors enabled. (https://rpi.science.uoit.ca/lab/gpio/)
- Next, we need to define a function to count the current occupants.
- Def countCurr(): for example
- Then, we want to check if the the sensor on the “outside of the door” is high. Check out GPIO.input().
- If it is high, we want to wait until we see the “inside of the door” sensor turn on, and add (1) to the current count (this is occupancy.Object.curOccInt that’s created in the occupancy class).
- After adding to the counter, wait until both pins clear so you don’t count someone more than once.
- Then, we want to check if the “inside of the door” sensor is high.
- If it is, wait until the “outside of the door” is sensor on and subtract (1) from the current count.
- After subtracting from the counter, wait for both pins to clear so you don’t count someone more than once.
- Then, we need to make that integer a string and update the text in the GUI to reflect the current count. I’d recommend taking this from my code as well.