Detecting movement with Raspberry

Photo from adafruit web site

Photo from adafruit web site

Detecting movement with Raspberry

You need

  • PIR (motion) sensor
50% Complete

PIR (motion) sensor

PIR sensors are used to detect motion from pets/humanoids from about 20 feet away (possibly works on zombies, not guaranteed). This one has an adjustable delay before firing (approx 2-4 seconds), adjustable sensitivity and we include a 1 foot (30 cm) cable with a socket so you can easily reposition the sensor or mount it using the two drills on either side

Runs on 5V-16V power (if you need to run it off of 3V you can do that by bypassing the regulator, but that means doing a bit of soldering). Digital signal output is 3.3V high/low. Sensing range is about 7 meters (120 degree cone)
For a full tutorial with wiring diagrams, code examples and project ideas, PIR sensor tutorial page!

Plug-in the PIR on the Rasberry

Regarding this tutorial you simply have to connect the PIR ground to the raspberry ground, the PIR power to pin 1 or 2 on the raspberry and the output to a GPIO (I will use the 4).

Installing Python (optional)

While my raspberry system come with OSMC, I didn't have a valid Python program installed, so I have to upgrade to a new version with this command :

sudo apt-get install python3-dev

sudo apt-get install mercurial

sudo apt-get install python3-pip

Detecting movement

You need a small Python program to convert PIR's output signal to instructions.

Write the file /home/osmc/

#!/usr/bin/env python

import sys
import time
import RPi.GPIO as io
import subprocess

SHUTOFF_DELAY = 60 # seconds
LED_PIN = 16

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()
            io.output(LED_PIN, io.LOW)
            print ".",
            if turned_off:
                turned_off = False
            if not turned_off and time.time() > (last_motion_time +  SHUTOFF_DELAY):
                turned_off = True
            if not turned_off and time.time() > (last_motion_time + 1):
                io.output(LED_PIN, io.HIGH)

def turn_on():"sh /home/pi/photoframe/", shell=True)

def turn_off():"sh /home/pi/photoframe/", shell=True)

if __name__ == '__main__':
    except KeyboardInterrupt:

Turning the monitor HDMI on and off

Enable HDMI :

Add these two lines to /boot/config.txt and reboot Raspbmc:



hdmi_force_hotplug=1 sets the Raspbmc to use HDMI mode even if no HDMI monitor is detected.
hdmi_drive=2 sets the Raspbmc to normal HDMI mode (Sound will be sent if supported and enabled). Without this line, the Raspbmc would switch to DVI (with no audio) mode by default.

In file

tvservice -o

In file 

tvservice -p; fbset -depth 8; fbset -depth 16

Make files to be executed :

chmod 0744 monitor_*

Turning monitor on/off with a cron job

You can force the monitor to turn on and off with a cron job with a crontab like this one :

(this cron will turn on every day at 10:00, and turn off from Monday to Thursday at 20:30,  Friday at 21:00, Saturday and Sunday at 23:00)

# Allumage de l'ecran (tout les jours a 10:00)
00 10 * * * sh /home/osmc/
# extinction de l'écran
30 20 * * 1-4 sh /home/osmc/
00 21 * * 5 sh /home/osmc/
00 23 * * 6,7 sh /home/osmc/

Copyright © 2015 All rights reserved.