<img height="1" width="1" src="https://www.facebook.com/tr?id=1640437302870650&amp;ev=PageView &amp;noscript=1">

Subscribe to Email Updates

bg-img11.jpg

Building a People Counter with Raspberry Pi and Ubidots

Agustin Pelaez
By Agustin Pelaez
on December 13, 2016
12855755553_4b9e885e72_z
People counters are mostly used in the retail industry to gain better insights of how shoppers behave. They are also found in security, event management and smart cities applications. Imagine you manage a large mall; these counters would help you know how many people enter your mall, which paths they take, where they stop, and foremost, when does it all happen.
 
Just like most sensors, people counters have been around for a while. However, their data is not always centralised and connected to enterprise systems where they can support decision making.
In this article we'll explore a home-made, very basic people counter, and stream its data to the Ubidots cloud, where better insights can be created. 
I built this a day before a demo a the Boston New Tech Meetup last week, so I can tell you it was a fast and low-cost DIY project! 
 

What is Ubidots?

featuresTrigger2

Sign up for free

Ubidots is a cloud service to store and analize sensor data in real-time. It allows you to create applications for the Internet of Things, without having to know about web programming, data bases or APIs.

1. What you'll need

- Raspberry Pi Model B:

image

A small battery pack, with its micro-USB cable:

image

- Wifi USB Dongle:

image

Motion Sensor by Parallax:

image

- 3 female-female wires
- A small box to fit all the components

 

2. Wiring and Casing

As you can see, the motion sensor has just three pins: V+, Ground and a third one that outputs the signal: "1" when there is movement, and "0" when there isn't. No need to solder anything, nor to write I2C or serial functions to detect this signal; just plug the cables straight to the GPIO pins of your Raspberry Pi:
 image

Here's how it looked like:

image

**UPDATE** Following a suggestion by Doug Jefferies (Thanks!), Raspberry Pi's GPIOs are designed for 3.3v but we are putting 5v there. So it's a better idea to connect:

V+ --> Pin #1 (3.3v).

Because the sensor is very sensitive to movement, I used the jumper switch behind it to set the lowest sensibility. Also, I placed it in a dark case with a small aperture, so that the motion sensing focuses in one point instead of being so omnidirectional:

image

image

 

3. Coding 

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 the Raspberry Pi WiFi setup.
 
Let's make sure we have all the 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 the following code into it. 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 :)

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

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

 
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.
 
When the script is finished, you're ready to run it from the console:
 

$ python peoplecounter.py

 
 
That's it! Here is how the raw data looked like in my case:

image

Now that the data is in the cloud, you can add widgets in your dashboard to display the activity in real-time. You can also configure "Events" in your Ubidots account, so you get an SMS or Email notification when your variable hits a specific limit.

4. Conclusion

image

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.

More elaborate people counters use cameras and image processing algorithms to detect what the moving object is (person, car, pet..), in which direction it moves (in / out, left / right) and they could even be extended, in theory, to know the age and gender of the person.

Another way to detect people would be to passively sense the radio signals from their smartphones, such as Wifi or Bluetooth (check out how this guy detected smartphones around his house). Ultimately, the iBeacon technology should also be an big enabler of these applications.

In any case, the collected data can be easily sent to Ubidots, where it can be interpreted by creating alerts, live dashboards or plugging it to other systems.

Do you have sensor project ideas?

Create a Ubidots account an make them happen!

GET STARTED

Leave a comment

Agustin Pelaez
Written by Agustin Pelaez
Written by Author

Related posts

IoT Interfaces Made Simple With Nextion, Raspberry Pi and Ubidots
IoT Interfaces Made Simple With Nextion, Raspberry Pi and Ubidots
Agustin Pelaez
By Agustin Pelaez
on December 13, 2016

Have you heard about Nextion Display? Nextion allows you to craft awesome interfaces, even if your coding level is "hello...

Water your plants remotely using PiFace and Ubidots
Water your plants remotely using PiFace and Ubidots
Agustin Pelaez
By Agustin Pelaez
on December 13, 2016

One of the reasons I mostly have cactuses at home is because I often forget to water my plants. This is why I build this...