Anth's Computer Cave

Use a Movement sensor with your Raspberry Pi

Today I will show you how to connect a movement sensor to your Raspberry Pi and write a Python script to determine if a room is occupied.

The sensor

A cheap PIR movement sensor from Ebay

The movement sensor I am using is an infrared PIR motion detector commonly available on Ebay for a couple of dollars. Click here to see a similar unit.

These devices have three pins. The power pin will generally be labelled "Vin", or "Vcc", and connects to a 5V pin on the Raspberry Pi. The next pin may be labelled "Out", or "Output" or "Send". This connects to a GPIO pin on the Pi, in my case GPIO23. It requires a resistor inline to protect the Pi (see diagram below). Opinions vary about the Ohm value of this resistor, I have used a 1000 Ohm resistor and that seems to work fine. The final pin is GND and that connects to any of the GND pins on your Pi.

A connection diagram for the movement sensor and the Raspberry Pi.
The layout I am using for my movement sensor.

Please note that the pin diagram above refers to my Raspberry Pi (model B). If you have an older version of the Pi you should check that the board has the same pinouts.

These PIR sensors have two adjustment screws, though I have had no need to adjust anything. From what I can gather one of these screws adjusts the field of the sensor(the width of the sensing area). The other adjusts the sensitivity, which also relates to the range (the distance over which you wish to sense movement).

My requirements

The AAIMI Home Automation Project needs to know when rooms are occupied. It will then use that knowledge, along with data from the light sensor we made last week, to determine when to switch lights on or off.

I will go into further details about the actual method used to manipulate the lights in future articles, but for now I am going to show you how to use the sensor to set a Python variable called "Occupied" to either "yes" or "no".

The code

The following code will check for movement every three seconds, and keep a running time record of the last instance of movement. It will change the "Occupied" variable to "no" if it senses no movement for two minutes. The next time it senses movement it will change the variable to "yes", and the cycle will begin again.

#Room monitoring script for Raspberry Pi
#By Anthony Hartup

import time
import RPi.GPIO as GPIO

move_time = [0, 0] #variable to keep the last time that movement occured
occupied = "no" 
move_pin = 23 

GPIO.setmode(GPIO.BCM) #Set the Pi to use GPIO numbers
GPIO.setup(move_pin, GPIO.IN) #Set the GPIO pin as input.

while True:
	if ( GPIO.input(move_pin) == True ):
		#Record the hour that last movement occurred
		move_time[0] = time.strftime("%H")
		#Record the minute that last movement occurred 
		move_time[1] = time.strftime("%M") 
		if occupied == "no":
			occupied = "yes"
		if occupied == "yes":
			#Check the elapsed minutes since movement occurred and
			#set variable to False if more than two minutes
			if int(time.strftime("%M")) - int(move_time[1]) > 2:
				print("Room vacant at:")
				print(time.strftime("%H"), ":", time.strftime("%M"))
				occupied = "no"



Previous: Build a cheap and easy light-sensor

Next:Build a Relay Circuit to Control AC Power



Leave a comment on this article