PIR Sensor with Raspberry PiSharath
The PIR Sensor is also called as Pyroelectric IR Sensor, Passive Infrared Sensor or IR Motion Sensor. The applications of PIR are Burglar Alarm, Motion detected Floor Lamps, Automatic Door Open System and more.
The PIR Works on a change in the difference of IR Levels on the two Layers of its Lens. Means, the sensor (Inside the round metal with a rectangular crystal in the center) is split into two half on the Lens Area, where signals are opposite to each other.
In default condition on the two layers, the difference in two signal will cancel each other and don’t trigger any output. When a human crosses, the variation of IR levels in PIR output’s a trigger. This trigger signal is then processed and output the signal by an IC on the PIR Module.
Note: The sensitivity and delay time of the sensor can be adjusted on the module by the two potentiometers on it.
Sensitivity: The sensitivity determines the Range of the Sensor. It ranges from 3 to 5 meters.
Delay Time: It determines how long the output of the PIR Sensor remains HIGH.
In this project, we will see how to use PIR Sensor with Raspberry Pi and will light up an LED (You can use buzzer alternatively).
We will write a python code to detect a human presence and turn ON an LED during the trigger time. So whenever a human presence is detected the PIR Sensor will output a HIGH Signal (+5v or +3.3v). According to the delay time we set on PIR Sensor the LED will turn ON during that period. When no human presence is detected the PIR Sensor output’s a LOW Signal.
import RPi.GPIO as GPIO import time GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(11, GPIO.IN) #PIR sensor as INPUT GPIO.setup(3, GPIO.OUT) #LED as OUTPUT while True: sens=GPIO.input(11) if sens==0: #When output from motion sensor is LOW print "No intruders",sens GPIO.output(3, 0) #Turn OFF LED time.sleep(0.1) elif sens==1: #When output from motion sensor is HIGH print "Intruder detected",sens GPIO.output(3, 1) #Turn ON LED time.sleep(0.1)
Steps to Run our Python Program:
On the desktop, right click and create a text document. Name it as ‘PIRtest.py’ and save.
Now open terminal, type ‘cd Desktop’ to navigate to the desktop.
Open the file in nano editor by using the command,
It will open the file, copy paste the code given above and save it by pressing Ctrl+X. And type y to save it.
Once everything is setup, run the file using the command
Wave your hands in front of PIR Sensor. You can able to see the light will Turn ON according to the time delay set on PIR. Similarly, the print command will display the result on the screen.