Skip to content

Commit b10c0e8

Browse files
committed
workin' code & example
1 parent 267b409 commit b10c0e8

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

adafruit_us100.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,39 @@
4141

4242
__version__ = "0.0.0-auto.0"
4343
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_US100.git"
44+
45+
class US100:
46+
"""Control a US-100 ultrasonic range sensor."""
47+
48+
def __init__(self, uart):
49+
self._uart = uart
50+
51+
@property
52+
def distance(self):
53+
"""Return the distance measured by the sensor in cm.
54+
This is the function that will be called most often in user code.
55+
If no signal is received, we'll throw a RuntimeError exception. This means
56+
either the sensor was moving too fast to be pointing in the right
57+
direction to pick up the ultrasonic signal when it bounced back (less
58+
likely), or the object off of which the signal bounced is too far away
59+
for the sensor to handle. In my experience, the sensor can detect
60+
objects over 460 cm away.
61+
:return: Distance in centimeters.
62+
:rtype: float
63+
"""
64+
self._uart.write(bytes([0x55]))
65+
data = self._uart.read(2) # 2 bytes return for distance
66+
if len(data) != 2:
67+
raise RuntimeError("Did not receive distance response")
68+
dist = (data[1] + (data[0] << 8)) / 10
69+
return dist
70+
71+
@property
72+
def temperature(self):
73+
"""Return the on-chip temperature, in Celsius"""
74+
self._uart.write(bytes([0x50]))
75+
data = self._uart.read(1) # 1 byte return for temp
76+
if len(data) != 1:
77+
raise RuntimeError("Did not receive temperature response")
78+
temp = data[0] - 45
79+
return temp

examples/us100_simpletest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import time
2+
import board
3+
import busio
4+
import adafruit_us100
5+
6+
uart = busio.UART(board.TX, board.RX, baudrate=9600)
7+
# Create a US-100 module instance.
8+
us100 = adafruit_us100.US100(uart)
9+
10+
while True:
11+
print("-----")
12+
print("Temperature: ", us100.temperature)
13+
print("Distance: ", us100.distance)
14+
time.sleep(0.5)

0 commit comments

Comments
 (0)