Introduction: Updated People Counter With Raspberry Pi

This is an updated version of UbiMaker's Open Source People Counter with new scripts.

Where I have made mistakes, I will note them, in the hopes that you don't have to do the same!

People Counters are an especially useful tool for libraries, and one that many cannot afford. The total cost for my build was under $100.

This tutorial assumes that your Pi is already set up (I recommend using Raspbian) and you've already configured your WiFi dongle.

Step 1: Gather Your Materials

You'll need:

  1. Raspberry Pi B+
  2. A Micro SD Card (at least 8 GB)
  3. A PIR Sensor (Note: The one I have pictured and purchased is from SparkFun and is more useful for Ardunio projects, If you buy the Parallax option listed in the original tutorial you'll save yourself a step and some jumper wires)
  4. 3 Male-to-male jumper wires (Note: Not necessary if you buy the PIR sensor linked above)
  5. 3 Female-to-female jumper wires
  6. A USB WiFi Dongle (Note: Only purchase if it explicitly says it will work with Raspberry Pi
  7. Optional: USB Charger/Battery Pack if you want to go Wireless (helpful to have this pre-charged)
  8. Optional but helpful: Some kind of casing (not pictured)

Step 2: Sign Up for an Ubidots Account and Grab Info

  1. Create an account with Ubidots or sign into your pre-existing account
  2. Grab your account specific API (found in your profile)
  3. Create a new data source titled "People Counter" and retrieve the variable ID
    1. Click "Sources" in the top navigation menu
    2. Click "Add a Data Source"
    3. Choose "Raspberry Pi" as source type
    4. Title this "People Counter" or something similar
    5. Your variable ID will be displayed on the left column

Step 3: Wire It Up!

Even though the Raspberry Pi B+ has added ports, the schematics from the previous tutorial are still usable and the ports are the same.

  • V+ to 5 volt power source (Pin 2)
  • Ground to ground (Pin 6)
  • Signal to GPIO07 (Pin 26)

Because I bought a PIR sensor that had the leads pre-soldered and combined, I had to use male-to-male jump wires to connect the sensor to female-to-female jump wires and then to the board itself. If you are clever and bought the Parallax sensor, you can connect your female-to-female wires directly from the sensor to the board.

Step 4: Coding Time!

If you're running the GUI (graphical user interface) on your Raspberry Pi, open a command window.

First call up the necessary libraries:

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install python-setuptools
$ sudo easy_install pip
$ pip install ubidots==1.6.1

Create a new file called "peoplecounter.py":

$ sudo nano peoplecounter.py

Then enter the following code (Note: Enter your personal API and variable ID that we pulled from your ubidots account earlier) NOTE: This code is not properly formatted because the basic plain text editor will not allow tabs to show nesting. Correctly formatted code can be accessed via this Drive doc.

from ubidots import ApiClient
import RPi.GPIO as GPIO

import timeGPIO.setmode(GPIO.BCM)
GPIO.setup(7, GPIO.IN)

try:

api = ApiClient("04856548e100d631985d3e9bd9d112c1846ff8da")
people = api.get_variable("55b2b19376254219c59334c0")

except:

print("Couldn't connect to the API, check your Internet connection")

counter = 0
peoplecount = 0
while(1):

presence = GPIO.input(7)

if(presence):

peoplecount += 1
presence = 0
time.sleep(1.5)
time.sleep(1)
counter += 1

if(counter==10):

print(peoplecount)
people.save_value({'value':peoplecount})
counter = 0
peoplecount = 0

Step 5: Run the Script

Enter into your command line:

$ sudo python2 peoplecounter.py

If you run your hand over the sensor, you should start seeing numbers generated. If you've entered in your Ubidots information correctly, the same data should be generated into your Ubidots account.

Collect all your data and use the Ubidots platform to generate fabulous reports!