PIR Motion Sensor with the Raspberry PI

Image for PIR Motion Sensor with the Raspberry PI
Tinkerer: Lory Livezey

In this tutorial you will learn how to use a PIR Motion Sensor with the Raspberry Pi. The PIR sensor will allow you to build projects that will detect motion, then trigger some event such as turning on a light or an alarm.

Previous Step

  Calling A Python Script from Another Python Script

What you will need

Materials for this Project

A Look at the PIR Motion Sensor

PIR stands for Passive InfraRed. Our bodies generate infrared heat that the sensor is able to detect. When it triggers, it sends a 5V signal to the Raspberry Pi, that we will be reacting to in our Python program. Since we hooked up a traffic light, then learned how to call those scripts remotely in our previous lessons, we'll turn the red light on when we detect motion, using just a single line of code.

The board has a 3-pin connector that you can plug directly into a breadboard. We would do that in a more complex project, but to keep things simple to learn, we are going to just plug the motion sensor directly into the Raspberry Pi.

Connect the Wires

As you're looking at the bottom of the board with the pins toward you, plug the red (power) wire into the first pin, the white (signal) wire into the middle pin, and the black (ground) wire into the third pin. If you have different color wires, that's ok. The color makes no difference other than to help you tell them apart.

Connect the wires into the sensor

Next, connect the other end of the wires into the Raspberry Pi. The red (power) wire into the first pin in the first row, the black (ground) wire into the third pin of the first row, and the white (signal) wire into the sixth pin of the second row.

Connect the wires into the Raspberry Pi

Create Python Script

Boot up the Raspberry Pi, and use the following commands to create a directory to hold our new script:

cd /home/pi/tinker-pi/modules
mkdir motion-sensor
cd motion-sensor
sudo nano motion.py

Copy/paste the following code. Read the comments in the code to learn what's happening:

import RPi.GPIO as GPIO             #set up the GPIO library
import time                         #set up the 'time' module so we can pause


GPIO.setmode(GPIO.BCM)              #this sets the numbering scheme for GPIO pins that we are using

GPIO.setup(17, GPIO.IN)             #pin #17 is set to IN, meaning the signal 
                                    #is coming into the pi, not out

while True:                         #endless loop
       i=GPIO.input(17)             #see if pin #17 sent a signal (motion detected)
       if i==0:                     #do this if motion was detected
             print "I see nothing",i
             execfile("/home/pi/tinker-pi/modules/traffic-light/green-on.py") #turn green on
       elif i==1:                   #do this if it didn't
             print "Red Alert! Red Alert!",i
             execfile("/home/pi/tinker-pi/modules/traffic-light/red-on.py") #turn red on

Save the script:

Ctrl + x + y

Run it:

sudo python motion.py

The output on the screen should look something like this:

Connect the wires into the Raspberry Pi

As you put your hand in front of the sensor:

Connect the wires into the Raspberry Pi

In this tutorial, we wired up a PIR sensor and built on our last project to make the traffic light light up. Not by re-writing that code, but by calling it from our Python script.

Our tutorials build on one another so that you can learn and expand your skills by 'doing'. I hope that you'll continue on with more and more advanced projects, and have some fun in the process!

Be sure to subscribe to our channel!