Introduction: Raspberry Pi Occupancy Counter Project

About: At Vilros, our mission is to supply a challenge that is fun, useful and rewarding. Whether you are a DIY novice or pro, enjoy the journey of computer building with us!

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:

Step 1: 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.

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

Step 2:

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

Step 3:

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

Step 4:

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

Step 5:

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

Step 6:

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

Step 7:

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

Step 8: 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.

2. 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.

3. 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/)

4. Next, we need to define a function
to count the current occupants.

a) Def countCurr(): for example

5. Then, we want to check if the the sensor on the “outside of the door” is high. Check out GPIO.input().

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

a) After adding to the counter, wait until both pins clear so you don’t count someone more than once.

7. Then, we want to check if the “inside of the door” sensor is high.

8. If it is, wait until the “outside of the door” is sensor on and subtract (1) from the current count.

a) After subtracting from the counter, wait for both pins to clear so you don’t count someone more than once.

9. 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.