OK, now to code up the example with a TCA9548A multiplexer and four BME280s, all with I2C addresses of 0x77.

Here's the code:

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

#include <Adafruit_BME280.h>

#define TCAADDR 0x70

// For each device, create a separate instance.
Adafruit_BME280 bme1;  // BME280 #1
Adafruit_BME280 bme2;  // BME280 #2
Adafruit_BME280 bme3;  // BME280 #3
Adafruit_BME280 bme4;  // BME280 #4

// Helper function for changing TCA output channel
void tcaselect(uint8_t channel) {
  if (channel > 7) return;
  Wire.write(1 << channel);

void setup() {
  Serial.println(F("Four BME280 Example"));

  // Must call this once manually before first call to tcaselect()
  // Before using any BME280, call tcaselect to select its output channel
  tcaselect(0);      // TCA channel for bme1
  bme1.begin();      // use the default address of 0x77

  tcaselect(1);      // TCA channel for bme2
  bme2.begin();      // use the default address of 0x77

  tcaselect(2);      // TCA channel for bme3
  bme3.begin();      // use the default address of 0x77

  tcaselect(3);      // TCA channel for bme4
  bme4.begin();      // use the default address of 0x77

void loop() {
  float pressure1, pressure2, pressure3, pressure4;

  // Read each device separately
  pressure1 = bme1.readPressure();
  pressure2 = bme2.readPressure();
  pressure3 = bme3.readPressure();
  pressure4 = bme4.readPressure();
  Serial.print("BME280 #1 Pressure = "); Serial.println(pressure1);
  Serial.print("BME280 #2 Pressure = "); Serial.println(pressure2);
  Serial.print("BME280 #3 Pressure = "); Serial.println(pressure3);
  Serial.print("BME280 #4 Pressure = "); Serial.println(pressure4);


With that sketch running on the Arduino board, the output in the Serial Monitor will look like this:

Hopefully by comparing the 4xBME280 code to the previous 3xBME280 code example, the basic code pattern can be seen. It's essentially just a copy-paste of the same code to add one more instance:

Adafruit_BME280 bme4;  // BME280 #4

And then call tcaselect() same as done for the other BME280s:

tcaselect(3);      // TCA channel for bme4
bme4.begin();      // use the default address of 0x77
Don't forget to call Wire.begin() once before calling tcaselect().
Don't forget to call tcaselect() before accessing each sensor.

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

This page (Arduino) was last updated on Jun 22, 2024.

Text editor powered by tinymce.