Motion sensor

I purchased a motion sensor from Lee’s Electronics in Vancouver for ~$15.

Connecting the sensor to the Pi was simple enough with some female-to-female connectors. The three wires plug into Pin 2 (5V), Pin 6 (ground) and Pin 7 (GPIO 4). GPIO 4 is then monitored by the Pi to detect motion. I followed an online tutorial for creating a Python script to detect motion and turn on/off the monitor. I made a few modifications to the script for my own purposes:

import sys

import time

import RPi.GPIO as io

import subprocess

io.setmode(io.BCM)

SHUTOFF_DELAY = 60 # seconds

PIR_PIN = 4

def main():

io.setup(PIR_PIN, io.IN)

io.setup(LED_PIN, io.OUT)

turned_off = False

last_motion_time = time.time()

while True:

if io.input(PIR_PIN):

last_motion_time = time.time()

sys.stdout.flush()

if turned_off:

turned_off = False

turn_on()

else:

if not turned_off and time.time() > (last_motion_time + SHUTOFF_DELAY):

turned_off = True

turn_off()

if not turned_off and time.time() > (last_motion_time + 1):

io.output(LED_PIN, io.HIGH)

time.sleep(.1)

def turn_on():

subprocess.call(“sh /home/pi/mirror/monitor_on.sh”, shell=True)

def turn_off():

subprocess.call(“sh /home/pi/mirror/monitor_off.sh”, shell=True)

if __name__ == ‘__main__’: try: main() except KeyboardInterrupt:

io.cleanup()

Leave a Reply