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:
git clone git://git.drogon.net/wiringPi
Download Cowboysdude’s PIR-sensor script from here:
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:
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:
sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart
Add this to the bottom of the file:
Press ctrl+x to save.
Now your script should be all set up, reboot the pi and test if it works:
By default, the monitor shuts off after 20 seconds of no movement. To change this, edit the pir.py script and change this line:
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:
vcgencmd display_power 1
Turn monitor off:
vcgencmd display_power 0