Hardik Rathod
Published © GPL3+

PIR Motion Sensor with Raspberry Pi

Quick project to interface PIR sensor to Raspberry Pi.

EasyProtip30 minutes20,630
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
×1
Jumper Wires
×1

Story

Read more

Schematics

Schematic

Untitled sketch bb coksgn73yn

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 • 10 followers
Contact

Comments