Building a motion sensor for Philips Hue lights using Raspberry Pi Zero

Posted by Shijaz Abdulla | Leave a comment

In this post, we discuss how you can use a Raspberry Pi Zero W to build a motion sensor to control your Philips Hue lights at home. There is a lot of sample code for motion sensors out there on the Internet, however I believe this one would be more practical for actual use at home.

For instance, the code includes:

  • The capability to prevent the motion sensor from turning on lights at certain times of the day (for example during daytime or at bedtime in a bedroom).
  • You can adjust how long the Hue lights in your room should remain on once motion is detected.
  • The code runs in an endless loop. Use crontab to make sure this code starts running each time the pi reboots.
  • The code also prints logging information for troubleshooting.

Here’s what you’ll need:

  1. Raspberry Pi Zero W with NOOBS installed: Although you can use other models of Raspberry Pi with this code, I used a Raspberry Pi Zero W, which is low cost, comes with adequate processing power for a motion sensor and built-in WiFi and Bluetooth.
  2. PIR Motion Sensor module
  3. Suitable case for Raspberry Pi Zero W (I use Pibow)
  4. GPIO Header, jumper cables, power supply, etc.

Wiring up your Raspberry Pi Zero W

If you are familiar connecting a PIR motion sensor module to your Raspberry Pi Zero W, you can skip this step.

Raspberry Pi Zero W - connecting a PIR motion sensor  PIR motion sensor module - connections 

The PIR motion sensor module has three pins – VCC, OUT and GND. Make connections as follows using jumper cables:

VCC –> GPIO Pin 2 (5V power) – black wire in picture

OUT –> GPIO Pin 16 – white wire in picture

GND –> GPIO Pin 6 (ground) – gray wire in picture

The PIR motion sensor module also has two potentiometers labeled TX (time delay) and SX (sensitivity). You will need to experiment these settings with a small Philips screwdriver till you get desired results. Sensitivity (SX) determines how much motion is required for the sensor to conclude that there has been motion. The time delay (TX) determines how long the PIR will maintain a “High” (ON) output after detecting motion.

PIR motion sensor module - adjusting sensitivity and time
The two potentiometers – TX and SX (orange)

Preparing your Raspberry Pi Zero W and writing the python script

Prepare the Raspberry Pi Zero, by installing NOOBS, configuring your WiFi, and enabling SSH. You will also need to install python. For information around this, read the documentation for Raspberry Pi.

Next, install the phue python library on your Raspberry Pi by typing the following command:

pip install phue

Use the below code to create a python file, say motionsensor.py.

You can use an editor such as nano, for creating the python script.

nano motionsensor.py

You can test your code and see if it works by executing the python script from the command line:

python motionsensor.py

The first time you execute this code, you’ll need to push the button on your Philips Hue Bridge to authorize the application to access and send commands to the bridge.

Putty session

Configuring crontab to automatically run the code

Once you’ve satisfactorily tested the code, it’s time to edit crontab so that the motion sensor code executes each time the Raspberry Pi is restarted. The code itself runs in an endless loop so it continues running till the Raspberry Pi device is shutdown or restarted.

First, you’ll need to create a bash script that runs the application. Let’s call this file launcher.sh. You can edit the file using a text editor like nano.

nano launcher.sh

In the launcher.sh file, type the commands you’ll need to execute to run the python file. Save the file.

cd /
cd /home/pi/motionsensor
python motionsensor.py
cd /

You’ll also need to make the file executable with the following command:

chmod +x launcher.sh

Now you’re ready to edit the crontab, and configure the launcher.sh bash script to run at reboot. Use the following commands to edit your crontab:

sudo crontab -e

You can also pipe the screen output from the python code to a log file named after the date and time so that you can maintain log files for troubleshooting. I have also added a command to delete the older log files on a daily basis.

Below is an example of how the crontab file will look like after the required edits (see the last two lines).

For more information on crontab, see the documentation.