How to: Install a PIR motion sensor on your Raspberry Pi Magic Mirror

Some people don’t want their magic mirror to be constantly on, for example during the night or when no one’s home. There is an easy solution to this, just install a PIR sensor!

You will need a PIR sensor, they are really cheap online, here are a couple of options:

So let’s get down to how to set this up..

1. Hook up the PIR module to your Raspberry Pi

This is pretty easy, you connect three Female/Female jumper cables to the PIR module.

the other end of the cables go to these pins on the Raspberry Pi 3 pins:

  • 5v goes to pin 2 on the raspberry pi
  • The gnd (ground) wire goes to pin 6 on the pi
  • The middle wire goes to pin 11 (GPIO #17 – See diagram here)

2. Install WiringPi & Other stuff

Type the following into your Pi’s terminal window to install WiringPi:

1
git clone git://git.drogon.net/wiringPi

Download Cowboysdude’s PIR-sensor script from here:
https://github.com/cowboysdude/Pir-Sensor/archive/master.zip

Upload the files to your Pi’s root folder, otherwise it won’t work without modifying the script.

Now make your files executable. From a terminal, run the following commands in the root directory:

1
2
3
4
5
sudo chmod +x pir.py

sudo chmod +x monitor_off.sh

sudo chmod +x monitor_on.sh

3. Make your script run on boot

From a terminal, enter the following command:

1
sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart

Add this to the bottom of the file:

1
@/usr/bin/python /home/pi/pir.py

Press ctrl+x to save.

Now your script should be all set up, reboot the pi and test if it works:

1
sudo reboot

By default, the monitor shuts off after 20 seconds of no movement. To change this, edit the pir.py script and change this line:

1
SHUTOFF_DELAY = 20 # seconds

Getting a black screen when motion is detected?

try replacing the tvservice command in the monitor_on & monitor_off files with these ones to turn the monitor on/off:

Turn monitor on:

1
vcgencmd display_power 1

Turn monitor off:

1
vcgencmd display_power 0
Facebook Comments

You may also like...

6 Responses

  1. Brian says:

    Great article, just had one question if you could help. Followed the instructions and when I reboot, everything starts up fine like it should. Magic Mirror comes and and after 20 seconds the display shuts off. But when activating the motion sensor the screen lights up but with nothing displaying on it, just a black screen. Checked Wiring Pi and it is up to date and double checked my wiring placement (2 for 5v, 6 for gnd, 11 for middle wire). Just not sure what I am missing, any ideas? Thanks.

  2. Brian says:

    Thanks for the quick response, the added code works but it zooms the screen way in and turns the background from black to yellow so its progress. Thanks for your help ill continue to mess with some settings until its up and running. Again thanks for the awesome article 🙂

    • Jon says:

      No worries, glad to help.

      I found two additional commands you can try, I’ll post them here in case someone has the same problem as you.

      in the monitor_on.sh file, replace the tvservice command with this one:
      vcgencmd display_power 1

      and put this one on the monitor_off script:
      vcgencmd display_power 0

      could possibly work!

  3. Brian says:

    man you are great! Thanks for all your help, works perfect now with those new commands. Appreciate taking the time to help me out 🙂

    • Jon says:

      Great to hear Brian, I’ll add these commands to the tutorial in case the other ones don’t work for some people 😀