PIR Sensor on the Pi
Today I soldered a PIR sensor to my Pi! Basically, I want it to detect movement and turn on a LCD screen, then turn the screen off again after a minute of no movement. So when I walk into a room, the screen turns on and when I leave, the screen turns off.
Equipment
- My Pi (See early Posts)
- PIR Sensor ($10)
- Soldering Iron (Aoyue 937+ is about $63 on Amazon)
- Solder ($8.16 Amazon Prime)
Solder
First thing, I looked up the pinout for the Raspberry Pi. The below diagram comes from elinux.org.
We care about one of the 5V, ground and GPIO25 pins.
- Solder the sensor red cable to either 5V.
- Solder the black cable to ground.
- End by soldering the yellow line to GPIO25.
Your results should be similar to my picture below.
Next, I used this guy’s pir.py script. The script requires the Python library RPi.GPIO. I installed this by downloading the library from here, the direct link is here. To untag or unzip the file I used the following command:
tar -xvf RPi.GPIO-0.5.4.tar.gz
Before installing it, make sure you have python-dev installed.
apt-get install python-dev
With that necessary package, install RPi.GPIO.
cd RPi.GPIO-0.5.4 python setup.py install
Now you can run the pir.py script. I made some slight changes to his code. I didn’t feel the need to call separate scripts to run a single command so I made the following edits.
import subprocess
to
import os
and
def turn_on(): subprocess.call("sh /home/pi/photoframe/monitor_on.sh", shell=True)
def turn_off(): subprocess.call("sh /home/pi/photoframe/monitor_off.sh", shell=True)
this
def turn_on(): os.system("chvt 2")
def turn_off(): os.system("chvt 2")
Run the script and test it out! The sensor will turn off after a minute of no movement and on again once it detects something. I ended by setting my script to run on startup.
I need to put a picture in the frame to act as background to the pi…