-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultrasoon.py
35 lines (26 loc) · 839 Bytes
/
ultrasoon.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
import RPi.GPIO as GPIO
import time
try:
GPIO.setmode(GPIO.BCM)
PIN_TRIGGER = 27
PIN_ECHO = 17
GPIO.setup(PIN_TRIGGER, GPIO.OUT)
GPIO.setup(PIN_ECHO, GPIO.IN)
GPIO.output(PIN_TRIGGER, GPIO.LOW)
print("Setting up Sensor")
while(True):
GPIO.output(PIN_TRIGGER, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(PIN_TRIGGER, GPIO.LOW)
pulse_start_time = 0
pulse_end_time = 0
while GPIO.input(PIN_ECHO)==0:
pulse_start_time = time.time()
while GPIO.input(PIN_ECHO)==1:
pulse_end_time = time.time()
pulse_duration = pulse_end_time - pulse_start_time
distance = round(pulse_duration * 17150, 2)
print("Distance: ",distance,"cm")
time.sleep(0.1)
finally:
GPIO.cleanup()