How to Interface a PIR Motion Sensor With Raspberry Pi GPIO

Raspberry pi GPIO pin tutorial


This tutorial will show you to interface a PIR motion sensor with the Raspberry Pi and how 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, or whatever crazy creation you come up with. In all these cases you will have to use the GPIO (General Purpose Input/Output) pins on the Raspberry Pi. With this simple tutorial, you will be able to learn how to control the output on the GPIO pins. and read inputs through them. Moreover, you will get to read the output from a PIR motion sensor and write a simple code to blink an LED. If you’re not familiar with the Rasbperry Pi terminal, check out this tutorial on Basic Linux Commands. 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 some fun with the Raspberry Pi GPIOs!

Required Materials

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

How Does it Work?

The Raspberry Pi GPIO can be accessed through a python program. You will get to know 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 diagram below:



Here, we are using a PIR motion sensor. PIR stands for Passive InfraRed. This motion sensor consists of a fresnel lens, an infrared detector, and supporting detection circuitry. The lens on the sensor focuses any infrared radiation present around it toward the infrared detector. Our bodies generate infrared heat, and as a result, this heat is 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 meters 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 the python coding. Here we are just printing: “Intruder detected”.


How the PIR Motion Sensor works

Blinking an LED Using the Raspberry Pi GPIO- Output GPIO Control

After you have set up your 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 your Raspberry Pi and copying this code into it and save this as a python file: :


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 on the Raspberry Pi GPIO. You can check out the connection diagram below to do that:



Raspberry Pi GPIO LED connection diagram


You should notice that the LED starts blinking after you execute the python program. Using this command: sudo python The LED blinks because it receives a HIGH (5V) signal and a LOW (0V) signal from the Raspberry Pi GPIO at a delay of one second. You can check out the video below for a demo:













Interfacing the PIR Motion Sensor to the Raspberry Pi’s Input GPIO Read

Now, we can try reading the output from the PIR motion sensor. The sensor outputs a digital HIGH (5V) signal when it detects a person. Copy and paste the following code into your Raspberry Pi and save it as a python file:

import RPi.GPIO as GPIO
import time
GPIO.setup(11, GPIO.IN)         #Read output from PIR motion sensor
GPIO.setup(3, GPIO.OUT)         #LED output pin
while True:
       if i==0:                 #When output from motion sensor is LOW
             print "No intruders",i
             GPIO.output(3, 0)  #Turn OFF LED
       elif i==1:               #When output from motion sensor is HIGH
             print "Intruder detected",i
             GPIO.output(3, 1)  #Turn ON LED


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


Raspberry Pi PIR motion sensor connection


You will notice that this code prints: “Intruder detected” when you place your hand over the sensor. After removing your hand and waiting some time, it prints: “No intruders”.

In certain PIR motion sensors, you can even adjust the delay at which the sensor outputs a HIGH signal at the expense of compromising the accuracy. You just need to turn the two knobs on the sensor counterclockwise using a screwdriver.


Raspberry pi GPIO pin tutorial

PIR Motion Sensor pin out


Raspberry pi GPIO pin tutorial

PIR motion sensor adjustment knobs


You can also extend the display of your laptop to the Raspberry Pi via a VNC server and a LAN cable just like how I did in the below video.

You can see this sensor in action, in the video below:











