-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi_test.py
76 lines (58 loc) · 1.89 KB
/
pi_test.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#burgular detection in pi
#burgular detection in pi
import RPi.GPIO as GPIO
import time
# Set up GPIO mode
GPIO.setmode(GPIO.BCM)
# PIR Sensor
PIR_PIN = 5
GPIO.setup(PIR_PIN, GPIO.IN)
# Ultrasonic Sensor
TRIG = 2
ECHO = 3
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
# Buzzer and LED
BUZZER_PIN = 1
LED_PIN = 4
GPIO.setup(BUZZER_PIN, GPIO.OUT)
GPIO.setup(LED_PIN, GPIO.OUT)
def get_distance():
# Send a 10us pulse to TRIG
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
# Measure the ECHO response pulse
while GPIO.input(ECHO) == 0:
pulse_start = time.time()
while GPIO.input(ECHO) == 1:
pulse_end = time.time()
print("echo signal 1")
pulse_duration = pulse_end - pulse_start
print("Pulse duration is:",pulse_duration)
# Calculate distance (34300 cm/s is the speed of sound)
distance = pulse_duration * 17150
distance = round(distance, 2)
return distance
try:
print("Burglar detection system activated...")
while True:
get_distance()
# Check motion detection from PIR sensor
if GPIO.input(PIR_PIN):
print("Motion detected!")
GPIO.output(BUZZER_PIN, GPIO.HIGH)
GPIO.output(LED_PIN, GPIO.HIGH)
# Check distance from the ultrasonic sensor
distance = get_distance()
print(f"Distance to object: {distance} cm")
if distance < 40: # Trigger based on distance
print("Object is too close!")
time.sleep(2) # Alert for 2 seconds
GPIO.output(BUZZER_PIN, GPIO.LOW)
GPIO.output(LED_PIN, GPIO.LOW)
time.sleep(1)
except KeyboardInterrupt:
print("System deactivated")
finally:
GPIO.cleanup()