PIR Motion Sensor Interfacing with Raspberry Pi using Python
Introduction

PIR Motion Detection Sensor
- PIR sensor is used for detecting infrared heat radiations. This makes them useful in the detection of moving living objects that emit infrared heat radiations.
- The output (in terms of voltage) of PIR sensor is high when it senses motion; whereas it is low when there is no motion (stationary object or no object).
- PIR sensors are used in many applications like for room light control using human detection, human motion detection for security purpose at home, etc.
For more information on PIR sensor and how to use it, refer the topic PIR Sensor in the sensors and modules section.
Interfacing Diagram

PIR Motion Sensor Interfacing with Raspberry Pi
Example
Let’s interface PIR sensor with Raspberry Pi for motion detection.
- When motion is detected, PIR output goes HIGH which will be read by Raspberry Pi. So, we will turn on LED when motion is detected by PIR sensor.
- Here, LED is connected to GPIO12 (pin no. 32) whereas PIR output is connected to GPIO5 (pin no. 29). Let’s write a python based program to interface PIR motion sensor with Raspberry Pi. To know more about how to access GPIO on Raspberry Pi, you can refer Raspberry GPIO Access.
Python Program
'''
Motion detection using PIR on raspberry Pi
http://www.electronicwings.com
'''
import RPi.GPIO as GPIO
PIR_input = 29 #read PIR Output
LED = 32 #LED for signalling motion detected
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) #choose pin no. system
GPIO.setup(PIR_input, GPIO.IN)
GPIO.setup(LED, GPIO.OUT)
GPIO.output(LED, GPIO.LOW)
while True:
#when motion detected turn on LED
if(GPIO.input(PIR_input)):
GPIO.output(LED, GPIO.HIGH)
else:
GPIO.output(LED, GPIO.LOW)
No comments:
Post a Comment