Programming Raspberry Pi for motion detection using PIR sensor
AIM: Programming Raspberry Pi to detect motion using PIR Sensor
OBJECTIVES:
a. To understand the basics of the GPIO Pins of Raspberry Pi.
b. To study the basic of PIR sensor.
c. To write a python program to detect any human motion using PIR Sensor.
CIRCUIT/ BLOCK DIAGRAM:
Note: Either Buzzer or LED can be interfaced to GPIO 20 pin. Both cannot be connected simultaneously.
1|Page
GPIO Pin Connection for PIR, Buzzer and LED:
Pin on Kit Pins on Raspberry Pi
GPIO Pins Pin Nmber
Buzzer/ LED GPIO 20 38
PIR Sensor GPIO 21 40
+ 3.3V - 1
+ 5V - 2
GND - 6
Procedure:
Note: The Raspbian operating system comes with Python already installed in it.
1. Setting up the circuit:
a. Turn OFF the Raspberry Pi while building/connecting the circuit board/components
according to diagram.
b. Then turn the Raspberry Pi and check the Raspbian OS loaded properly or not. If not
then check the circuit connection again.
2. Python Programming: Python via IDLE
a. Start Raspberry Pi in desktop mode, open the Applications Menu in the top left of
your screen, and navigate to Programming > Python 3 (IDLE) /. This will open the
Python shell.
2|Page
b. Write a program for PIR sensor and Buzzer interfacing with raspberry pi and save as
with pir.py
Program: # PIR sensor and Buzzer interfacing with raspberry pi
importRPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM) #choose GPIO
GPIO.setwarnings(False)
PIR_input=21 #read PIR Output
BUZZ=20 #BUZZ for signaling motion detected
GPIO.setup(PIR_input, GPIO.IN)
GPIO.setup(BUZZ, GPIO.OUT)
GPIO.output(BUZZ, GPIO.LOW)
while True:
#when motion detected turn on BUZZ
if(GPIO.input(PIR_input)):
GPIO.output(BUZZ, GPIO.HIGH)
else:
GPIO.output(BUZZ, GPIO.LOW)
Steps to execute Program
1. Make connections as given above
2. Open ‘PIR_BUZZER.py’ file in python 3 IDE
3. Select Run from top menu and click Run Module
3|Page