File tree Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Original file line number Diff line number Diff line change 37
37
https://github.com/adafruit/circuitpython/releases
38
38
"""
39
39
40
- # imports
41
-
42
40
__version__ = "0.0.0-auto.0"
43
41
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_US100.git"
44
42
43
+
45
44
class US100 :
46
45
"""Control a US-100 ultrasonic range sensor."""
47
46
@@ -63,6 +62,9 @@ def distance(self):
63
62
"""
64
63
self ._uart .write (bytes ([0x55 ]))
65
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
+
66
68
if len (data ) != 2 :
67
69
raise RuntimeError ("Did not receive distance response" )
68
70
dist = (data [1 ] + (data [0 ] << 8 )) / 10
@@ -73,6 +75,9 @@ def temperature(self):
73
75
"""Return the on-chip temperature, in Celsius"""
74
76
self ._uart .write (bytes ([0x50 ]))
75
77
data = self ._uart .read (1 ) # 1 byte return for temp
78
+ if not data :
79
+ raise RuntimeError ("Sensor not found. Check your wiring!" )
80
+
76
81
if len (data ) != 1 :
77
82
raise RuntimeError ("Did not receive temperature response" )
78
83
temp = data [0 ] - 45
Original file line number Diff line number Diff line change 1
1
import time
2
+
3
+ # For use with a microcontroller:
2
4
import board
3
5
import busio
4
6
import adafruit_us100
5
-
6
7
uart = busio .UART (board .TX , board .RX , baudrate = 9600 )
7
- # Create a US-100 module instance.
8
+
9
+ # For use with Raspberry Pi/Linux:
10
+ # import serial
11
+ # import adafruit_us100
12
+ # uart = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1)
13
+
8
14
us100 = adafruit_us100 .US100 (uart )
9
15
10
16
while True :
11
17
print ("-----" )
12
18
print ("Temperature: " , us100 .temperature )
19
+ time .sleep (0.5 )
13
20
print ("Distance: " , us100 .distance )
14
21
time .sleep (0.5 )
You can’t perform that action at this time.
0 commit comments