Introduction: How to Build a People Counter With Raspberry Pi and Ubidots

Picture of How to Build a People Counter With Raspberry Pi and Ubidots

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

Picture of 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

Picture of 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

Picture of 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 "peoplecounter.py":

$ sudo nano peoplecounter.py

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.setmode(GPIO.BCM)

GPIO.setup(7, GPIO.IN)

try:

api =ApiClient("a21ebaf64e14d195c0044fcc3b9f6dab9d653af3")

people = api.get_variable("5238cec3f91b282c7357a140")

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

counter = 0

peoplev = 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

peoplev = 0

Step 5: Display Your Data

Picture of 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

Picture of 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.

Comments

it_support3 (author)2017-04-27

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

AanR3 (author)2016-12-25

amazing project

engdar (author)2016-10-15

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.

ThomasS267 (author)2016-07-15

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!

lungpamei247 (author)2016-05-09

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.

SheikhF1 (author)2015-12-17

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

brentmade (author)2015-12-04

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!

Thanks!

UbiMaker (author)brentmade2015-12-06

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: www.ubidots.com/docs/devices

CarlosM11 (author)2014-12-18

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.

UbiMaker (author)CarlosM112015-06-24

Hi there, sorry for the late reply -no comment notification was received-

This guy is actually using a similar setup in a real store: http://armaal.com/peoples-counter-using-ubidots/

Differentiating in/out flow would be difficult given the limitations of the PIR sensor.

saymowan (author)2014-09-23

Simple and creative! Thanks for sharing! :)

JM1999 (author)2014-09-23

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

seamster (author)2014-09-23

Nice project. Thanks for sharing it!

UbiMaker (author)seamster2014-09-23

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

About This Instructable

44,500views

53favorites

Bio: Community manager @Ubidots, a startup empowering makers and hardware engineers to create applications for the Internet of Things #IoT
More by UbiMaker:Build an Energy Monitor Device Using a Particle ElectronDIY Raspberry Pi Temperature System With UbidotsTest You Internet Speed Using a Raspberry Pi + Ubidots
Add instructable to: