A great little sensor you can add to your Raspberry Pi projects is a PIR module. These 5V “Passive Infra Red” sensors are available for a few pounds from eBay. They can be powered from 5V and output 3V so can be connected directly to pins on the Pi’s GPIO header without any other components.
The module sets a single output pin high whenever it detects movement within its field of view. It holds this pin High (3.3V) for a minimum period of time. If continuous movement is detected the output pin will stay High. When the time has elapsed and no more movement is detected the output pin returns Low (0V).
I am currently using one in an alarm system and it works great for such a small and cheap device.
PIR Connections
Here is a diagram showing the pin-out on the PIR module and how I connected it to my Raspberry Pi :

PIR Module
The device has two variable resistors that you can adjust to tweak the performance of the module.
The first one (left-hand side on the photo) determines the sensitivity of the device. The default setting is usually 50%.
The second control (right-hand side on the photo and usually marked “time” on the PCB) allows you to adjust the amount of time the output pin stays at 3V (high) when it is triggered by movement. This can be set from a few seconds to 200 seconds. The default setting is usually a few seconds.
The units available on eBay vary in specification but they are all very similar.
Python Example Script
If you connect your module as shown in the diagram above the following Python script will allow you to get started. Cut and paste the script below into a text file and transfer to the Pi or download the script directly using this link.
#!/usr/bin/python
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k|
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#
# pir_1.py
# Detect movement using a PIR module
#
# Author : Matt Hawkins
# Date : 21/01/2013
# Import required Python libraries
import RPi.GPIO as GPIO
import time
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
# Define GPIO to use on Pi
GPIO_PIR = 7
print "PIR Module Test (CTRL-C to exit)"
# Set pin as input
GPIO.setup(GPIO_PIR,GPIO.IN) # Echo
Current_State = 0
Previous_State = 0
try:
print "Waiting for PIR to settle ..."
# Loop until PIR output is 0
while GPIO.input(GPIO_PIR)==1:
Current_State = 0
print " Ready"
# Loop until users quits with CTRL-C
while True :
# Read PIR state
Current_State = GPIO.input(GPIO_PIR)
if Current_State==1 and Previous_State==0:
# PIR is triggered
print " Motion detected!"
# Record previous state
Previous_State=1
elif Current_State==0 and Previous_State==1:
# PIR has returned to ready state
print " Ready"
Previous_State=0
# Wait for 10 milliseconds
time.sleep(0.01)
except KeyboardInterrupt:
print " Quit"
# Reset GPIO settings
GPIO.cleanup()
This script can also be downloaded onto your Pi directly using this command line :
wget http://www.raspberrypi-spy.co.uk/archive/python/pir_1.py
This can then be run using :
sudo python pir_1.py
When run the script waits for the output pin to go Low. It then prints a message to the screen every time the output state changes. This is either when movement is detected (output changes to High) or the device sees no movement (outout changes to Low).
Try changing the reset time by turning the “time” resistor clockwise by a few degrees. Run the script again, trigger the device and then wait to see how long it takes to go back to the ready state.
Photos
Here some more detailed photos of the PIR pins and two trimming controls :
- PIR Module
Applications
The applications of this circuit include :
- Security systems
- Automatic photographs
- Robot sensors
Related Articles
Here are some of my other articles you might find interesting if you enjoyed this one :






Hi Matt.
Great information as usual.
I don’t suppose you have a link to the eBay lot you purchased from, do you? Or a part number? I have one of these cheap sensors but without the trimmers and the pin-outs do vary a bit!
–
Mike
The last two I got from eBay were from a seller called “abeyerr”. If you search for “PIR Motion Sensor Detector Module” you should see a load that look like the ones in my photos with the two trimmers on them.
I got one locally and also added this URL to my wish list.
http://www.aliexpress.com/item/Free-Ship-10PCS-LOT-HCSR501-HC-SR501-NEW-Adjust-Infrared-IR-PIR-Motion-Sensor-Detector-Module/519751367.html
Thanks Matt. Making a new breakout board to start testing one. To be added to the outdoor camera I am working on.
–
Peter
You can also buy a simplified version (no sensitivity or timing adjustment pot) from http://www.seeedstudio.com. I got mine from their website to use it with an Arduino but I have never hooked it up with the Raspberry Pi.
Just a quick question about the diagram. It says to connect the module to the Pi via pin 2 and 6 for power.
Isn’t Pin 2 for power “input” to the Pi? I read in another article on here that this Pin was used to power the Pi from batteries. So now I wonder how this Pin 2 can output 5V as well as take in 5V.
Can anybody explain?
If you don’t have a supply connected in the “usual” manner by the mini USB port you COULD supply the Raspberry by via it’s GPIO port on Pin 2.
Should you already have the Pi powered up in the normal way, then +5V is presented to the GPIO pin which can then be used to supply other devices. Not to sure what sort of power you can draw from it though, guessing it won’t be all that much.
Hope that helps
Hi!
That was an incredible post and thank to the post you explain how to install RPI GPIO package which helped too. I think a very good post.
By itself the PIR and the script aren’t very useful but as soon as I run this script with a webcam and an automatic email sender script, this will be my next surveillance system
So thanks to you and thanks for taking your time, it really helps.
Hi,
Thanks for this, it is very useful. I would like to suggest an improvement, if you put at the end of the loop but still within the loop a ‘time.sleep(0.01)’ (you will also need to import time). It will pause the software for just 10ms each loop allowing the processor to do other things. if you look at CPU stats, before doing this, python will take up all available processor power now it only take about 2%.
Many thanks