Skip to main content

Interfacing HC-SR501 PIR Motion Sensor with Raspberry Pi

By:Prayag Nao

All living beings radiate energy to the surroundings in the form of infrared radiations which are invisible to human eyes. A PIR (Passive infrared) sensor can be used to detect these passive radiations. When an object (human or animal) emitting infrared radiations passes through the field of view of the sensor, it detects the change in temperature and therefore can be used to detect motion.
HC-SR501 uses differential detection with two pyroelectric infrared sensors. By taking difference of the values, the average temperature from the field of view of sensor is removed and thereby reducing false positives.
Interfacing HC-SR501 with Raspberry Pi is easy because the output of sensor is Pi friendly ie. 3.3V and it can be powered from the 5V rail of  Pi.

The PIR sensor consist of 3 pins:

  • Vcc – 4.5V to 20V, Input power
  • OUTPUT – TTL output of sensor 0V, 3.3V
  • GND  Ground

Working of HC-SR501 PIR sensor

 

Pir motion sensor working


The module has a rectangular window with two sub probes 1 and 2 located at two ends of the rectangle. When a body emitting infrared radiation moves from side to side, the time for each probe for detection varies. Larger the time difference, more sensitive the device. It also uses a Fresnel lens to improve sensing aperture and filter in infrared waves.

Adjustment

  • For adjusting the detection delay (0.3 seconds to 600 seconds): Turn the potentiometer clockwise to increase and anticlockwise to decrease
  • For adjusting the sensing distance (3 meters to 7 meters): Turn the potentiometer clockwise to increase and anticlockwise to decrease
 

Circuit Connections

Vcc, Output, Ground are connected to 2 (5V), 26 (GPIO) and 6 (GND) pins of Pi respectively.

Python Program

Open text editor of raspberry pi and copy and paste following program into it.

import RPi.GPIO as GPIO                           #Import GPIO library
import time                                       #Import time library
GPIO.setmode(GPIO.BOARD)                          #Set GPIO pin numbering
pir = 26                                          #Associate pin 26 to pir
GPIO.setup(pir, GPIO.IN)                          #Set pin as GPIO in 
print "Waiting for sensor to settle"
time.sleep(2)                                     #Waiting 2 seconds for the sensor to initiate
print "Detecting motion"
while True:
   if GPIO.input(pir):                            #Check whether pir is HIGH
      print "Motion Detected!"
      time.sleep(2)                               #D1- Delay to avoid multiple detection
   time.sleep(0.1)                                #While loop delay should be less than detection(hardware) delay

save program as PIR.py.

Run the above program.

Note: The program can be calibrated for smaller detection (hardware) delay by using smaller Program delay(D1).

Output
you will see "Motion Detected" printing on screen when any motion detected by sensor.

Any doubts or suggestions? Comment below. 

Comments

Popular posts from this blog

Shows full image when you hover over a thumbnail

By:Prayag nao Thumbnail Zoom Plus   It's a firefox add-ons.Shows full image when you hover over a thumbnail. Works with Amazon, Baidu Images, Bing Images, Facebook, Flickr, Google+, Google Images, IMDb, LinkedIn, Netflix, Pinterest, Reddit, Twitter, Yandex, YouTube, Wikipedia, Yahoo Images, & many more.     About this Add-on Thumbnail Zoom Plus is a Firefox plug-in which shows a full-size image popup when you hover over a thumbnail or image link. When you hover your mouse over a thumbnail or a link to an image or YouTube video, the add-on displays the full-size image or video still-frame in a floating window. The image remains visible until you move the mouse outside the thumb, click the mouse, or press Escape. It’s quick and easy to move the mouse from one thumbnail to another to see the corresponding full-size images. For details see  User Manual . Supported sites include Amazon , Baidu , Bing , Facebook , Flickr , Google , Huffington

Here's How This Supercool Hoverboard Works

By:Prayag nao   If you've ever dreamed of cruising around town on a floating skateboard like Marty McFly does in the classic '80s flick "Back to the Future Part II," then you could soon be in luck. A pair of innovators is trying to make the futuristic fantasy of riding a hoverboard into a reality. About two months ago, husband and wife design team Jill and Greg Henderson launched a Kickstarter campaignfor their Hendo Hoverboard, a levitating skateboard that could hit "hoverparks" as early as October 2015. The Kickstarter campaign , which ends Sunday (Dec. 14), has been a resounding success, bringing in well over its initial goal of $250,000 in its first week. With only a couple of days to go in the impressive crowdfunding campaign, the project has already raised nearly $500,000. But with all the hype comes an important question: How in the world does this thing work? The basic premise behind the technology is something called

Real life Jarvis-Talk With Your Computer like Jarvis in Iron Man ....!

By:Prayag nao                                            Code to Make your Computer like Jarvis New Speech macro..>> Choose Advanced and change the code like this.. <speechMacros>   <command>     <listenFor></listenFor>   </command> </speechMacros> You have to add a commands  <listenFor>........</listenFor> - computer listens the words you specify here and respond accordingly. <speak>............</speak> - computer speaks what is written in this field according to the command which it got. Similarly, You can Edit more commands in the same way.   <speechMacros> <command> <listenFor>What's going on dude</listenFor> <speak>Nothing special tony</speak> </command> </speechMacros> This is just a basic command,If you want more advanced commands.you have to use Java Scripts and VB scripts . Tell me Time : This is d