I2C Scan Sanity Check
To verify the I2C addresses, an I2C scan can be used. See the guide linked below for more information on I2C scanning. The guide also has a CircuitPython I2C scan code example that can be used.
Running that I2C scan code with the two BME280's attached shows:
Checking board.I2C()...ADDED. Checking board.STEMMA_I2C()...ADDED. Checking busio.I2C(board.GP1, board.GP0)...SKIPPED: no such attribute ---------------------------------------- I2C SCAN ---------------------------------------- board.I2C() addresses found: ['0x37', '0x77'] board.STEMMA_I2C() addresses found: ['0x37', '0x77'] board.I2C() addresses found: ['0x37', '0x77'] board.STEMMA_I2C() addresses found: ['0x37', '0x77'] board.I2C() addresses found: ['0x37', '0x77'] board.STEMMA_I2C() addresses found: ['0x37', '0x77']
The BME280 found at address 0x77
is the one directly attached. The 0x37
address is the BME280 located downstream of the LTC4316. These are the two addresses we need for modifying the code. The 0x37
address can be thought of as an alternate address for the BME280. In that manner, the code usage is the same as the Two Devices using Alternate Address approach shown previously. The only difference is the actual address.
# SPDX-FileCopyrightText: 2024 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, 0x37) # address = 0x37 #-------------------------------------------------------------------- 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)
Here's the output:
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 = 1002.28 BME280 #2 Pressure = 1001.88 -------------------- BME280 #1 Pressure = 1002.28 BME280 #2 Pressure = 1001.91 -------------------- BME280 #1 Pressure = 1002.26 BME280 #2 Pressure = 1001.88 -------------------- BME280 #1 Pressure = 1002.26 BME280 #2 Pressure = 1001.88
These are the two important lines:
bme1 = adafruit_bme280.Adafruit_BME280_I2C(i2c, 0x77) # address = 0x77 bme2 = adafruit_bme280.Adafruit_BME280_I2C(i2c, 0x37) # address = 0x37
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. The 0x37
address was determined from the I2C scan.
After that, each can be used to read the sensor values:
pressure1 = bme1.pressure pressure2 = bme2.pressure
Text editor powered by tinymce.