Raspberry Pi GPIO with PIR motion sensor: Best tutorial

This tutorial, based on the latest Raspberry Pi B+/model 2, will show you to interface a PIR motion sensor with the Raspberry Pi. And also guides you to use the GPIO pins on it. The GPIO pins on the raspberry pi are critical when it comes to making a hardware project. May it be a robot, home automation system, etc. In all these cases you will have to use the GPIO (General Purpose Input/Output) pins of the raspberry pi. With this simple tutorial, you will be able to learn to control the output on the GPIO pins. And also read inputs through it. Moreover, you will get to read the output from a PIR motion sensor and also write a simple code to blink an LED. And if you are a true beginner, you can always use our free eBook on Raspberry Pi and Arduino to get started from step 0. So gear up and get ready to have fun with the Raspberry Pi GPIO s

What are the stuff required to do this?

  1. Raspberry Pi B/B+ or 2 and basic peripherals: SD card, keyboard, mouse, etc.
  2. An LED and 220Ohm resistor.
  3. PIR motion sensor.
  4. Breadboard.
  5. Male to male and Female to Male jumper wires.

Raspberry Pi GPIO with PIR motion sensor Best tutorial

How does this work?

The raspberry pi GPIO can be accessed through a python program. You will get to know about how to access these pins and the commands required to do that, later in this tutorial. Each pin on the raspberry pi is named based on its order (1,2,3,..) as shown in the below diagram:

Here, we are using a PIR motion sensor. PIR stands for Passive InfraRed. This motion sensor consists of a fresnel lens, a infrared detector and supporting detection circuitry. The lens on the sensor focuses any infrared radiation present around it towards the infrared detector. Our bodies generate infrared heat and as a result this gets picked up by the motion sensor. The sensor outputs a 5V signal for a period of one minute as soon as it detects the presence of a person. It offers a tentative range of detection of about 6-7 m and is highly sensitive. When the PIR motion sensor detects a person, it outputs a 5V signal to the raspberry pi through its GPIO. And we define what the raspberry pi should do as it detects an intruder through python coding. Here we are just printing: “Intruder detected”.

Raspberry Pi GPIO with PIR motion sensor Best tutorial Diagram

Step 1: Blinking an LED using the Raspberry Pi GPIO- Output GPIO control

After you have setup the raspberry pi, we can now start messing around with its GPIO pins.  Here, we will try to blink an LED using a python script. Copy and paste the following code into your raspberry pi. You can do this by opening the text editor: “leafpad” on the raspberry pi. And then copy this code into it and save this as a python file: ledblink.py :

import RPi.GPIO as GPIO
import time
GPIO.setup(3,GPIO.OUT)     #Define pin 3 as an output pin

while True:
        GPIO.output(3,1)   #Outputs digital HIGH signal (5V) on pin 3
        time.sleep(1)      #Time delay of 1 second

        GPIO.output(3,0)   #Outputs digital LOW signal (0V) on pin 3
        time.sleep(1)      #Time delay of 1 second

Next, we need to connect the LED to pin 3 of the raspberry pi GPIO. You can check out the connection diagram below for doing that:


For more detail: Raspberry Pi GPIO with PIR motion sensor Best tutorial

About The Author

Ibrar Ayyub

I am an experienced technical writer with a Master's degree in computer science from BZU Multan University. I have written for various industries, mainly home automation, and engineering. I have a clear and simple writing style and am skilled in using infographics and diagrams. I am a great researcher and is able to present information in a well-organized and logical manner.

Follow Us:
Scroll to Top