Top Logo Sp Logo
Raspberry Pi Occupancy Counter Project

Raspberry Pi Occupancy Counter Project

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

Materials

Hookup Instructions

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.

  1. Connect a jumper wire from the 5V pin on the Raspberry Pi to one of the + rails on the breadboard.

  1. Connect a jumper wire from the GND pin on the Raspberry Pi to the - rail next to the + rail you chose on the breadboard.

  1. Put your IR sensors in the breadboard spaced out as much as possible.

  1. 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).

  1. 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).

  1. 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).

  1. Run a jumper wire from GPIO pins 17 and 27 on the Raspberry Pi to the signal pins of the IR sensors.

Programming Instructions

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.

  1. 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.
  1. 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.
  1. 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/)
  2. Next, we need to define a function to count the current occupants.
  • Def countCurr(): for example
  1. Then, we want to check if the the sensor on the “outside of the door” is high. Check out GPIO.input().
  2. 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.
  1. Then, we want to check if the “inside of the door” sensor is high.
  2. 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.
  1. 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.
Shop the story