-
Notifications
You must be signed in to change notification settings - Fork 0
/
ultrasonic_test.py
61 lines (49 loc) · 1.88 KB
/
ultrasonic_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
import RPi.GPIO as GPIO
import time
# Set up GPIO mode
GPIO.setmode(GPIO.BCM)
# Define GPIO pins for Trigger and Echo
TRIG_PIN = 23 # Replace with your actual GPIO pin for TRIG
ECHO_PIN = 24 # Replace with your actual GPIO pin for ECHO
# Set up the GPIO pins
GPIO.setup(TRIG_PIN, GPIO.OUT)
GPIO.setup(ECHO_PIN, GPIO.IN)
def measure_distance():
# Ensure trigger is low
GPIO.output(TRIG_PIN, False)
time.sleep(2) # Allow the sensor to settle
# Send a 10 microsecond pulse to trigger the sensor
GPIO.output(TRIG_PIN, True)
time.sleep(0.00001) # 10 microseconds
GPIO.output(TRIG_PIN, False)
# Wait for the Echo pin to go HIGH (start the pulse) with a timeout
timeout_start = time.time()
while GPIO.input(ECHO_PIN) == 0:
pulse_start = time.time()
if time.time() - timeout_start > 1: # Timeout after 1 second
print("Timeout waiting for Echo to go HIGH")
return None
# Wait for the Echo pin to go LOW (end the pulse) with a timeout
timeout_start = time.time()
while GPIO.input(ECHO_PIN) == 1:
pulse_end = time.time()
if time.time() - timeout_start > 1: # Timeout after 1 second
print("Timeout waiting for Echo to go LOW")
return None
# Calculate the time difference between start and end
pulse_duration = pulse_end - pulse_start
# Calculate the distance (speed of sound is 34300 cm/s)
distance = pulse_duration * 17150 # Distance in centimeters
distance = round(distance, 2) # Round to two decimal places
return distance
try:
while True:
distance = measure_distance()
if distance is not None:
print(f"Distance: {distance} cm")
else:
print("Failed to measure distance")
time.sleep(1)
except KeyboardInterrupt:
print("Measurement stopped by user")
GPIO.cleanup() # Reset GPIO settings