Skip to content

Commit 1f2d47a

Browse files
authored
Merge pull request #49 from sellensr/master
begin() fail gracefully
2 parents 920de5b + 46eb77e commit 1f2d47a

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

Adafruit_BME280.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,15 @@ bool Adafruit_BME280::begin(uint8_t addr, TwoWire *theWire)
117117
/**************************************************************************/
118118
bool Adafruit_BME280::begin(void)
119119
{
120+
bool status = false;
120121
_i2caddr = BME280_ADDRESS;
121122
_wire = &Wire;
122-
return init();
123+
status = init();
124+
if(!status){
125+
_i2caddr = BME280_ADDRESS_ALTERNATE;
126+
status = init();
127+
}
128+
return status;
123129
}
124130

125131
/**************************************************************************/
@@ -149,7 +155,8 @@ bool Adafruit_BME280::init()
149155
}
150156

151157
// check if sensor, i.e. the chip ID is correct
152-
if (read8(BME280_REGISTER_CHIPID) != 0x60)
158+
_sensorID = read8(BME280_REGISTER_CHIPID);
159+
if (_sensorID != 0x60)
153160
return false;
154161

155162
// reset the device using soft-reset
@@ -613,3 +620,14 @@ float Adafruit_BME280::seaLevelForAltitude(float altitude, float atmospheric)
613620

614621
return atmospheric / pow(1.0 - (altitude/44330.0), 5.255);
615622
}
623+
624+
/**************************************************************************/
625+
/*!
626+
Returns Sensor ID found by init() for diagnostics
627+
@returns Sensor ID 0x60 for BME280, 0x56, 0x57, 0x58 BMP280
628+
*/
629+
/**************************************************************************/
630+
uint32_t Adafruit_BME280::sensorID(void)
631+
{
632+
return _sensorID;
633+
}

Adafruit_BME280.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@
3535
@brief default I2C address
3636
*/
3737
/**************************************************************************/
38-
#define BME280_ADDRESS (0x77)
38+
#define BME280_ADDRESS (0x77) // Primary I2C Address
39+
/**************************************************************************/
40+
/*!
41+
@brief alternate I2C address
42+
*/
43+
/**************************************************************************/
44+
#define BME280_ADDRESS_ALTERNATE (0x76) // Alternate Address
3945
/*=========================================================================*/
4046

4147
/**************************************************************************/
@@ -219,7 +225,7 @@ class Adafruit_BME280 {
219225

220226
float readAltitude(float seaLevel);
221227
float seaLevelForAltitude(float altitude, float pressure);
222-
228+
uint32_t sensorID(void);
223229

224230
protected:
225231
TwoWire *_wire; //!< pointer to a TwoWire object

examples/bme280test/bme280test.ino

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,21 @@ unsigned long delayTime;
3636

3737
void setup() {
3838
Serial.begin(9600);
39+
while(!Serial); // time to get serial running
3940
Serial.println(F("BME280 test"));
4041

41-
bool status;
42+
unsigned status;
4243

4344
// default settings
4445
// (you can also pass in a Wire library object like &Wire2)
4546
status = bme.begin();
4647
if (!status) {
47-
Serial.println("Could not find a valid BME280 sensor, check wiring!");
48+
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
49+
Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
50+
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
51+
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
52+
Serial.print(" ID of 0x60 represents a BME 280.\n");
53+
Serial.print(" ID of 0x61 represents a BME 680.\n");
4854
while (1);
4955
}
5056

0 commit comments

Comments
 (0)