Introduction: PIR Sensor Interfacing With Raspberry Pi

All living beings radiate energy to the surroundings in the form of infrared radiations which are invisible to human eyes. A PIR (Passive infrared) sensor can be used to detect these passive radiations. When an object (human or animal) emitting infrared radiations passes through the field of view of the sensor, it detects the change in temperature and therefore can be used to detect motion.

HC-SR501 uses differential detection with two pyroelectric infrared sensors. By taking a difference of the values, the average temperature from the field of view of a sensor is removed and thereby reducing false positives.

Interfacing HC-SR501 with Raspberry Pi is easy because the output of a sensor is Pi friendly ie. 3.3V and it can be powered from the 5V rail of Pi.

Step 1: Prerequisite:

1. Raspberry Pi setup (screen,mouse,keypad,raspberry pi,AC to 5V DC converter).

2. 3 female to female connector wires

3. A PIR sensor.

4. Some coding skills.

Step 2: Working of HC-SR501 PIR Sensor

The PIR sensor consist of 3 pins:

1. Vcc – 4.5V to 20V, Input power

2. OUTPUT – TTL output of sensor 0V, 3.3V

3. GND – Ground

The module has a rectangular window with two sub-probes 1 and 2 located at two ends of the rectangle. When a body emitting infrared radiation moves from side to side, the time for each probe for detection varies. Larger the time difference, more sensitive the device. It also uses a Fresnel lens to improve sensing aperture and filter in infrared waves.


For adjusting the detection delay (0.3 seconds to 600 seconds): Turn the potentiometer clockwise to increase and anticlockwise to decrease.

For adjusting the sensing distance (3 meters to 7 meters): Turn the potentiometer clockwise to increase and anticlockwise to decrease.

Step 3: Programming

Copy and paste given programme into PYTHON Editor of raspberry pi.

import RPi.GPIO as GPIO #Import GPIO library
import time #Import time library

GPIO.setmode(GPIO.BOARD) #Set GPIO pin numbering

pir = 26 #Associate pin 26 to pir

GPIO.setup(pir, GPIO.IN) #Set pin as GPIO in print "Waiting for sensor to settle"

time.sleep(2) #Waiting 2 seconds for the sensor to initiate print "Detecting motion"

while True:

if GPIO.input(pir): #Check whether pir is HIGH print "Motion Detected!"

time.sleep(2) #D1- Delay to avoid multiple detection

time.sleep(0.1) #While loop delay should be less than detection(hardware) delay

Save this program as

Step 4: Circuit Connections

Vcc, Output, Ground are connected to 2 (5V), 26 (GPIO) and 6 (GND) pins of Pi respectively as shown in the figure.

Step 5: Testing......

Run the program......

Open terminal and goto dir where your program is saved and open that program with using sudo.

Note: The program can be calibrated for smaller detection (hardware) delay by using smaller Program delay(D1).


you will see "Motion Detected" printing on screen when any motion detected by sensor.enjoy..........!

Any doubts or suggestions? Comment below.


blue eng (author)2017-09-20

i have a problem with this code:

when i use this delay :"time.sleep(2) #D1- Delay to avoid multiple detection" raspberry detects 6 times but when i increase the delay to 20 it detects 3 times and when it is 60, it detects 1 times but the delay is too much for sensor to detects next movement. how can i solve it?

syafiqan (author)2017-08-08

what if permission denied after goto dir

About This Instructable




Bio: Hello My name is Prayag Nao.I am a tech enthusiastic person who does not follow reality,instead I create reality.I am sharing some ... More »
More by prayag nao:How to Change or Customize Windows 7/8 Boot Screen Smart Light LampHow to Change Blue Color of Facebook to Your Favorite Color..?
Add instructable to: