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

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> ...

How does a handpump work ?

The most common tool to access a life source like water — this innovation boasts of none of the accolades that modern machines enjoy. Yet the simplicity and efficiency of design drives a sea of devices that permeate our lives at home and in industries. The unsung hero that India should be particularly proud of is called India Mark II. A human-powered pump designed to lift water from a depth of 50 m or less, it is the world’s most widely used water hand pump. It was designed in 1970 through the joint efforts of the government of India, UNICEF and WHO. Its purpose was to address the deathly problem of paucity of water and draught in rural areas of developing nations. By the mid 1990s, five million of the pumps had been manufactured and installed around the world. Hand Pump Parts: Handle Pump rod water outlet Piston Piston valve Foot valve Rising main Suction lift What does it do? Simply defined, hand pumps are manually operated pumps that use human power and ...

How To Change The Windows Logon Screen Message And Font ?

By:Prayag nao Recently I was playing with windows 7 customization setting , I feel that some of graphics of windows is no directly editable. If we want to customize them, How to do that ? In Windows 7, these options (Logon text and background) have been restricted by Microsoft, leaving many users with a problem. If you’re responsible for applying company-specific branding to a network of computers or if you’re simply an enthusiast who enjoys tweaking the OS to suit a whim, you’ll have noticed that the Windows 7 logon message, the font and the color of the text are apparently impossible to change. Or are they? Well, no, they’re not. As a matter of fact, making these changes is relatively easy. Customize Logon Text Download Resource Hacker software. DOWNLOAD HERE       While this is downloading, open C:\Windows\System32\en-US\ (note that non-English versions of Windows will have a different folder name, such as \es-ES\ for Spanish) and f...