Let's do the same thing in CircuitPython.
Here's the code:
# SPDX-FileCopyrightText: 2022 Carter Nelson for Adafruit Industries # # SPDX-License-Identifier: MIT import time import board from adafruit_bme280 import basic as adafruit_bme280 # Get the board's default I2C port i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller #-------------------------------------------------------------------- # NOTE!!! This is the "special" part of the code # # Create each sensor instance # If left out, the default address is used. # But also OK to be explicit and specify address. bme1 = adafruit_bme280.Adafruit_BME280_I2C(i2c, 0x77) # address = 0x77 bme2 = adafruit_bme280.Adafruit_BME280_I2C(i2c, 0x76) # address = 0x76 #-------------------------------------------------------------------- print("Two BME280 Example") while True: # Access each sensor via its instance pressure1 = bme1.pressure pressure2 = bme2.pressure print("-"*20) print("BME280 #1 Pressure =", pressure1) print("BME280 #2 Pressure =", pressure2) 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: Two BME280 Example -------------------- BME280 #1 Pressure = 1013.98 BME280 #2 Pressure = 1013.16 -------------------- BME280 #1 Pressure = 1013.98 BME280 #2 Pressure = 1013.13 -------------------- BME280 #1 Pressure = 1014.0 BME280 #2 Pressure = 1013.16 -------------------- BME280 #1 Pressure = 1014.01 BME280 #2 Pressure = 1013.2
These are the two important lines:
bme1 = adafruit_bme280.Adafruit_BME280_I2C(i2c, 0x77) # address = 0x77 bme2 = adafruit_bme280.Adafruit_BME280_I2C(i2c, 0x76) # address = 0x76
They create a separate sensor instance for each BME280 and specify the I2C address for each. We intentionally specify the default 0x77 address just to be explicit.
After that, each can be used to read the sensor values:
pressure1 = bme1.pressure pressure2 = bme2.pressure
So things are pretty easy if alternate addresses can be used.
Page last edited January 21, 2025
Text editor powered by tinymce.