PIR Motion Sensor Interfacing with Raspberry Pi using Python

Overview of PIR Sensor

PIR Motion Sensor
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 the 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 room light control using human detection, human motion detection for security purposes 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.

 

Connection Diagram of PIR Sensor with Raspberry Pi

PIR interfacing with Raspberry Pi for motion detection
PIR Motion Sensor Interfacing with Raspberry Pi

 

 

Note:

  • PIR sensor: Never keep PIR Sensor close to the Wi-Fi antenna, ESP32, or NodeMCU.
  • PIR (Passive Infrared) sensor close to a WiFi antenna impacts the sensor's performance.
  • PIR sensors detect changes in infrared radiation for motion detection.
  • WiFi signals emit electromagnetic radiation that can interfere with the PIR sensor. Which causes false detection.
  • So always keep the PIR sensor and WiFi antenna as far apart as possible.
  • Also, you can try to shield the PIR sensor from the WiFi signal. This can be done by using metal shields or Faraday cages around the PIR sensor.

 

Example

Let’s interface the 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 the LED when motion is detected by the PIR sensor.
  • Here, the LED is connected to GPIO12 (pin no. 32) whereas the PIR output is connected to GPIO5 (pin no. 29). Let’s write a python based program to interface the PIR motion sensor with Raspberry Pi. To know more about how to access GPIO on Raspberry Pi, you can refer Raspberry GPIO Access.

 

 

PIR Sensor Python Program for Raspberry Pi

'''
	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)

Components Used

PIR Sensor
PIR motion sensors sense the Infrared signal radiated from moving objects including human or animal body. It is generally used to detect the presence of human or animal motions.
1
Raspberry Pi 4B
Raspberry Pi 4BRaspberry Pi 4B
1
Raspberry Pi Zero
Raspberry Pi Zero
1

Downloads

PIR interface with Raspberry Pi using Python Download
Ad