-
Notifications
You must be signed in to change notification settings - Fork 0
/
HCSR04.py
45 lines (37 loc) · 1.28 KB
/
HCSR04.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
# Written and Debugged by: Shantanu Ghosh, Vishal Venkateswaran
import RPi.GPIO as GPIO
import time
while True:
try:
GPIO.setmode(GPIO.BOARD)
#assign gpio pin numbers to trigger&echo
PIN_TRIGGER = 7
PIN_ECHO = 11
#assign trigger&echo to proper gpio i/o status
GPIO.setup(PIN_TRIGGER, GPIO.OUT)
GPIO.setup(PIN_ECHO, GPIO.IN)
#set trigger to low
GPIO.output(PIN_TRIGGER, GPIO.LOW)
#prompt notification, sensor calibration
print "Waiting for sensor to settle"
time.sleep(2)
print "Calculating distance"
#set trigger to high
GPIO.output(PIN_TRIGGER, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(PIN_TRIGGER, GPIO.LOW)
#condition to set start/stop time based on echo
while GPIO.input(PIN_ECHO)==0:
pulse_start_time = time.time()
while GPIO.input(PIN_ECHO)==1:
pulse_end_time = time.time()
#calculate distance based on times. assume speed of sound to be 17150 cm/s. round distance to 2 decimal places
pulse_duration = pulse_end_time - pulse_start_time
distance = round(pulse_duration * 17150, 2)
#display distance
print "Distance:",distance,"cm"
#allow keyboard interrupt to stop program
except KeyboardInterrupt:
print "Measurement stopped by User"
GPIO.cleanup()
break