Datasheet
i2c.scan()] .
Open the serial console to see the results! The code prints out an array of addresses. We've connected the TSL2561
which has a 7-bit I2C address of 0x39. The result for this sensor is I2C addresses found: ['0x39'] . If no addresses are
returned, refer back to the wiring diagrams to make sure you've wired up your sensor correctly.
I2C Sensor Data
Now we know for certain that our sensor is connected and ready to go. Let's find out how to get the data from our
sensor!
Copy and paste the code into code.py using your favorite editor, and save the file.
This code begins the same way as the scan code. We've included the scan code so you have verification that your
sensor is wired up correctly and is detected. It prints the address once. After the scan, we unlock I2C with i2c_unlock()
so we can use the sensor for data.
We create our sensor object using the sensor library. We call it tsl2561 and provide it the i2c object.
Then we have a simple loop that prints out the lux reading using the sensor object we created. We add a
time.sleep(1.0) , so it only prints once per second. Connect to the serial console to see the results. Try shining a light on
it to see the results change!
# CircuitPython Demo - I2C sensor
import time
import adafruit_tsl2561
import board
import busio
i2c = busio.I2C(board.SCL, board.SDA)
# Lock the I2C device before we try to scan
while not i2c.try_lock():
pass
# 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.
i2c.unlock()
# Create library object on our I2C port
tsl2561 = adafruit_tsl2561.TSL2561(i2c)
# Use the object to print the sensor readings
while True:
print("Lux:", tsl2561.lux)
time.sleep(1.0)
© Adafruit Industries
https://learn.adafruit.com/adafruit-feather-m0-express-designed-for-circuit-python-
circuitpython
Page 157 of 199