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?
- Raspberry Pi B/B+ or 2 and basic peripherals: SD card, keyboard, mouse, etc.
- An LED and 220Ohm resistor.
- PIR motion sensor.
- 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”.
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:
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:
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.
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: