Raspberry Pi GPIO: PIR Sensor Control

raspberry pi logo

Motion Detection for Your Pi!

In this guide, we’ll explore the exciting world of motion detection and automation using a Passive Infrared (PIR) sensor with your Raspberry Pi GPIO. PIR sensors are perfect for creating projects related to home security, energy conservation, and interactive installations. We’ll cover the wiring, Python programming, and demonstrate how to detect motion and trigger actions with your Raspberry Pi. Let’s get started on the path to motion-powered projects!

Materials Needed:

Before we begin, make sure you have the following materials ready:

  • Raspberry Pi (any model with GPIO pins)
  • PIR (Passive Infrared) sensor
  • Breadboard and jumper wires
  • Power supply for your Raspberry Pi

About the PIR Sensor

PIR stands for Passive Infrared. The PIR sensor detects heat and movement and will not trigger without both conditions being satisfied. This stops the PIR triggering on events like hot spots caused by the sun for example, or equipment heating up. There must be motion as well as heat to trigger the sensor.

The PIR uses a Fresnel lens to give an extra wide range of view for the sensor.

On the back of the sensor, you will notice 2 potentiometers, one is used for sensitivity and the other is used for a time delay between triggers.

  • The left pot turns clockwise for greater sensitivity.
  • The right pot turns clockwise to increase delay time between detections (Minimum 7 seconds).

You will also notice there is a header jumper, this is the retrigger control. If positioned to the edge of the PCB it will retrigger after an event, if positioned to the inside of the PCB it will not retrigger after an event until its reset.

The Circuit:

Follow the circuit diagram below, the connections are as follows:

  • Connect the VCC (power) pin of the PIR sensor to a 5V pin on the Raspberry Pi.
  • Connect the GND (ground) pin of the PIR sensor to a ground (GND) pin on the Raspberry Pi.
  • The OUT (signal) pin of the PIR sensor should be connected to GPIO 12.
RPI PIR

Python Code:

Now, let’s write the Python code to detect motion using the PIR sensor. You can use the RPi.GPIO library to interact with the GPIO pins.

import RPi.GPIO as GPIO
import time

sensorPin=12

GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensorPin, GPIO.IN)


try:
    while True:
        motion=GPIO.input(sensorPin)
        print(motion)
        if (motion==1):
            print('Motion Detected')
        else:
            pass

        
except KeyboardInterrupt:
    GPIO.cleanup()
    print('GPIO Ready')
Explanation:
  • Import the RPi.GPIO library and set the GPIO mode to BCM.
  • Define the GPIO pin connected to the PIR sensor and set it up as an input.
  • Continuously monitor the sensor’s output and print messages based on motion detection.

Conclusion

With the PIR sensor and your Raspberry Pi, you’ve unlocked the potential for various motion-related projects. You can expand on this by integrating it into a security system, automating lighting, or creating interactive art installations. Explore the limitless possibilities of motion detection and automation.

Happy Tinkering Folks!

You can explore more of our Raspberry Pi guides here: Raspberry Pi for Beginners

Unlock the Possibilities

We have put together a list of some great Raspberry Pi products and deals: Shop Raspberry Pi Goodies Now! 

Luke Barber

Hello, fellow tech enthusiasts! I'm Luke, a passionate learner and explorer in the vast realms of technology. Welcome to my digital space where I share the insights and adventures gained from my journey into the fascinating worlds of Arduino, Python, Linux, Ethical Hacking, and beyond. Armed with qualifications including CompTIA A+, Sec+, Cisco CCNA, Unix/Linux and Bash Shell Scripting, JavaScript Application Programming, Python Programming and Ethical Hacking, I thrive in the ever-evolving landscape of coding, computers, and networks. As a tech enthusiast, I'm on a mission to simplify the complexities of technology through my blogs, offering a glimpse into the marvels of Arduino, Python, Linux, and Ethical Hacking techniques. Whether you're a fellow coder or a curious mind, I invite you to join me on this journey of continuous learning and discovery.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights