Home ยป Raspberry Pi and PIR example

Raspberry Pi and PIR example

by shedboy71

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

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

 

Layout

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

Code

Save this example as pirled.py

import RPi.GPIO as GPIO
import time

sensor = 11
led = 7

GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor,GPIO.IN)
GPIO.setup(led,GPIO.OUT)

GPIO.output(led,False)
print "Initialzing PIR"
time.sleep(12)
print "PIR Ready"
print " "

try: 
   while True:
      if GPIO.input(sensor):
          GPIO.output(led,True)
          print "Motion Detected"
          while GPIO.input(sensor):
              time.sleep(0.2)
      else:
          GPIO.output(led,False)


except KeyboardInterrupt:
    GPIO.cleanup()

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

sudo python pirled.py

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More