Skip to content

Commit 8741476

Browse files
committed
begin() fail gracefully
try 0x76 address as well. record _sensorID
1 parent 799dae5 commit 8741476

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

Adafruit_BME280.cpp

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

124130
/**************************************************************************/
@@ -148,7 +154,8 @@ bool Adafruit_BME280::init()
148154
}
149155

150156
// check if sensor, i.e. the chip ID is correct
151-
if (read8(BME280_REGISTER_CHIPID) != 0x60)
157+
_sensorID = read8(BME280_REGISTER_CHIPID);
158+
if (_sensorID != 0x60)
152159
return false;
153160

154161
// reset the device using soft-reset
@@ -612,3 +619,14 @@ float Adafruit_BME280::seaLevelForAltitude(float altitude, float atmospheric)
612619

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

Adafruit_BME280.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
*/
3636
/**************************************************************************/
3737
#define BME280_ADDRESS (0x77)
38+
#define BME280_ADDRESS_ALTERNATE (0x76)
3839
/*=========================================================================*/
3940

4041
/**************************************************************************/
@@ -218,7 +219,7 @@ class Adafruit_BME280 {
218219

219220
float readAltitude(float seaLevel);
220221
float seaLevelForAltitude(float altitude, float pressure);
221-
222+
uint32_t sensorID(void);
222223

223224
protected:
224225
TwoWire *_wire; //!< pointer to a TwoWire object

examples/bme280test/bme280test.ino

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,21 @@ unsigned long delayTime;
3535

3636
void setup() {
3737
Serial.begin(9600);
38+
delay(4000); // time to get serial running
3839
Serial.println(F("BME280 test"));
3940

40-
bool status;
41+
unsigned status;
4142

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

@@ -79,4 +85,4 @@ void printValues() {
7985
Serial.println(" %");
8086

8187
Serial.println();
82-
}
88+
}

0 commit comments

Comments
 (0)