Simple test

Ensure your device works with this simple test.

examples/tca9548a_simpletest.py
 1# SPDX-FileCopyrightText: 2021 Carter Nelson for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# This example shows using TCA9548A to perform a simple scan for connected devices
 5import board
 6
 7import adafruit_tca9548a
 8
 9# Create I2C bus as normal
10i2c = board.I2C()  # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
12
13# Create the TCA9548A object and give it the I2C bus
14tca = adafruit_tca9548a.TCA9548A(i2c)
15
16for channel in range(8):
17    if tca[channel].try_lock():
18        print(f"Channel {channel}:", end="")
19        addresses = tca[channel].scan()
20        print([hex(address) for address in addresses if address != 0x70])
21        tca[channel].unlock()

Multisensor test

Shows how to use the I2C Multiplexer with two sensors

examples/tca9548a_multisensor.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# This example shows using two TSL2491 light sensors attached to TCA9548A channels 0 and 1.
 5# Use with other I2C sensors would be similar.
 6import time
 7
 8import adafruit_tsl2591
 9import board
10
11import adafruit_tca9548a
12
13# Create I2C bus as normal
14i2c = board.I2C()  # uses board.SCL and board.SDA
15# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
16
17# Create the TCA9548A object and give it the I2C bus
18tca = adafruit_tca9548a.TCA9548A(i2c)
19
20# For each sensor, create it using the TCA9548A channel instead of the I2C object
21tsl1 = adafruit_tsl2591.TSL2591(tca[0])
22tsl2 = adafruit_tsl2591.TSL2591(tca[1])
23
24# After initial setup, can just use sensors as normal.
25while True:
26    print(tsl1.lux, tsl2.lux)
27    time.sleep(0.1)