Raspberry Pi Projects

Interface PIR Sensor With Raspberry PI

Introduction

In this guide, we will show you how to use a PIR sensor with Raspberry Pi and write Python code to detect motion. PIR Sensors are used to detect movement and are often used in security systems. In this guide, we will use a PIR Sensor to turn an LED on when movement is detected.  In this tutorial, you will learn how to set up a PIR sensor with a Raspberry Pi. You will also learn how to use the PIR sensor to detect when someone enters the room. check Also What is Raspberry Pi And Their Uses – Beginner’s Guide

Required Material

What is a PIR sensor?

Interface PIR Sensor With Raspberry PI
PIR Sensor

A passive infrared sensor (PIR sensor) is an electronic sensor that t is used to detect the presence of an object or person and measure it. infrared (IR) light emitted from objects in their field of view. They are most often used in PIR-based motion detectors. A PIR sensor is a sensor that detects motion. PIR sensors are commonly used in security systems and are used to detect when someone is in a room.

PIR sensors are used in a variety of applications, including security systems, automatic lighting controls, and energy-saving devices.

PIR sensor Applications

The applications of this circuit include :

  • Security systems
  • Automatic photographs
  • Robot sensors

How does a PIR sensor work?

 

Ad

PIR sensors can also be used to detect the movement of objects, such as in a motion detector. PIR sensors work by detecting the infrared radiation emitted by objects. When an object is heated, it emits infrared radiation. This radiation is invisible to the human eye but can be detected by a PIR sensor. The sensor contains a special material that is sensitive to infrared radiation. When the sensor is exposed to infrared radiation, the material produces a small electrical current. This current is then used to trigger an alarm or activate a camera. PIR sensors are very sensitive, and can even detect the heat emitted by a human body.

Interface PIR Sensor With Raspberry PI

The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in academies and developing countries. and PIR sensors are a simple and useful way to add security to a home or office.

How to set up a PIR sensor with Raspberry Pi?

Raspberry Pi GPIO pin

Ad

The GPIO pins can be used to interface the Pi with external devices such as sensors, motors, and LEDs. They can also be used to interface the Pi with other computers and devices such as the Arduino.

The pinout of the GPIO header is shown below:

Raspberry Pi GPIO pin
Image Credit -Raspberrypi.UK

The GPIO provides the following power and interface options :

  • 3.3V (2 pins)
  • 5V (2 pins)
  • Ground (8 pins)
  • General-purpose input and output
  • PWM (pulse width modulation)
  • I2C
  • I2S
  • SPI
  • Serial

These allow a huge range of sensors, motors, and LEDs to be connected to the Raspberry Pi.

Schematic Diagram of PIR Sensor With Raspberry PI

Here is a circuit diagram of PIR Sensor and Raspberry Pi :

Wire color Sensor Raspberry pi
① Red 5V
② Yellow OUT GPIO 18
③ Black GND

pir-sensor-with-raspberry-pi

Now, connect your Raspberry Pi GPIO PIN to the PIR motion sensor as per the connection diagram above:

Python Code Script for PIR Sensor With Raspberry PI

This program can read the sensor signal when motion detects and print on the serial monitor.

Copy the following code and paste into Thonny IDE and save it as a Python file: Main.py

Getting started with Raspberry PI:- https://projects.raspberrypi.org/getting-started-with-raspberry-pi

# Import required Python libraries
import RPi.GPIO as GPIO
import time
 
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
 
# Define GPIO to use on Pi
GPIO_PIR = 7
 
print "PIR Module Test (CTRL-C to exit)"
 
# Set pin as input
GPIO.setup(GPIO_PIR,GPIO.IN)      # Echo
 
Current_State  = 0
Previous_State = 0
 
try:
 
  print "Waiting for PIR to settle ..."
 
  # Loop until PIR output is 0
  while GPIO.input(GPIO_PIR)==1:
    Current_State  = 0
 
  print "  Ready"
 
  # Loop until users quits with CTRL-C
  while True :
 
    # Read PIR state
    Current_State = GPIO.input(GPIO_PIR)
 
    if Current_State==1 and Previous_State==0:
      # PIR is triggered
      print "  Motion detected!"
      # Record previous state
      Previous_State=1
    elif Current_State==0 and Previous_State==1:
      # PIR has returned to ready state
      print "  Ready"
      Previous_State=0
 
    # Wait for 10 milliseconds
    time.sleep(0.01)
 
except KeyboardInterrupt:
  print "  Quit"
  # Reset GPIO settings
  GPIO.cleanup()

This can then be run using :

 sudo python pir_1.py

When run the script waits for the output pin to reach Low. It then prints a message on the screen every time the output changes. This is either when motion is detected (output changes to High) or the device sees no motion (output changes to Low).

Try modifying the reset time by rotating the “time” resistor clockwise by a few degrees. Run the script again, trigger the device, and then wait to see how long it takes to go back to the ready state.

Applications

The applications of this Project:

  • Security systems
  • Automatic doors
  • Robot sensors

Conclusion

 

Read Similar Articles:

Leave a Reply

Your email address will not be published.

Related Articles

Ad
Back to top button