Here is the CircuitPython code for a TCA9548A and three BME280s:

# SPDX-FileCopyrightText: 2022 Carter Nelson for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import time
import board
import adafruit_tca9548a
from adafruit_bme280 import basic as adafruit_bme280

# Create I2C bus as normal
i2c = board.I2C()  # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller

# Create the TCA9548A object and give it the I2C bus
tca = adafruit_tca9548a.TCA9548A(i2c)

#--------------------------------------------------------------------
# NOTE!!! This is the "special" part of the code
#
# Create each BME280 using the TCA9548A channel instead of the I2C object
bme1 = adafruit_bme280.Adafruit_BME280_I2C(tca[0])   # TCA Channel 0
bme2 = adafruit_bme280.Adafruit_BME280_I2C(tca[1])   # TCA Channel 1
bme3 = adafruit_bme280.Adafruit_BME280_I2C(tca[2])   # TCA Channel 2
#--------------------------------------------------------------------

print("Three BME280 Example")

while True:
    # Access each sensor via its instance
    pressure1 = bme1.pressure
    pressure2 = bme2.pressure
    pressure3 = bme3.pressure

    print("-"*20)
    print("BME280 #1 Pressure =", pressure1)
    print("BME280 #2 Pressure =", pressure2)
    print("BME280 #3 Pressure =", pressure3)

    time.sleep(1)

With that code running on the CircuitPython board, the output will look like this:

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Three BME280 Example
--------------------
BME280 #1 Pressure = 1013.96
BME280 #2 Pressure = 1013.72
BME280 #3 Pressure = 1013.76
--------------------
BME280 #1 Pressure = 1013.97
BME280 #2 Pressure = 1013.73
BME280 #3 Pressure = 1013.77
--------------------
BME280 #1 Pressure = 1013.98
BME280 #2 Pressure = 1013.73
BME280 #3 Pressure = 1013.73

The TCA9548A itself is setup like any other I2C device:

tca = adafruit_tca9548a.TCA9548A(i2c)

The board's I2C bus (i2c) is passed in. The I2C address for the TCA9548A would also be specified here. But in this case we leave it out to show how to use with the default 0x70 address. Remember - that's the address of the TCA9548A.

The important difference to note is what is being passed in when creating each instance of the BME280 device:

bme1 = adafruit_bme280.Adafruit_BME280_I2C(tca[0])   # TCA Channel 0
bme2 = adafruit_bme280.Adafruit_BME280_I2C(tca[1])   # TCA Channel 1
bme3 = adafruit_bme280.Adafruit_BME280_I2C(tca[2])   # TCA Channel 2

Instead of passing in the I2C bus (i2c), the [] operator is used on the tca instance to access and specify that output channel. So tca[0] is channel 0 of the TCA9548A, etc.

This is the fancy thing that the Adafruit CircuitPython TCA9548A library does. Because of this feature, once the initial setup is done, the instances can be used in a normal way:

pressure1 = bme1.pressure
pressure2 = bme2.pressure
pressure3 = bme3.pressure

There's no need to worry about dealing with the TCA9548A directly, changing its output channel, etc.

Exactly how the library does this is beyond the scope of this guide. But it relies on some Python specific tricks, which is why a similar capability is lacking in Arduino land.

This guide was first published on May 04, 2022. It was last updated on May 04, 2022.

This page (CircuitPython) was last updated on May 31, 2023.

Text editor powered by tinymce.