Raspberry Pi and PIR example

In this article we will connect a PIR to a raspberry Pi and light an LED if motion detected, in real life you would use a buzzer or sounder.

Lets look at a PIR

A passive infrared sensor (PIR sensor) is an electronic sensor that measures infrared (IR) light radiating from objects in its field of view. They are most often used in PIR-based motion detectors.

A PIR-based motion detector is used to sense movement of people, animals, or other objects. They are commonly used in burglar alarms and automatically-activated lighting systems. They are commonly called simply “PIR”, or sometimes “PID”, for “passive infrared detector”

An individual PIR sensor detects changes in the amount of infrared radiation impinging upon it, which varies depending on the temperature and surface characteristics of the objects in front of the sensor. When an object, such as a human, passes in front of the background, such as a wall, the temperature at that point in the sensor’s field of view will rise from room temperature to body temperature, and then back again. The sensor converts the resulting change in the incoming infrared radiation into a change in the output voltage, and this triggers the detection. Objects of similar temperature but different surface characteristics may also have a different infrared emission pattern, and thus moving them with respect to the background may trigger the detector as well.


Parts List

Raspberry Pi3Raspberry Pi 3 Model B With WiFi & Bluetooth
PIR module1PCS/LOT HC-SR501 HCSR501 SR501 human infrared sensor module Pyroelectric infrared sensor imports probe
connecting wireFree shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire
LED200pcs/box 3mm 5mm LED Light Assorted Kit Red Green Blue Yellow White LEDs Diode Set
resistors600PCS Component Kit 1% 1/4W Resistor Pack



We used GPIO4 and GPIO17 – you can use any GPIO pin, just remember and change the pins used in the code

pi and plr
pi and plr


Save this example as pirled.py

import RPi.GPIO as GPIO
import time
sensor = 11
led = 7
print "Initialzing PIR"
print "PIR Ready"
print " "
   while True:
      if GPIO.input(sensor):
          print "Motion Detected"
          while GPIO.input(sensor):
except KeyboardInterrupt:

Copy this to your raspberry pi and open up a terminal and type in

sudo python pirled.py