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.beginTransmission(TCAADDR);
  Wire.write(1 << channel);
  Wire.endTransmission();  
}

void setup() {
  Serial.begin(9600);
  while(!Serial);
  Serial.println(F("Four BME280 Example"));

  // NOTE!!! VERY IMPORTANT!!!
  // Must call this once manually before first call to tcaselect()
  Wire.begin();
  
  // 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
  tcaselect(0);
  pressure1 = bme1.readPressure();
  tcaselect(1);
  pressure2 = bme2.readPressure();
  tcaselect(2);
  pressure3 = bme3.readPressure();
  tcaselect(3);
  pressure4 = bme4.readPressure();
 
  Serial.println("------------------------------------");
  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);

  delay(1000);
}

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 May 04, 2022.

This page (Arduino) was last updated on May 24, 2022.

Text editor powered by tinymce.