Specify the I2C address

Hi,

I have two BME680 sensors connected through the same I2C interface. Apparently I can change the address of a sensor from 0x77 to 0x76 by connecting SDO to GND and use this to use both simultaneously. How would I write the commands to switch between communicating with the two? I found the command Wire.begin(), but don't know how to use it.

Also, I'm reading stuff about pull up resistors. Do I need them in this situation and between which pins? Thanks

Peter

You specify the address via Wire.beginTransmission():

So in the loop, I would do Wire.beginTransmission(0x77), that would make bme.performReading() work only with one sensor, and if I run Wire.endTransmission() and Wire.beginTransmission(0x76), I switch to communicating with the other?

Generally that's it but now the bme.performReading() thing makes me think that maybe you're using a library for your sensor. In that case, it's likely the library handles all the I2C stuff for you and you should not be using the Wire library directly in your sketch. I can't know for sure unless you say if you're using a library and which library it is.

Yeah, that's right, the #include <Adafruit_Sensor.h> and #include "Adafruit_BME680.h". That's why I didn't understand where to put the Wire command, there's no such command in my code. What would you recommend me to do, would it be too difficult to make it work?

OK. What you need to do is forget about the Wire library. The Adafruit_BME680 library is handling that all for you so you should not be using Wire at all in your code. What you want to do is to create an Adafruit_BME680 object for each sensor:

Adafruit_BME680 bme1;
Adafruit_BME680 bme2;

Then in setup, you can set the address for each object:

bme1.begin(0x77);
bme2.begin(0x76);

To get a big more fancy, you could use an array for the objects:

Adafruit_BME680 bme[2];
bme[0].begin(0x77);
bme[1].begin(0x76);

Oh, that's perfect, that makes a lot of sense! I also have these lines:

// Set up oversampling and filter initialization
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320*C for 150 ms

I assume I need two sets, one for each sensor?

Exactly. This is the sort of thing where the array can be useful. Without the array you need to do something like this:

// Set up oversampling and filter initialization
  bme1.setTemperatureOversampling(BME680_OS_8X);
  bme1.setHumidityOversampling(BME680_OS_2X);
  bme1.setPressureOversampling(BME680_OS_4X);
  bme1.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme1.setGasHeater(320, 150); // 320*C for 150 ms
  bme2.setTemperatureOversampling(BME680_OS_8X);
  bme2.setHumidityOversampling(BME680_OS_2X);
  bme2.setPressureOversampling(BME680_OS_4X);
  bme2.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme2.setGasHeater(320, 150); // 320*C for 150 ms

With the array you can do it like this:

Adafruit_BME680 bme[2];
byte BME680address[]={0x77,0x76};
// Set up oversampling and filter initialization
for (byte sensorIndex = 0; sensorIndex < (sizeof(bme) / sizeof(bme[0])); sensorIndex++) {
  bme[sensorIndex].begin(BME680address[sensorIndex]);
  bme[sensorIndex].setTemperatureOversampling(BME680_OS_8X);
  bme[sensorIndex].setHumidityOversampling(BME680_OS_2X);
  bme[sensorIndex].setPressureOversampling(BME680_OS_4X);
  bme[sensorIndex].setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme[sensorIndex].setGasHeater(320, 150); // 320*C for 150 ms
}

The (sizeof(bme) / sizeof(bme[0]) automatically determines the number of sensors. In addition to avoiding code duplication, the array system will also allow your code to adjust to any number of sensors (assuming you can give additional sensors unique addresses) by only changing the bme array size and address array.

Oh, amazing. Thanks for your help! I have only the two, which is fine because I don't know how to change the address again.
And do you have any tips regarding the pull-up resistors?

I think that will depend on whether you're using BME680 sensor modules or just bare sensors, and if the former, which modules you're using. I would expect that the modules will have any necessary resistors already but I could certainly be wrong.

I got the purple breakout boards, these ones: https://www.banggood.com/CJMCU-680-BME680-BOSCH-Temperature-And-Humidity-Pressure-Sensor-p-1268323.html?gmcCountry=GB&currency=GBP&createTmp=1&cur_warehouse=CN&utm_source=googleshopping&utm_medium=cpc_elc&utm_content=zouzou&utm_campaign=pla-gb-ele-diy-acs-pc&gclid=Cj0KCQiA8f_eBRDcARIsAEKwRGcfLARi43mB3M85d3kTPamo3GGKPOarpK1QFF9jrwniFszxC1sXNrUaAlE2EALw_wcB

Sorry to report that it doesn't work for me. the bme1.begin(0x77) or the other one doesn't seem to work, even though I got an I2C checker and those two indeed have the two addresses. Do you have any idea what's wrong? the code is

Adafruit_BME680 bme1;
Adafruit_BME680 bme2;

void setup() {
 Serial.begin(9600);
 while (!Serial);
 Serial.println(F("BME680 test"));

 if (!bme1.begin(0x77)) {
   Serial.println("Could not find a valid BME680 sensor, check wiring!");
   while (1);
 }

Does it work if you only have a single object, and only a single BME260 sensor connected?:

Adafruit_BME680 bme1;
void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println(F("BME680 test"));

  if (!bme1.begin(0x77)) {
    Serial.println("Could not find a valid BME680 sensor, check wiring!");
    while (1);
  }
}
void loop() {}

Yeah, that actually does work, the problem starts when I do the begin of both, it says "BMB" in the serial and that's it...

Does the working code still work if you connect both sensors?

Yeah, I have both connected and it works with either 0x77 or 0x76.

OK, it definitely sounds like the multiple objects are causing the problem. Unfortunately I don't have any experience with this library and don't own any of the BME680 so I'm not sure how to proceed. Maybe someone else here on the forum will have some ideas.

Oh, don't worry. Thanks for your help, I really appreciate it!

It actually works now, I guess restart was needed..

Well that's quite strange. I'm glad to hear it's working now. Enjoy!
Per