adafruit / Adafruit_BME280_Library

Arduino Library for BME280 sensors
Other
328 stars 301 forks source link

Change begin() to check both I2C addrs only if addr not passed #70

Closed gt500sc closed 4 years ago

gt500sc commented 4 years ago

The begin() method currently checks the I2C address that was passed (or the base address 0x77), and if that fails, it checks the other address (base 0x77 or alternate 0x76). This can cause a false success return if two sensors are being used and one is successful and the other is not. In this case, both sensor objects will return success but point to the same device address.

Checking both addresses should only be done if no address was passed (e.g. begin(void) or begin(TwoWire*)). In cases where the address is passed, only that address should be checked (begin(addr) or begin(addr, TwoWire*)).

Test can be done using one BME280 sensor on address 0x76 with the following code:

Adafruit_BME280 bme1, bme2;

bool rc1 = bme1.begin(0x77);  // should be false
bool rc2 = bme2.begin(0x76);  // should be true

rc1 = bme1.begin();  // should be true because both addresses were checked

Note that prior to this change, all of the above begin() calls would return true because both addresses were always checked.

gt500sc commented 4 years ago

I made the requested changes in my repository and I think it pushed them here, but not sure if I did it right (github noob). Please let me know if there is something else I need to do. Thanks!