Skip to content

Commit efbc25f

Browse files
committed
Add error check, update for Pi
1 parent 4364ce1 commit efbc25f

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

adafruit_us100.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@
3737
https://github.com/adafruit/circuitpython/releases
3838
"""
3939

40-
# imports
41-
4240
__version__ = "0.0.0-auto.0"
4341
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_US100.git"
4442

43+
4544
class US100:
4645
"""Control a US-100 ultrasonic range sensor."""
4746

@@ -63,6 +62,9 @@ def distance(self):
6362
"""
6463
self._uart.write(bytes([0x55]))
6564
data = self._uart.read(2) # 2 bytes return for distance
65+
if not data:
66+
raise RuntimeError("Sensor not found. Check your wiring!")
67+
6668
if len(data) != 2:
6769
raise RuntimeError("Did not receive distance response")
6870
dist = (data[1] + (data[0] << 8)) / 10
@@ -73,6 +75,9 @@ def temperature(self):
7375
"""Return the on-chip temperature, in Celsius"""
7476
self._uart.write(bytes([0x50]))
7577
data = self._uart.read(1) # 1 byte return for temp
78+
if not data:
79+
raise RuntimeError("Sensor not found. Check your wiring!")
80+
7681
if len(data) != 1:
7782
raise RuntimeError("Did not receive temperature response")
7883
temp = data[0] - 45

examples/us100_simpletest.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import time
2-
import board
3-
import busio
42
import adafruit_us100
53

4+
# For use with a microcontroller:
5+
import board
6+
import busio
67
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+
# uart = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1)
12+
813
us100 = adafruit_us100.US100(uart)
914

1015
while True:
1116
print("-----")
1217
print("Temperature: ", us100.temperature)
18+
time.sleep(0.5)
1319
print("Distance: ", us100.distance)
1420
time.sleep(0.5)

0 commit comments

Comments
 (0)