Initial confirmation of two BME280 sensors

I have two BME280’s connected via I2C, I just want to check the code is correct for startup.

When I unplug one or the other, it shows the relevant message “Error - check sensor no.x”.
But it does not show the other ‘connected’ sensor output in serial monitor.

  1. Is it ok to use “status =” code for both sensors? (it shows as orange text).
  2. How to get it to skip past the sensor with the error and show the other sensor output anyway?

Here is the full code:

/***************************************************************************
  This is a library for the BME280 humidity, temperature & pressure sensor

  Designed specifically to work with the Adafruit BME280 Breakout
  ----> http://www.adafruit.com/products/2650

  These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  to interface. The device's I2C address is either 0x76 or 0x77.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!

  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ***************************************************************************/

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>


#define SEALEVELPRESSURE_HPA (1033.00)  //default 1013.25 or 1 ATM

//Setup as I2C 
Adafruit_BME280 bme1; // I2C to Vcc (0x77)
Adafruit_BME280 bme2; // I2C to Gnd (0x76)

unsigned long delayTime;

void setup() {
    Serial.begin(9600);
    Serial.println(F("BME280 test"));

bool status;

// start first sensor
status = bme1.begin(0x77); // I2C - SDO connection to Vcc selects (0x77)
    if (!status)
    {
        Serial.print("Error - check sensor no.1");
        while (1);
    }


// start second sensor
status = bme2.begin(0x76); // I2C - SDO connection to GND selects (0x76)
    if (!status)
    {
        Serial.print("Error - check sensor no.2");
        while (1);
    }

  
    Serial.println("-- Default Test --");
    delayTime = 5000;

    Serial.println();
}


void loop() { 
    printValues();
    delay(delayTime);
}


void printValues() {

    Serial.println("Sensor no.1");  
    Serial.print("Temperature = ");
    Serial.print(bme1.readTemperature());
    Serial.println(" *C");

    Serial.print("Pressure = ");

    Serial.print((bme1.readPressure() / 100.0F) + 0);
    Serial.println(" hPa");

    Serial.print("Approx. Altitude = ");
    Serial.print(bme1.readAltitude(SEALEVELPRESSURE_HPA));
    Serial.println(" m");

    Serial.print("Humidity = ");
    Serial.print(bme1.readHumidity());
    Serial.println(" %");
    
    Serial.println();
  

    Serial.println("Sensor no.2");  
    Serial.print("Temperature = ");
    Serial.print(bme2.readTemperature());
    Serial.println(" *C");

    Serial.print("Pressure = ");

    Serial.print((bme2.readPressure() / 100.0F) + 0);
    Serial.println(" hPa");

    Serial.print("Approx. Altitude = ");
    Serial.print(bme2.readAltitude(SEALEVELPRESSURE_HPA));
    Serial.println(" m");

    Serial.print("Humidity = ");
    Serial.print(bme2.readHumidity());
    Serial.println(" %");

    Serial.println("--------------------");
    Serial.println();
    
}

Thanks for any advice

When I unplug one or the other, it shows the relevant message "Error - check sensor no.x".
But it does not show the other 'connected' sensor output in serial monitor.

If one sensor is not connected, you print an error message and then enter an infinite loop.

Wait long enough, and the other message will appear. How long? Well, an infinite amount of time plus a few microseconds, obviously.

PaulS:
If one sensor is not connected, you print an error message and then enter an infinite loop.

Wait long enough, and the other message will appear. How long? Well, an infinite amount of time plus a few microseconds, obviously.

Thanks PaulS, yes I see that now.

So instead what is usual/best practice code in this case?

I'm thinking:

Check sensor 1
If ok move on
If not ok wait 10 seconds, print error message and move onto sensor 2

If not ok wait 10 seconds, print error message and move onto sensor 2

Why wait? If the sensor isn't there, print an error, make a note not to try to read from the sensor, and move on immediately.

PaulS:
Why wait? If the sensor isn't there, print an error, make a note not to try to read from the sensor, and move on immediately.

True, thanks for the help :slight_smile: