-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUSS.py
More file actions
61 lines (50 loc) · 1.89 KB
/
USS.py
File metadata and controls
61 lines (50 loc) · 1.89 KB
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
from machine import Pin
import utime
# Initialize trigger and echo pins
trigger = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
def get_distance():
"""
Measures the distance to an object using an ultrasonic sensor.
Returns:
float: Distance to the object in cm, or None if no echo is detected.
"""
try:
# Ensure trigger pin is low before starting
trigger.low()
utime.sleep_us(2) # Short delay
# Send the trigger pulse (5 microseconds)
trigger.high()
utime.sleep_us(5) # Trigger pulse duration
trigger.low()
# Wait for echo to go HIGH (signal sent)
timeout_start = utime.ticks_us()
while echo.value() == 0:
if utime.ticks_diff(utime.ticks_us(), timeout_start) > 30000: # Timeout after 30ms
print("Timeout: No echo received.")
return None
signaloff = utime.ticks_us()
# Wait for echo to go LOW (signal returned)
timeout_start = utime.ticks_us()
while echo.value() == 1:
if utime.ticks_diff(utime.ticks_us(), timeout_start) > 30000: # Timeout after 30ms
print("Timeout: Echo signal stuck HIGH.")
return None
signalon = utime.ticks_us()
# Calculate the time the signal took to travel
timepassed = signalon - signaloff
# Calculate the distance (in cm)
distance = (timepassed * 0.0343) / 2 # Speed of sound is 0.0343 cm per microsecond
return distance
except Exception as e:
print(f"Error: {e}")
return None
# Example of how to call the function
#while True:
# print("Measuring distance...")
# distance = get_distance()
# if distance is not None:
# print(f"Distance to the object: {distance:.2f} cm")
# else:
# print("Failed to measure distance.")
##time.sleep(1) # Wait before the next measurement