PIR Motion with Raspberry Pi

Introduction: Detecting Motion Using Raspberry Pi

In this instructable,we are going to learn how we can use PIR ( Passive InfraRed ) Sensor with Raspberry Pi, in order to build a simple motion detector.It is used to sense movement of people, animals, or other objects. They are commonly used in burglar alarms and automatically-activated lighting systems.
Operating principles:
All objects with a temperature above absolute zero emit heat energy in the form of radiation. Usually this radiation isn't visible to the human eye because it radiates at infrared wavelengths, but it can be detected by electronic devices designed for such a purpose.(Source : Wikipedia )
Purpose of instructable :
The main idea of this tutorial is to Turn ON Led if a motion detected, and Turn OFF Led if else.As I said in the introduction you can use the sensor to control Light Room or Alarm instead of Led.

Step 1: Supplies

Hardware Supplies :
1. Raspberry Pi 3 Model B
2. PIR Sensor
3. Breadboard
4. 220 Ohms Resistor
5. LED
6. Wires
Software Supplies :
1. Raspbian Jessie (Operating System of Raspberry Pi : for more details you can take look to my previous Tutorial here).
2. Python IDLE
So I assume that you have successfully done some basic projects. If not, don’t worry I advice you to follow my previous tutorial(Start Your First Project With Raspberry : Blinking LED)

Step 2: Circuit Assembly

The wiring is quite simple, the PIR sensor has three pins:
1. Vcc to the 5v of Raspberry's GPIO.
2. GND to the GNS of Raspberry's GPIO.
3. OUT to 17 GPIO pin.
To wiring the LED and resistor you can follow the steps bellow :
1. Connect a 220Ω resistor to the anode of the LED,then the resistor to 5 V.
2. Connect the cathode of the LED to 4 GPIO pin (See the picture above).

Step 3: Python Code

1. Turn on your Pi and Create a new text file “pir.py” (You could named the file as you like).
2. Type in the following code:
import RPi.GPIO as GPIO
<p>import time<br>GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN) #PIR 
GPIO.setup(4, GPIO.OUT) #Led
try:     time.sleep(2) # to stabilize sensor     
while True:        
          i=GPIO.input(17)       
                    if i==0:               #When output from motion sensor is LOW                            
          GPIO.output(4, 0)  #Turn OFF LED              
            print (" No motion detected",i )                                                 
                    elif i==1:             #When output from motion sensor is HIGH                                                       
            GPIO.output(4, 1)  #Turn ON LED                            
             print (" Motion detected ",i )                             
except:     
            GPIO.cleanup()
3. Once you have typed all the code checked save it.
4. Run the python code by typing the following code in the terminal:
cd Desktop and press Enter( I type Desktop because I have saved the file in the pi's Desktop).
python pir.py and pressEnter.

Comments