40
40
__version__ = "0.0.0-auto.0"
41
41
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_US100.git"
42
42
43
+ import time
44
+
43
45
44
46
class US100 :
45
47
"""Control a US-100 ultrasonic range sensor."""
@@ -51,34 +53,54 @@ def __init__(self, uart):
51
53
def distance (self ):
52
54
"""Return the distance measured by the sensor in cm.
53
55
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
56
60
direction to pick up the ultrasonic signal when it bounced back (less
57
61
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
59
63
objects over 460 cm away.
60
64
:return: Distance in centimeters.
61
65
:rtype: float
62
66
"""
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
+ time .sleep (0.1 )
70
+ data = self ._uart .read (2 ) # 2 byte return for distance.
71
+ if data : # If there is a reading, exit the loop.
72
+ break
73
+ time .sleep (0.1 ) # You need to wait between readings, so delay is included.
74
+ else :
75
+ # Loop exited normally, so read failed twice.
76
+ # This can happen when the object in front of the sensor is too close, if the wiring
77
+ # is incorrect or the sensor is not found.
78
+ return None
67
79
68
80
if len (data ) != 2 :
69
- raise RuntimeError ("Did not receive distance response" )
81
+ return None
82
+
70
83
dist = (data [1 ] + (data [0 ] << 8 )) / 10
71
84
return dist
72
85
73
86
@property
74
87
def temperature (self ):
75
88
"""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!" )
89
+ for _ in range (2 ): # Attempt to read twice.
90
+ self ._uart .write (bytes ([0x50 ]))
91
+ time .sleep (0.1 )
92
+ data = self ._uart .read (1 ) # 1 byte return for temp
93
+ if data : # If there is a reading, exit the loop.
94
+ break
95
+ time .sleep (0.1 ) # You need to wait between readings, so delay is included.
96
+ else :
97
+ # Loop exited normally, so read failed twice.
98
+ # This can happen when the object in front of the sensor is too close, if the wiring
99
+ # is incorrect or the sensor is not found.
100
+ return None
80
101
81
102
if len (data ) != 1 :
82
- raise RuntimeError ("Did not receive temperature response" )
103
+ return None
104
+
83
105
temp = data [0 ] - 45
84
106
return temp
0 commit comments