How to Build a People Counter With Raspberry Pi and Ubidots




About: Community manager @Ubidots, a startup empowering makers and hardware engineers to create applications for the Internet of Things #IoT

In this simple project we'll use a motion sensor to detect if an object is passing in front of our Raspberry Pi. Then we'll count how many times that happens, and send this value to Ubidots.

People counters are typically expensive devices used in the retail industry to understand how shoppers behave. Thanks to Raspberry Pi and Ubidots, we're able to build a functional people counter in a few hours and with a few bucks!

Once we send the people counting data to Ubidots, we can create nice graphs for analysis, as well as SMS/Email alerts.

Step 1: Getting the Right Materials

To complete this project you'll need:

  1. A Raspberry Pi Model B
  2. A PIR Sensor by Parallax
  3. A Raspberry Pi compatible USB WiFi Dongle
  4. A USB battery pack to power the Raspberry Pi (this is optional in case you want to leave the Pi completely wireless)
  5. Three female-female jumper wires

Step 2: Wiring Things Up

The PIR motion sensor is quite simple to use because it only has three pins:

  • V+
  • GND
  • A signal pin that outputs "1" when there is movement and "0" when there isn't.

No need to solder anything, nor to write complex I2C or serial functions to detect this signal; just plug the cables straight to the GPIO pins of your Raspberry Pi and it will work!

Step 3: Casing

Because the PIR sensor is very sensitive to movement, I used the jumper switch behind it to set the lowest posible sensibility. Also, I took an old case from a pair of sunglasses and made a hole in it, then placed the RPi and the PIR sensor inside it. This way, the motion sensor highly focused in one point, instead of being so omnidirectional.

Step 4: Coding Your RPi

At this point, we'll assume you have done a basic setup of your Raspberry Pi and you are looking at its Linux command line. If not, we recommend going through this guide first. You can also check this post about using Wicd to setup the WiFi of your Raspberry Pi.

Let's begin by making sure we have all the required libraries:

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

Create a new file called "":

$ sudo nano

And write into it the code below. Make sure to replace the values of the API key and the variable ID with the ones in your personal Ubidots account. (Note: the code is not too elegant, but hey I'm not a Python developer, just a hardware guy :)

The script consists of a loop that checks the state of the pin #7 (the motion sensor). If it reads a "1", meaning that there was movement, then it increments the "peoplecount" variable and waits 1.5 seconds so the motion sensor goes back to normal. This is done 10 times, making sure there is at least 1 second between each cycle, then it sends the total sum of "movements" to Ubidots. If you need to calibrate the People Counter, you should then play with the "time.sleep" lines with other values.

from ubidots import ApiClient

import RPi.GPIO as GPIO

import time


GPIO.setup(7, GPIO.IN)


api =ApiClient("a21ebaf64e14d195c0044fcc3b9f6dab9d653af3")

people = api.get_variable("5238cec3f91b282c7357a140")

except: print "Couldn't connect to the API, check your Internet connection"

counter = 0

peoplev = 0


presence = GPIO.input(7)


peoplecount += 1

presence = 0



counter += 1


print peoplecount


counter = 0

peoplev = 0

Step 5: Display Your Data

Finally, go to your Ubidots dashboard and add a widget of the type "Statement". This will display the total number of people detected within a time frame you specify

Step 6: Wrapping Up

This project provides a hint of the amount of people passing through a particular point. It doesn't provide the exact number of people, given the limitations of the motion sensor, but in some applications this might be just enough.

The collected data can be easily sent to the Ubidots Cloud, where it can be interpreted by creating alerts, live dashboards or even sharing this data in social media, as embed code, or just in a public link. You can also read this data from another application using the Ubidots API.



    • Woodworking Contest

      Woodworking Contest
    • Gardening Contest

      Gardening Contest
    • Party Challenge

      Party Challenge

    14 Discussions


    2 years ago

    can i use any other PIR sensor? Any other sensor i can buy in spain? The shipping costs are too high


    2 years ago

    amazing project


    2 years ago

    For the rasberry pi 3, do I need to use WICD to set its wifi? Because it has a built in wifi, unless that's needed for static IP purposes.


    2 years ago

    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!


    3 years ago

    I've done everything according to the guide but it keeps saying that it can't connect to the internet which is the except print statement. Please help me rectify this.


    3 years ago

    Hello, Is it possible to compute Dwell time apart from people counting?


    3 years ago

    UbiMaker, great job! Have you thought about doing this same project using Arduino instead of Raspberry Pi? If so, I would love to see it!


    1 reply

    Reply 3 years ago

    Good idea @brentmade. I don't have one right now but the logic should be simple to port. The connectivity might be a bit trickier to port because it depends on what you want to use (wifi, ethernet, gprs) here are some guides for the connectivity part:


    4 years ago on Introduction

    Out of curiosity, have you used this to count in/out flow at a retail store? Is it too sensitive? Curious how its been applied and if its worked as expected.

    1 reply

    4 years ago on Introduction

    Now you need to put this onto each of the rams of the skate park and find out what ramp is used the most!


    Reply 4 years ago on Introduction

    Thanks @seamster! Feel free to share and let me know if you have any questions/suggestions.