Once the BME280 is up and running and providing readings, If it is disconnected from the I2C bus the readings are frozen at their last value. When it is reconnected again - The values are still frozen.
I’m using the Sparkfun Qwiic BME280 module with the Sparkfun BME280 library for Arduino.
ReadAllRegisters shows that when the BME280 is disconnected the register values all fall to zero.
The snippet below restores two registers once the BME280 is reconnected allowing it to continue operation.
// Restore BME280 Connection
#include <Wire.h>
#include "SparkFunBME280.h"
BME280 mySensor;
// Global registers for restore
byte initBME280_CTRL_HUMIDITY_REG;
byte initBME280_CTRL_MEAS_REG;
void setup()
{
Serial.begin(9600); while(!Serial); // Wait for serial connection
Wire.begin();
while (mySensor.beginI2C() == false) //Begin communication over I2C
{
Serial.println("The sensor did not respond. Please check wiring.");
delay (1000); // Pause
}
// Store register values
initBME280_CTRL_HUMIDITY_REG = mySensor.readRegister(BME280_CTRL_HUMIDITY_REG);
initBME280_CTRL_MEAS_REG = mySensor.readRegister(BME280_CTRL_MEAS_REG);
}
void loop()
{
// Do other stuff....
Serial.print("Humidity: ");Serial.print(mySensor.readFloatHumidity(), 2);
Serial.print(" Pressure: ");Serial.print(mySensor.readFloatPressure(), 0);
Serial.print(" Alt: ");Serial.print(mySensor.readFloatAltitudeMeters(), 1);
Serial.print(" Temp: "); Serial.print(mySensor.readTempC(), 2);
Serial.println();
// Reconnect Sensor
if ((mySensor.readRegister(BME280_CTRL_HUMIDITY_REG) == 0) && (mySensor.readRegister(BME280_CTRL_MEAS_REG) == 0))
{
// Kiss restart the connection
while (mySensor.readRegister(BME280_CHIP_ID_REG) != 0x60) //Wait for BME280 to reconnect
{
Serial.println("The sensor has disconnected.");
delay(1000);
}
Serial.println("The sensor has reconnected.");
// Restore registers
mySensor.writeRegister(BME280_CTRL_HUMIDITY_REG, initBME280_CTRL_HUMIDITY_REG);
mySensor.writeRegister(BME280_CTRL_MEAS_REG, initBME280_CTRL_MEAS_REG);
}
delay(1000);
}
An alternate way could be to reinit the sensor with .beginI2C Though I’m not sure if this would allocate resources each time it reconnected but never release them.
void loop()
{
// Do other stuff....
// Reconnect Sensor
if ((mySensor.readRegister(BME280_CTRL_HUMIDITY_REG) == 0) && (mySensor.readRegister(BME280_CTRL_MEAS_REG) == 0))
{
// Kiss restart the connection
while (mySensor.beginI2C() == false) //Begin communication over I2C
{
Serial.println("The sensor has disconnected.");
delay(1000);
}
Serial.println("The sensor has reconnected.");
}
}