Multiple BMP280 pressure sensors with TCA9548 mux; inconsistent readings

My first post.
Hardware: Pro Mini clone ATMEGA 168p 5v 16 MHz purchased via AliExpress https://www.aliexpress.com/item/32821902128.html
also WEMOS CH340 breakout board for serial communication
TCA9548A I2C multiplixer: https://www.aliexpress.com/item/32648420655.html
Three sensor breakout boards with AHT20 and BMP20 on each https://www.aliexpress.com/item/1005003752792510.html

The sensors are all on the same I2C address so the TCA9548 multiplexer is to switch I2C bus before reading each sensor.

The problem: When I use one sensor I see the expected results, temperature, pressure and altitude, extending to two sensors the values for the first sensor look sincorrect and extending to three sensors the first sensor looks incorrect while the other two look correct.
Example temp deg C, pressure, altitude
Sensor 0 23.51 803.38 2059.94
Sensor 1 25.29 1006.03 62.50
Sensor 2 25.60 1006.19 61.04

Any guidance about what to try welcome. There is probably something in my code that is obvious but I can not see it.

Code:

/////////////////////////////////////////////////////////////////////////////////
// BMP280_DEV - I2C Communications - 3 sensors using arrays
/////////////////////////////////////////////////////////////////////////////////

#include <BMP280_DEV.h>                           // Include the BMP280_DEV.h library
#include <Wire.h>

// BUS selection function
void selectBus(uint8_t bus) {
  Wire.beginTransmission(0x70);                  // begin transmission to address 70
  Wire.write(1 << bus);                          // shift 1 left by number of bits equal to bus number (0 to 7)and write it
  Wire.endTransmission();
  //Serial.print("bus set to ");
  //Serial.println(bus);
}
int num_sensors = 3;
float temperature[3], pressure[3], altitude[3];      // Create the temperature, pressure and altitude variables
BMP280_DEV bmp280[3];                                // Instantiate a BMP280_DEV object
int sensorStatus[3];

void setup()
{
  Serial.begin(19200);                           // Initialise the serial port

  for (int j = 0; j < num_sensors; j++) {
    Serial.print("initialising sensor ");
    Serial.println(j);
    selectBus(j);
    sensorStatus[j] = bmp280[j].begin(FORCED_MODE);
    //sensorStatus[j] = bmp280[j].begin(NORMAL_MODE);
    //bmp280[j].setPresOversampling(OVERSAMPLING_X4);    // Set the pressure oversampling to X4
    //bmp280[j].setTempOversampling(OVERSAMPLING_X1);    // Set the temperature oversampling to X1
    //bmp280[j].setIIRFilter(IIR_FILTER_4);              // Set the IIR filter to setting 4
    if (!sensorStatus[j] ) {
      Serial.print("sensor ");
      Serial.print(j);
      Serial.println(" initialisation failed");
    } else {
      Serial.print("sensor ");
      Serial.print(j);
      Serial.println(" initialised OK");
    }
    delay(500); // not really necessary
  }
}
// note: if initialisation fails execution continues into loop
void loop()
{
  for (int i = 0; i < num_sensors; i++) {
    selectBus(i);
    bmp280[i].startForcedConversion();                 // Start each BMP280 for forced conversion
  }
  for (int k = 0; k < num_sensors; k++) {
    selectBus(k);
    Serial.print("Sensor ");
    Serial.print(k);
    Serial.print(" ");
    //bmp280[k].getCurrentPressure(pressure[k]);
    bmp280[k].getCurrentMeasurements(temperature[k], pressure[k], altitude[k]);
    Serial.print(temperature[k]);
    Serial.print(" ");
    Serial.print(pressure[k]);
    Serial.print(" ");
    Serial.print(altitude[k]);
    Serial.println(" ");
  }
  Serial.println("");
  delay(2000);
}

Schematic:
schematic

Steps taken to resolve this problem:

Modified code to use arrays of sensors as suggested in this post

Wrote a similar sketch to read the temperatures from the AHT20 sensors, based on the Adafruit aht test example, this reports the temperatures as expected. This suggests the TCA9548 works.