Recommended Posts
Showing 40 comments
  • Bojengle

    Motion detection is awesome, what a nice little sensor that is. I’m not sure if I want to use motion detection or not for my dog treat spy camera/ treat dispenser. I think after I build my doggo machine I will do a motion detecting camera! Thanks for this information.

  • Victor

    Where can i by all this stuff? i live in Denmark, so the source must be from Denmark or within Europe.


    • Andrew

  • JimBob Electronics from the Ozarks

    Woah! Be careful of your IR wiring.
    With my device is the opposite of the photos here. VCC (+ side) is in the opposite position.
    With your own device verify by taking over the plastic lens (comes off easy with a fingernail or small screwdriver.
    On the top it should have silkscreened labels “VCC”, “OUT” and “GRND”.
    Might explain why some people are reporting failures here.

  • Raki

    Hi team,

    Please help me as i tried the same connection but not sure neither the led is blinking nor the sensor is working fine.
    My Observation is:

    When i touch the breadboard connection with my hand , the led glows and message as Intruder detected comes. without even touching the sensor …surprising

    is that something to do with loose connection on bread baord as i checked many time but still not through ?

    1) is that my PIR sensor is having any issue?
    2) what power input i shud give 3.3 V or 5 V for the PIR Sensor ?
    3) is that i am making any mistake with GND connection?

    Please help


    • JimBob Electronics from the Ozarks

      5V as the device has a regulator that’ll drop down to 3.3v on board.
      See my other comment, you might have fried the said regulator if did your IR wiring like the photo.

  • himanshu

    hello sir
    if i stay acrooss pi sencer for 1 minit then sencer ouput is high for 1 minat ?

  • Rorian

    I also had +5/GND reversed, spent hours to find this, I did as illustrated in this tutorial first, then saw your comments and reversed +5/GND pins and voila! At last nothing was burnt :P.

  • sicalíptico
    Have yοu eѵeг thought аbout adding a ⅼittle Ьit mofe than just youг articles?
    I mean, what you say іѕ fundamental аnd evеrything. Howevеr jᥙst imagine
    if you aԀded ѕome great pictures ߋr videos tо givе your
    posts more, “pop”! Yoour сontent іs excellent Ƅut wіth pics and videos, tҺіs site coսld certаinly bе one of the mⲟѕt beneficial in its niche.
    Wonderful blog!

  • Tina

    Pin diagram for PIR sensor is wrong.Ground and power pins are interchanged

  • Josh

    I tried this but it only keeps saying “Intruder Detected” when I run the py file. I already tried adjusting the sensor and still says “Intruder detected”. Is there anything I missed? Please help.

    Also, correct me if I’m wrong but isn’t there supposed to be parenthesis on the print part?

    like these:

    print (“No intruders”),i

    print (“Intruder detected”),i

    • Robert

      I get the same issue, did you find a fix?

  • sendo lừa đảo

    Have you ever considered about adding a little bit more than just your
    articles? I mean, what you say is valuable and all.

    But imagine if you added some great visuals or video clips
    to give your posts more, “pop”! Your content is excellent but with pics and video clips, this blog could
    certainly be one of the most beneficial in its niche. Excellent blog!

  • Howard

    gpiozero would also make this a lot easier

  • M Hammond

    No intruder unless unplug ground

  • Syama


  • Shawnerz98

    On my version of the same PIR sensor, +DC Voltage (3V or 5Vand Ground (Gnd) are backwards of what’s shown in the picture.
    Connecting the power backwards, to both 3 and 5 volts has not seemed to damage the sensor or the Pi.

    • Dan

      I noticed the same thing! The HC-SR501 Product Description shows Pin 1 as +5 and Pin 3 as GND, however, looking at the component side with the connector on top, GND is on the left side and +5 on the right. That’s what is show on the PD photo and the device works ok. The PIR lesson makes the same mistake, oddly.

      • Howard

        Yes, I agree.

        Incredibly frustrating – spent ages finding that out for myself – the first sensor I had was defective and with this mistake built into the equation, it was a double whammy.

        Thought the poster could have also given more instruction on calibration of the sensor, too

  • Tuan

    Great tutorial. I have some questions for you, please help me.
    1. Why input is slot 11 ?
    2. Can I change to another slot ?

    • Shawnerz98

      Tuan: The author used RPi.GPIO library. It appears that library addresses each IO Pin using the pin number and not the GPIO number. Pin 11 is GPIO #17.
      The answer to your question is yes, you can use another GPIO pin. You can use Pin 3, Pin 5, Pin 7, Pin 8, Pin 10….or any pin that is labeled as “GP”.

    • broskie

      If you set the mode to GPIO.BCM rather than GPIO.BOARD then you can reference the actual GPIO pin. In this tutorial the pin number would be the actual GPIO #17.

  • Ian

    Please bear with me as I am a noob. I have read that the R-pi GPIO pins cannot handle 5V signals, only 3V (or 3.3V?). If so, why isn’t a resistor or something necessary to drop the voltage down from motion sensors 5V data signal?

    • Gregory Pierce

      This is actually important. The GPIO on the RPI isn’t supposed to receive more than 3.3V. You should be splitting the voltage with resistors and not wiring the 5V high from the PIR directly to the GPIO on the RPI. You will certainly damage it long term.

    • Norm

      The Vcc input voltage for the PIR is 5v, but the output voltage is 3.3 volts according to the spec sheet.

  • eddy

    please , can I use this methode to count eggs ? does eggs generate infrared heat like body ?

  • alan

    en verdad agradezco tu tutorial esta muy bien explicado !!!

    no suelo dar las gracias pero este en verdad me gusto


  • benjotituus

    good one

  • Freddyny

    yes, me too haha

  • nehal

    i love raspberry pi

pingbacks / trackbacks

Leave a Comment

7 + 3 =

Start typing and press Enter to search