-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBody_Detect.py
More file actions
44 lines (29 loc) · 988 Bytes
/
Copy pathBody_Detect.py
File metadata and controls
44 lines (29 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#Detect the presence of a human getting on our bicycle
# set the Sensitivity to minimum (3 meters) and the Time delay to minimum (3 seconds)
# Way Zheng: HC-SR501 PIR motion sensor
import RPi.GPIO as GPIO
import time
"""
to call thie function, use the following code:
from pir_sensor import read_pir_sensor
pir_sensor_pin = 11
motion_detected = read_pir_sensor(pir_sensor_pin)
if motion_detected:
print("Motion detected!")
else:
print("No motion detected.")
"""
def read_pir_sensor(pir_sensor_pin):
GPIO.setmode(GPIO.BOARD)
# Set up PIR sensor input pin
GPIO.setup(pir_sensor_pin, GPIO.IN)
print("PIR sensor warming up...")
time.sleep(10) # allow sensor to warm up
print("PIR sensor ready")
try:
while True:
if GPIO.input(pir_sensor_pin):
return True # motion detected!
time.sleep(0.5) # wait for half a second before checking again
except KeyboardInterrupt:
GPIO.cleanup()