14 lines
No EOL
284 B
Python
14 lines
No EOL
284 B
Python
import time
|
|
import board
|
|
import adafruit_hcsr04
|
|
|
|
# Set the sensor pins
|
|
sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.TRIG, echo_pin=board.ECHO)
|
|
|
|
while True:
|
|
try:
|
|
print(f"{sonar.distance:.2f}")
|
|
|
|
except RuntimeError:
|
|
print("Retrying!")
|
|
time.sleep(0.1) |