Raspberry Pi GPIO with PIR motion sensor: Best tutorial

 
Raspberry pi GPIO pin tutorialThis 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.

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”.

Working of a PIR motion sensor DIY hacking

Working of a PIR motion sensor

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.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
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:

Raspberry pi GPIO pin tutorial

Raspberry pi GPIO LED connection diagram

You can then notice that the LED starts blinking after you execute the python program. Using this command: sudo python ledblink.py. The LED blinks since 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 the demo:

 

Step 2: Interfacing the PIR motion sensor to the raspberry pi- 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: pirtest.py:


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

Now, connect your raspberry pi GPIO to the PIR motion sensor as per the following connection diagram:

Raspberry pi GPIO pin tutorial

Raspberry Pi PIR motion sensor connection

You will notice that this code prints: “Intruder detected” when you place your hand over the sensor. And 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 counter clockwise 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 VNC server and a LAN cable just like how I did in the below video by using this tutorial.

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

 

Recommended Posts
Showing 22 comments
  • sendo lừa đảo
    Reply

    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
    Reply

    gpiozero would also make this a lot easier

  • M Hammond
    Reply

    No intruder unless unplug ground

  • Syama
    Reply

    HC-SR501 PYROELECTRIC INFRARED PIR MOTION SENSOR DETECTOR MODULE for ARDUINO AVR is this the same as RASPBERRY PI GPIO WITH PIR MOTION SENSOR?

  • Shawnerz98
    Reply

    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
      Reply

      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 Raspberrypi.org PIR lesson makes the same mistake, oddly.

      • Howard
        Reply

        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
    Reply

    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
      Reply

      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”.

  • Ian
    Reply

    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
      Reply

      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
      Reply

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

  • eddy
    Reply

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

  • alan
    Reply

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

    no suelo dar las gracias pero este en verdad me gusto

    TE FELICITO

  • benjotituus
    Reply

    good one

  • Freddyny
    Reply

    yes, me too haha

  • nehal
    Reply

    i love raspberry pi

pingbacks / trackbacks

Leave a Comment

82 − 79 =

Start typing and press Enter to search