To detect intruders in my living room I used the PIR sensor from a Funduino starter kit. A Python script triggers some appropriate actions when motion is detected.
Set the jumper on the sensor PCB to its inner position. This results in an uninterrupted output signal as long as motion is detected. Connect the VCC pin to the 5V pin of the Rasperry Pi and connect the GND pin to GND.
According to their page (in German) the sensor outputs a 5 V signal. However, I measured it at 3.3 V which is safe for the Pi’s GPIO. I connected the OUT pin to GPIO 27 (whereas other pins were in use for other purposes).
Create a small script to monitor the signal.
pir.py:
#!/usr/bin/python import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(27, GPIO.IN) while True: input_state = GPIO.input(27) if input_state == True: print('Motion Detected') time.sleep(0.1) else: print('No Motion Detected') |
The left trimpot (sensor side up) sets the duration of the signal. A short signal will do to trigger an event. The right trimpot sets the sensor sensitivity. You’ll probably want to experiment with it.
Once satisfied you can write a script that initiates some actions, like capturing video (see ‘Context’), sending an e-mail, turning on the lights or generating speech.
pir_greetings.py:
#!/usr/bin/python import RPi.GPIO as GPIO import datetime import time from subprocess import call GPIO.setmode(GPIO.BCM) GPIO.setup(27, GPIO.IN) last_greeting_time = datetime.datetime(1970, 1, 1); last_motion_time = datetime.datetime(1970, 1, 1); speakers_on = False while True: input_state = GPIO.input(27) now = datetime.datetime.now() if input_state == True: if not speakers_on: call(['./speakers', 'on']) speakers_on = True; last_motion_time = now td_greet = now - last_greeting_time if td_greet.total_seconds() > 15 * 60: call(['./say', 'Hello. Camera is running and you are being recorded. I can see you are really upset about this.']) call(['./motion_mail']) last_greeting_time = now time.sleep(0.1) else: td_motion = now - last_motion_time if td_motion.total_seconds() > 5 * 60 and speakers_on: call(['./speakers', 'off']) speakers_on = False |
We have used a timestamp to prevent any action to repeat itself within the next 15 minutes.
speakers:
#!/bin/sh case "$1" in on) sudo pilight-send -p elro_hc -s 0 -u 27 -t ;; off) sudo pilight-send -p elro_hc -s 0 -u 27 -f ;; esac |
say:
#!/bin/sh IFS=+ /usr/bin/mplayer -ao alsa -really-quiet -noconsolecontrols "http://translate.google.com/translate_tts?tl=en&q=$*" |
motion_mail:
#!/bin/sh echo "http://<camerapage>" | mail -aFrom:noreply@nitri.de -s "Motion Detected" <user>@gmail.com |
One thought on “Motion detection with Raspberry Pi”
Comments are closed.