Updated I2C code, added I2C test script

This commit is contained in:
Kattni 2018-03-21 22:17:55 -04:00
parent d240ca570a
commit 5bff545872
2 changed files with 48 additions and 10 deletions

View file

@ -1,25 +1,25 @@
# I2C sensor demo
# CircuitPython Demo - I2C sensor
import board
import busio
import adafruit_si7021
import adafruit_tsl2561
import time
i2c = busio.I2C(board.SCL, board.SDA)
# lock the I2C device before we try to scan
# Lock the I2C device before we try to scan
while not i2c.try_lock():
pass
print("I2C addresses found:", [hex(i) for i in i2c.scan()])
# Print the addresses found once
print("I2C addresses found:", [hex(device_address) for device_address in i2c.scan()])
# unlock I2C now that we're done scanning.
# Unlock I2C now that we're done scanning.
i2c.unlock()
# Create library object on our I2C port
si7021 = adafruit_si7021.SI7021(i2c)
tsl2561 = adafruit_tsl2561.TSL2561(i2c)
# Use library to read the data!
# Use the object to print the sensor readings
while True:
print("Temp: %0.2F *C Humidity: %0.1F %%" % (si7021.temperature, si7021.relative_humidity))
time.sleep(1)
print("Lux:", tsl2561.lux)
time.sleep(1.0)

View file

@ -0,0 +1,38 @@
import board
import busio
def is_hardware_I2C(scl, sda):
try:
p = busio.I2C(scl, sda)
p.deinit()
return True
except ValueError:
return False
def get_unique_pins():
pin_names = dir(board)
if "NEOPIXEL" in pin_names:
pin_names.remove("NEOPIXEL")
if "APA102_MOSI" in pin_names:
pin_names.remove("APA102_MOSI")
if "APA102_SCK" in pin_names:
pin_names.remove("APA102_SCK")
pins = [getattr(board, p) for p in pin_names]
unique = []
for p in pins:
if p not in unique:
unique.append(p)
return unique
for scl_pin in get_unique_pins():
for sda_pin in get_unique_pins():
if scl_pin is sda_pin:
continue
else:
if is_hardware_I2C(scl_pin, sda_pin):
print("SCL pin:", scl_pin, "\t SDA pin:", sda_pin)
else:
pass