I added num_sensors so it can access 1, 2 or 3 sensors. With one sensor all appears to work as expected, with two sensors the first reports unexpected results and the second shows what look right and with three sensors the last two look correct.
I swapped the sensors around in case they were different, but that made no difference.

My electronics knowledge is fairly basic, I am new to C++ but happy in PHP and Javascript.

Change that to getMeasurements() method and check the return value to see if the returned values are valid. In forced mode you have to check if the values are already valid.

Thank you for replying, I'll modify the code and test it out.

pylon thank you for the solution :grinning: I am posting the working code in case anyone else has the same issue.
I suspect there is something else happening because sometimes the results sometimes inconsistent, but as it says in the post about how to use the forum 'Code ALWAYS works; that is the nature of code.'

/////////////////////////////////////////////////////////////////////////////////
// BMP280_DEV - I2C Communications - 3 sensors using arrays
/////////////////////////////////////////////////////////////////////////////////

#include <BMP280_DEV.h>                           // Include the BMP280_DEV.h library
#include <Wire.h>

// BUS selection function
void selectBus(uint8_t bus) {
  Wire.beginTransmission(0x70);                  // begin transmission to address 70
  Wire.write(1 << bus);                          // shift 1 left by number of bits equal to bus number (0 to 7)and write it
  Wire.endTransmission();
  //Serial.print("bus set to ");
  //Serial.println(bus);
}
int num_sensors = 3;
float temperature[3], pressure[3], altitude[3];      // Create the temperature, pressure and altitude variables
BMP280_DEV bmp280[3];                                // Instantiate a BMP280_DEV object
int sensorStatus[3];

void setup()
{
  Serial.begin(19200);                           // Initialise the serial port

  // make sure all variables exist and are initialised
  //  for (int n = 0; n < 5; n++) {
  //    temperature[n] = 99.00;
  //    pressure[n] = 1000.00;
  //    altitude[n] = 49.99;
  //    sensorStatus[n] = 1;
  //  }

  for (int j = 0; j < num_sensors; j++) {
    Serial.print("initialising sensor ");
    Serial.println(j);
    selectBus(j);
    sensorStatus[j] = bmp280[j].begin(FORCED_MODE);
    //sensorStatus[j] = bmp280[j].begin(NORMAL_MODE);
    //bmp280[j].setPresOversampling(OVERSAMPLING_X4);    // Set the pressure oversampling to X4
    //bmp280[j].setTempOversampling(OVERSAMPLING_X1);    // Set the temperature oversampling to X1
    //bmp280[j].setIIRFilter(IIR_FILTER_4);              // Set the IIR filter to setting 4
    if (!sensorStatus[j] ) {
      Serial.print("sensor ");
      Serial.print(j);
      Serial.println(" initialisation failed");
    } else {
      Serial.print("sensor ");
      Serial.print(j);
      Serial.println(" initialised OK");
    }
    delay(500); // not really necessary
  }
}
// note: if initialisation fails execution continues into loop
void loop()
{
  for (int i = 0; i < num_sensors; i++) {
    selectBus(i);
    bmp280[i].startForcedConversion();                 // Start each BMP280 for forced conversion
  }
  for (int k = 0; k < num_sensors; k++) {
    selectBus(k);
    //Serial.print("Sensor ");
    //Serial.print(k);
    //Serial.print(" ");
    //bmp280[k].getCurrentPressure(pressure[k]);
    //bmp280[k].getCurrentMeasurements(temperature[k], pressure[k], altitude[k]);

    sensorStatus[k] = bmp280[k].getMeasurements(temperature[k], pressure[k], altitude[k]);
    if (sensorStatus[k]) {
      {
        Serial.print("Sensor ");
        Serial.print(k);
        Serial.print(" ");
        Serial.print(temperature[k]);
        Serial.print(" ");
        Serial.print(pressure[k]);
        Serial.print(" ");
        Serial.print(altitude[k]);
        Serial.println(" ");
      }
      //Serial.println("");
      //delay(5);
    }
  }
}

Output is now what I expected to see
good-output

It seems to me that the "Solution" mark should point to the post #2

Thanks for pointing that out b707, I've made the alteration.