Skip to content

Commit 32a0a91

Browse files
committed
Update failed read check.
1 parent 0420430 commit 32a0a91

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

adafruit_us100.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
__version__ = "0.0.0-auto.0"
4141
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_US100.git"
4242

43+
import time
44+
4345

4446
class US100:
4547
"""Control a US-100 ultrasonic range sensor."""
@@ -51,34 +53,52 @@ def __init__(self, uart):
5153
def distance(self):
5254
"""Return the distance measured by the sensor in cm.
5355
This is the function that will be called most often in user code.
54-
If no signal is received, we'll throw a RuntimeError exception. This means
55-
either the sensor was moving too fast to be pointing in the right
56+
If no signal is received, return ``None``. This can happen when the
57+
object in front of the sensor is too close, the wiring is incorrect or the
58+
sensor is not found. If the signal received is not 2 bytes, return ``None``.
59+
This means either the sensor was moving too fast to be pointing in the right
5660
direction to pick up the ultrasonic signal when it bounced back (less
5761
likely), or the object off of which the signal bounced is too far away
58-
for the sensor to handle. In my experience, the sensor can detect
62+
for the sensor to handle. In my experience, the sensor can not detect
5963
objects over 460 cm away.
6064
:return: Distance in centimeters.
6165
:rtype: float
6266
"""
63-
self._uart.write(bytes([0x55]))
64-
data = self._uart.read(2) # 2 bytes return for distance
65-
if not data:
66-
raise RuntimeError("Sensor not found. Check your wiring!")
67+
for _ in range(2): # Attempt to read twice.
68+
self._uart.write(bytes([0x55]))
69+
data = self._uart.read(2) # 2 byte return for distance.
70+
if data: # If there is a reading, exit the loop.
71+
break
72+
time.sleep(0.1) # You need to wait between readings, so delay is included.
73+
else:
74+
# Loop exited normally, so read failed twice.
75+
# This can happen when the object in front of the sensor is too close, if the wiring
76+
# is incorrect or the sensor is not found.
77+
return None
6778

6879
if len(data) != 2:
69-
raise RuntimeError("Did not receive distance response")
80+
return None
81+
7082
dist = (data[1] + (data[0] << 8)) / 10
7183
return dist
7284

7385
@property
7486
def temperature(self):
7587
"""Return the on-chip temperature, in Celsius"""
76-
self._uart.write(bytes([0x50]))
77-
data = self._uart.read(1) # 1 byte return for temp
78-
if not data:
79-
raise RuntimeError("Sensor not found. Check your wiring!")
88+
for _ in range(2): # Attempt to read twice.
89+
self._uart.write(bytes([0x50]))
90+
data = self._uart.read(1) # 1 byte return for temp
91+
if data: # If there is a reading, exit the loop.
92+
break
93+
time.sleep(0.1) # You need to wait between readings, so delay is included.
94+
else:
95+
# Loop exited normally, so read failed twice.
96+
# This can happen when the object in front of the sensor is too close, if the wiring
97+
# is incorrect or the sensor is not found.
98+
return None
8099

81100
if len(data) != 1:
82-
raise RuntimeError("Did not receive temperature response")
101+
return None
102+
83103
temp = data[0] - 45
84104
return temp

0 commit comments

Comments
 (0)