DIY: Engineering Your Company Culture

Amir Keren

Amir Keren

I am a Senior Software Engineer at Taboola with over 8 years of development experience working on personalization features for publishers.

Amir is a senior engineer at Taboola. He holds a B.Sc. in Software Engineering from the Technion, an M.B.A from Tel Aviv University and is currently pursuing his M.Sc. in Computer Science.

Amir Keren | 15 Feb 2018 | Javascript

Tags: bot, ping pong, PIR Sensor, Raspberry Pi, slack, tips, web development

This is a tale of heroism, of overcoming obstacles and hardships. This is a tale of ingenuity, of originality and thinking outside the box. This is a tale… of how I was too lazy to go and look if someone was already playing table tennis in the game room.



Hiking Across the Office is a Drag


Taboola’s Israeli office in Tel Aviv, houses about 350 people, spread over five large floors. The game room, however is smack dab in the middle of them.

In smaller companies, if you wanted to know if the game room was available, all you had to do was to look slightly over your monitor and you would have your answer.

Here, it takes 60 seconds and 110 steps, including one flight of stairs, to get from my workstation all the way to the game room – believe me, I counted. Unfortunately, due to the unbending laws of physics, it takes exactly the same time to go back, frustrated that it was already being used.

And so, after one too many of these time-wasting, context-switching, hope-shattering trips, I started asking myself one simple question – what if we could just ask if someone was already using the game room?

Weapon(s) of Choice – Slack Bot, PIR Sensor and a Raspberry Pi

Salvation came in the form of a Passive Infrared (PIR) motion sensor connected to a Raspberry Pi. The sensor data is transmitted to the Raspberry Pi, which in turn saves the data to a local DB. The Slack bot user then queries the DB, to find out if actual movement has been detected in the room recently.


The entire system looks like this:

I Sense a Disturbance in the Room



A PIR sensor is just a fancy name for your run-of-the-mill motion detector, the ones you usually see mounted on walls of houses. Basically, the way it works is you have a sensor that measures infrared light, radiating from objects in its field of view. The term passive in this instance refers to the fact that PIR devices do not generate or radiate energy for detection purposes, they work entirely by detecting infrared radiation, emitted by or reflected from objects (find more information about PIR sensors here).

Easy as Pi


Raspberry Pi is a series of small single-board computers designed to promote the teaching of basic computer science in schools and in developing countries. It achieves this goal by being highly versatile, easy to setup and most important – very, VERY affordable, starting at only $5 (!) for the most basic model and $35 for the latest model.

It has a lot of cool features, but maybe the most important one is the General Purpose Input/Output (GPIO) connector. These pins are a physical interface between the Pi and the outside world. This is what is going to be used in order to connect the motion sensor output to the Raspberry Pi.

A Tale of Two Scripts

Raspberry Pi can run all sorts of images on its MicroSDHC card, but the most prevalent one is its own flavor of Unix – Raspbian, which is what I installed to run the Python interpreter and the various dependencies and tools required for this project.

The solution was divided into two separate Python scripts, which run on startup – the motion detection and the slackbot script.

A local MySQL database is used to store information about the activity in the room, meaning a list of all previous sessions and a current active one, should one exist.

This is to allow for persistency and other possible future features, such as to determine when is the optimal time to go and play, or how long someone has already been using the room.

The motion detection script listens for movement (or lack thereof) and updates the database accordingly – opens a new game session if one is not already opened, extends active sessions and closes sessions after a certain period of non-movement.

The slackbot script handles Slack integration and queries the database to provide information about the room’s status, by checking if there are currently any active open sessions.

Halt! Who Goes There?


I used an HC-SR501 motion sensor, which you can get online for $1. It only has three pins that you need to connect to the Raspberry Pi – GND (Ground), VCC (5V) and OUT (Data) – follow the diagram above to connect these (see the pin layout here).

The motion detection script communicates with the GPIO channels by listening on the data pin – marked as OBSTACLE_PIN in the code. This is done utilizing the RPi.GPIO package, which allows you to access the various pins on the board and get their current state (see documentation here).

Once the motion detection script starts running it goes in an endless loop, which is divided to one second intervals, and in each iteration the sensor input is read. Querying the sensor gives a binary response – a movement was detected or not.

Since the motion detector samples motion every second, it needs to be able to determine that an actual movement occurred not only in a particular second, but several times during a short period of time, to avoid false positives, i.e. detecting movement when there was none, and false negatives, i.e. not detecting movement when it should have.

So, in every iteration a “detection frame” is examined, which is basically a time window in which the number of seconds a movement was detected is counted. The number of seconds with movement, out of the total number of seconds in the time window, is compared to a predefined threshold.

If the number exceeds the threshold, then a movement was detected in the room. This allows control of the sensitivity of the detection, in addition to being able to adjust it physically on the sensor itself.


The following code demonstrates this logic:


Slacking Off

Below is a code snippet from the slackbot script, outlining the interaction with the aptly named PingPongBot – you will need to add a new bot user to your Slack team (see this link for more information), after which you will receive a token to be used in your client:

You can find the full project code on my github page.

I Love It When a Plan Comes Together

This is what the sensor looks like taped to the floating ceiling of our game room (the Raspberry Pi is hidden from view):


And finally, this is how it looks like when you ask the PingPongBot if the game room is free:



Not Only for the Office


Hopefully by now you realize how simple and easy it is to implement an entire motion detection system using accessible and affordable parts.

There are many other sensors that you can incorporate in your next project, turning your office/home into a smart one. For example, a temperature and humidity sensor can be used as input to control your AC, or a rain detection module to let you know when to turn off your sprinklers or close the electric windows.

Well, writing this post really made me parched. Hmm… I wish there was a way to know if there are beers left in the kitchen…