Skip to content

Instantly share code, notes, and snippets.

@anecdata
Last active September 1, 2020 22:11
Show Gist options
  • Save anecdata/21899a92e58fa46a432afa8eb9112c78 to your computer and use it in GitHub Desktop.
Save anecdata/21899a92e58fa46a432afa8eb9112c78 to your computer and use it in GitHub Desktop.
SHTC3 I2C Test
import time
import board
import adafruit_lps35hw
import adafruit_dps310
import adafruit_shtc3
"""
run, then ^C^D or re-save file to CIRCUITPY,
and line 42 (`sht =`) will fail:
File "adafruit_shtc3.py", line 93, in __init__
File "adafruit_shtc3.py", line 124, in reset
File "adafruit_shtc3.py", line 120, in reset
File "adafruit_shtc3.py", line 103, in _write_command
File "adafruit_shtc3.py", line 103, in _write_command
File "adafruit_bus_device/i2c_device.py", line 104, in write
OSError: [Errno 5] Input/output error
unrecoverable w/o power cycle to the SHTC3
but can also recover by re-saving this code with SHTC3 code commented out
"""
try:
i2c = board.I2C()
except RuntimeError as e:
print("I2C error:", e)
try:
lps = adafruit_lps35hw.LPS35HW(i2c)
except ValueError as e:
print("LPS35HW not found:", e)
try:
dps310 = adafruit_dps310.DPS310(i2c)
except ValueError as e:
print("DPS310 not found:", e)
try:
sht = adafruit_shtc3.SHTC3(i2c)
except ValueError as e:
print("SHTC3 not found:", e)
"""
except OSError as e:
print("SHTC3 OSError:", e)
# i2c.deinit()
"""
def i2c_scan():
try:
while not i2c.try_lock():
pass
device_addresses = i2c.scan()
i2c.unlock()
print("I2C addresses:", end=" ")
for device_address in device_addresses:
print(hex(device_address), end=' ')
print("\n")
except (RuntimeError, NameError) as e:
print("I2C Error:", e)
i2c_scan()
while True:
try:
print("LPS35HW", end="")
fahr = lps.temperature * 9/5 + 32
print("\t%.0f °F" % fahr, end="")
print("\t\t\t%.1f hPa" % lps.pressure, end="")
print()
except NameError as e:
print(e)
try:
print("DPS310", end="")
fahr = dps310.temperature * 9/5 + 32
print("\t%.1f°F" % fahr, end="") # ±0.5°C accuracy
print("\t\t\t%.1f hPa" % dps310.pressure, end="")
print()
except NameError as e:
print(e)
try:
print("SHT3C", end="")
temperature, relative_humidity = sht.measurements
fahr = temperature * 9/5 + 32
print("\t%0.1f°F" % fahr, end="")
print("\t\t%0.0f%%" % relative_humidity, end="")
print()
except NameError as e:
print(e)
print()
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment