MBTechWorks.com    

Interface PIR Motion Sensor with Raspberry Pi

November 2, 2017

Demonstrate the use a PIR (Passive Infrared) sensor with the Raspberry Pi to turn on an LED when a person (or warm-blooded animal) comes within range.

HC-SR501 PIR

HC-SR501 PIR Fresnel Lens

HC-SR501 PIR

HC-SR501 PIR bottom view

We will use the HC-SR501 PIR motion sensor as it is inexpensive and easy to use. The sensor works by detecting movement of infrared radiation emitted from warm objects (people). In this project, we will turn on an LED when movement is detected.

Connecting the PIR to a Raspberry Pi

Interfacing to the Raspberry Pi requires only a few connections. Programming is done using Python language. Connect the Raspberry Pi as shown below.

HC-SR501 PIR

HC-SR501 PIR bottom view

Connect the wires as shown above to the bottom side of the PIR. Turn the PIR over and then connect the PIR wires to the breadboard.

HC-SR501 PIR connections to Raspberry Pi

HC-SR501 PIR connections to Raspberry Pi

As always, double check your connections!! You do not wish to “fry” your Pi due to incorrect wiring.

The Python Program

Following is the Python source code used for this demo. You can install the program in either of two ways.

A. Key the source code below into Raspberry Pi’s Text Editor. Save it into the Pi folder as pir.py

– or –

B. If the Pi is connected to the internet, you can download it from our website as follows.

  1. Launch the Terminal program (command line interface)
  2. Type: wget -O /home/pi/pir.py https://www.mbtechworks.com/files/pir.py
# MBTechWorks.com 2017
# Use an HC-SR501 PIR to detect motion (infrared)

#!/usr/bin/python

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD) #Set GPIO to pin numbering
pir = 8 #Assign pin 8 to PIR
led = 10 #Assign pin 10 to LED
GPIO.setup(pir, GPIO.IN) #Setup GPIO pin PIR as input
GPIO.setup(led, GPIO.OUT) #Setup GPIO pin for LED as output
print ("Sensor initializing . . .")
time.sleep(2) #Give sensor time to startup
print ("Active")
print ("Press Ctrl+c to end program")

try:
while True:
if GPIO.input(pir) == True: #If PIR pin goes high, motion is detected
print ("Motion Detected!")
GPIO.output(led, True) #Turn on LED
time.sleep(4) #Keep LED on for 4 seconds
GPIO.output(led, False) #Turn off LED
time.sleep(0.1)

except KeyboardInterrupt: #Ctrl+c
pass #Do nothing, continue to finally

finally:
GPIO.output(led, False) #Turn off LED in case left on
GPIO.cleanup() #reset all GPIO
print ("Program ended")
    

Time to detect!

Once you have the Python program on the Raspberry Pi, launch the command Terminal and type the following command in the Terminal (in the same directory where you put the python program, which should be /home/pi ).

sudo python pir.py

Step away from the sensor and let it timeout to turn LED off. Approach the sensor and it should turn LED on. Try changing the Sensitivity Adjust and Time Out Adjust as desired for sensor range and LED time on.

Press Ctl c to end the python program.

There you go. You have a Raspberry Pi that can sense an approaching person. Further programming and Pi configuration can be done to do other things in response, such as activating an alarm or annunciator, turn lights on / off, trigger a camera, . . .