July 22, 2018

IoT Project 5 : RaspberryPi3 and HC-SR501 Motion Sensor(Avinash Aeron)

I was really useful to have a motion sensor with my Home-Environment monitoring solution that I’m building (see earlier posts on an integration of DTH11, BMP085, GY-30 and MQ135 sensors). I got one ePro Labs PIR Sensor HC-SR501 Motion Sensor.

Required items:
– Raspberry PI 3 & SD Card with ubuntu-mate Operating System https://www.raspberrypi.org/products/raspberry-pi-3-model-b/
– HC-SR501 Motion Sensor pack
– 3 x pitch jumpers

Preparing Raspberry PI 3 & SHDC:
Create the setup as follows:
1. Download below two as described https://ubuntu-mate.org/raspberry-pi/
– 7-Zip to extract the image.
– Win32 Disk Imager to write the image.
2. Extracted ‘ubuntu-mate from zip and flashed SHCD using Win32 Disk Imager
3. Put the SHCD into slot and power-up the Raspberry PI 3.
4. After ubuntu-mate is up, execute following on the terminal:

CODE: SELECT ALL
sudo apt-get update
sudo apt-get upgrade

5. If you have not installed git, install it now:

CODE: SELECT ALL
sudo apt-get install git

6. A script used for this experiment:

CODE: SELECT ALL
"""
ePro Labs PIR Sensor HC-SR501 Motion Sensor. using Raspberry Pi
“””
# Import all the libraries we need to run
import sys
import RPi.GPIO as GPIO
import os
from time import sleep
import datetime

TRUE = 1
FALSE = 0

DEBUG = 1

# Setup the pins we are connect to
SR501_pin = 16

#Setup our API and delay
#myAPI = “***Insert Your API CODE HERE***”
myDelay = 2 #how many seconds between posting data

GPIO.setmode(GPIO.BCM)
GPIO.setup(SR501_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def getSensorData():
return (str(GPIO.input(SR501_pin)))

# main() function
def main():

print ‘starting…’

while True:
try:
PIRDETECT = getSensorData()

LT = datetime.datetime.strftime(datetime.datetime.now(), ‘%Y-%m-%d %H:%M:%S’)

if GPIO.input(SR501_pin):
print LT + ” : ” + PIRDETECT + ” MOTION DETECTED!!”
else:
print LT + ” : ” + PIRDETECT

sleep(int(myDelay))
except:
print ” ”
print “HC-SR501 :” + PIRDETECT
print ‘exiting.’
break

# call main
if __name__ == ‘__main__’:
main()

Connect as listed below:
VCC (HC-SR501) <-> pin04 (RPi-04 5.0V)
Ground (HC-SR501) <-> pin34 (RPi-34 Ground)
Out (HC-SR501) <-> pin36 (RPi-36 GPIO-16)

RasberryPi3 with sensors

Test Results:

Leave a Reply

Your email address will not be published. Required fields are marked *