Reconnect BME280 on the fly (Arduino)

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.");
  }
}

Yeah, don’t do that. I2C devices are not hot swappable.

1 Like

Hi YellowDog
It’s not intended to be unplugged while it’s running. But if it does have a problem it needs to be able to reconnect on it’s own and not have me reboot the Arduino to get it going again.
Just for clarification, The sensor is at the end of a SparkFun i2c differential bus module.
And the whole unit is operating right next to a compressor and other electrical equipment.
(ie. a noisy EMF environment)

1 Like

I’m not sure what I was doing wrong before but now if I restore BME280_CTRL_HUMIDITY_REG and BME280_CTRL_MEAS_REG operation of the BME280 continues with correct values.

I’ll edit the original post to reflect this way of recovering from sensor disconnect.

1 Like

Just to note: It’s important to write BME280_CTRL_HUMIDITY_REG before BME280_CTRL_MEAS_REG

As per the BME280 spec sheet notes.

1 Like