Hardik Rathod
Published © GPL3+

PIR Motion Sensor with Raspberry Pi

Quick project to interface PIR sensor to Raspberry Pi.

BeginnerProtip30 minutes72,791
PIR Motion Sensor with Raspberry Pi

Things used in this project

Hardware components

Raspberry Pi 2
×1
PIR Motion Sensor
×1
Buzzer (optional)
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper Wires
×1

Story

Read more

Schematics

Schematic

Code

PIR.py

Python
Create new python file PIR.py and enter the following code.
To run the code, open the terminal and go to the directory where your PIR.py is located. Then enter the command " sudo python PIR.py " and hit enter
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN) #PIR
GPIO.setup(24, GPIO.OUT) #BUzzer

try:
    time.sleep(2) # to stabilize sensor
    while True:
        if GPIO.input(23):
            GPIO.output(24, True)
            time.sleep(0.5) #Buzzer turns on for 0.5 sec
            GPIO.output(24, False)
            print("Motion Detected...")
            time.sleep(5) #to avoid multiple detection
        time.sleep(0.1) #loop delay, should be less than detection delay

except:
    GPIO.cleanup()

Credits

Hardik Rathod

Hardik Rathod

1 project • 33 followers

Comments