OK, to actually code up a simple example using two BME280's, one without any changes having the default address of 0x77 and one with the solder pad connected to set the alternate address of 0x76.
Here's the sketch code:
// SPDX-FileCopyrightText: 2022 Carter Nelson for Adafruit Industries // // SPDX-License-Identifier: MIT #include <Adafruit_BME280.h> // For each device, create a separate instance. Adafruit_BME280 bme1; // BME280 #1 @ 0x77 Adafruit_BME280 bme2; // BME280 #2 @ 0x76 void setup() { Serial.begin(9600); while(!Serial); Serial.println(F("Two BME280 Example")); // NOTE: There's no need to manually call Wire.begin(). // The BME280 library does that in its begin() method. // In the call to begin, pass in the I2C address. // If left out, the default address is used. // But also OK to just be explicit and specify. bme1.begin(0x77); // address = 0x77 (default) bme2.begin(0x76); // address = 0x76 } void loop() { float pressure1, pressure2; // Read each device separately pressure1 = bme1.readPressure(); pressure2 = bme2.readPressure(); Serial.println("------------------------------------"); Serial.print("BME280 #1 Pressure = "); Serial.println(pressure1); Serial.print("BME280 #2 Pressure = "); Serial.println(pressure2); delay(1000); }
With that sketch running on the Arduino board, the output in the Serial Monitor will look like this:
Note how for each device, there is a separate instance created:
Adafruit_BME280 bme1; // BME280 #1 @ 0x77 Adafruit_BME280 bme2; // BME280 #2 @ 0x76
And then for each, the begin()
function is called and the address is specified:
bme1.begin(0x77); // address = 0x77 (default) bme2.begin(0x76); // address = 0x76
Here we intentionally specify the default 0x77 address just to be explicit.
Once that is taken care of, the two instances can be used to directly read the sensor values:
pressure1 = bme1.readPressure(); pressure2 = bme2.readPressure();
Page last edited January 22, 2025
Text editor powered by tinymce.