Skip to content

Add error check, update for Pi #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions adafruit_us100.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@
https://github.com/adafruit/circuitpython/releases
"""

# imports

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_US100.git"


class US100:
"""Control a US-100 ultrasonic range sensor."""

Expand All @@ -63,6 +62,9 @@ def distance(self):
"""
self._uart.write(bytes([0x55]))
data = self._uart.read(2) # 2 bytes return for distance
if not data:
raise RuntimeError("Sensor not found. Check your wiring!")

if len(data) != 2:
raise RuntimeError("Did not receive distance response")
dist = (data[1] + (data[0] << 8)) / 10
Expand All @@ -73,6 +75,9 @@ def temperature(self):
"""Return the on-chip temperature, in Celsius"""
self._uart.write(bytes([0x50]))
data = self._uart.read(1) # 1 byte return for temp
if not data:
raise RuntimeError("Sensor not found. Check your wiring!")

if len(data) != 1:
raise RuntimeError("Did not receive temperature response")
temp = data[0] - 45
Expand Down
11 changes: 9 additions & 2 deletions examples/us100_simpletest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import time

# For use with a microcontroller:
import board
import busio
import adafruit_us100

uart = busio.UART(board.TX, board.RX, baudrate=9600)
# Create a US-100 module instance.

# For use with Raspberry Pi/Linux:
# import serial
# import adafruit_us100
# uart = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1)

us100 = adafruit_us100.US100(uart)

while True:
print("-----")
print("Temperature: ", us100.temperature)
time.sleep(0.5)
print("Distance: ", us100.distance)
time.sleep(0.5)