Updated People Counter With Raspberry Pi


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)


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


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

counter = 0
peoplecount = 0

presence = GPIO.input(7)


peoplecount += 1
presence = 0
counter += 1


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!



    • Creative Misuse Contest

      Creative Misuse Contest
    • Oil Contest

      Oil Contest
    • Water Contest

      Water Contest

    13 Discussions

    Hey Ashley,

    I setup everything per the instructions but i am not getting any reading from the sensor. The code is counting up without any triggers from the PIR. placing my hand in front i know the PIR is reading the presence of an object but don't think the input from the GPIO is doing anything.

    1 reply

    Hi Kaustubhs3,

    Were you able to get around your issue? I too am getting just 0's. I know the PIR is reading but I do not see the people count increase. Thanks for any help.

    Hi Ashley,

    Thanks for the detailed step by step. I am new tot his and tried the code and all i am getting it a bunch of "0".

    UBIDOTS is updating number but not the number of times the presence has been noted. Can you please help me understand where its failing?

    hey guys,

    could anyone give me some advice, i allways get an error saying:

    Tracebak (most recent call last):

    File "peoplecounter.py", line 1 in <module>

    from ubidots import ApiClient

    File "/home/pi/ubidots.py", line 1 in <module>

    from ubidots import ApiClient

    ImportError: cannot import name ApiClient

    Thank you

    Hi everyone!

    I know this has been idle for a while, but I was wondering if someone could help me with some error handling.

    The problem I am seeing is that some time in the middle of the night no packages are sent to Ubidots anymore. I suspect that is due to a disconnect of my internet connection, which will throw an error and the script quits / aborts.

    What would be an error handling solution to just try to reconnect (dropping data in the meantime would be ok for me) and send data the moment internet is available again?

    Thank you for your help!

    Hi, sorry to bring this back from the grave. I made one and it works perfectly. I'm planning to use two sensors to support bidirectional count. How do you edit the script to support it?
    So let's say presencein and presenceout variables,

    if presenceout was triggered first before presencein then count it and vice versa.


    1 reply

    I haven't been able to get my hands on a 2nd sensor to do this for real, but in the python script, this is the sensor part:

    presence = GPIO.input(7)

    Theoretically the Pi is capable of power multiple PIR sensors (but I haven't tested yet to know 100%), so you could simply do something like:

    presence2 = GPIO.input(8)

    …and then repeat the process (sense, increment value, send data to ubidots) for each sensor.

    // For each sensor
    for sensor in [GPIO.input(7), GPIO.input(8)]
    // If the sensor activates
    if ( sensor ) :
    // do stuff

    Makes sense, I wasn't sure if it was intentional... sometimes things appear odd to take advantage of some interesting caveats.

    Is there an advantage to using
    Rather than just a single

    3 replies

    But the basic rationale is that the time.sleep(1.5) is supposed to be nested in an above behavior, and the time.sleep(1) is a separate command.

    I think this is my code just looking awkward because I don't have the Pro version of Instructables. I'll go back and try fiddling with my indentations.

    We're telling the Pi that if the sensor reads motion (time.sleep(1), where 1 = true) to wait 1.5 seconds (the recommended time from Ubidots for a reset) and to then run again. I'm going to try and C+P my code from my original doc, but the 3 lines with "people count +=1, presence = 0, time.sleep(1.5)" should be nested under if(presence):, where as the time.sleep(1) is not.


    peoplecount += 1

    presence = 0