Dear Seeed team,
I have similar trouble with 3 BME688 sensors plugged in the multiplexer. I manage to see the multiplexer and sensors when using a scanner but when adapting the BME688 code to work with the multiplexer, it never initialize. I am not an expert coder and would very much like to understand what is wrong with it.
Here is the code :
#include <Wire.h>
#include <TCA9548A.h>
#include “seeed_bme680.h”
#define TCA_ADDR 0x70 // I2C address of the TCA9548A multiplexer
TCA9548A tca;
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define BME680_IIC_ADDR1 uint8_t(0x76)
#define BME680_IIC_ADDR2 uint8_t(0x77)
#define BME680_IIC_ADDR3 uint8_t(0x78)
Seeed_BME680 bme680_1;
Seeed_BME680 bme680_2;
Seeed_BME680 bme680_3;
void setup() {
Serial.begin(9600);
while (!Serial);
Wire.begin();
tca.begin(Wire);
Serial.println("Serial start!!!");
delay(100);
initializeBME680(bme680_1, BME680_IIC_ADDR1, "Sensor 1");
initializeBME680(bme680_2, BME680_IIC_ADDR2, "Sensor 2");
initializeBME680(bme680_3, BME680_IIC_ADDR3, "Sensor 3");
}
void loop() {
// Reading from the first BME680 sensor
tca.openChannel(0);
readAndPrintSensorData(bme680_1, "Sensor 1");
// Reading from the second BME680 sensor
tca.openChannel(1);
readAndPrintSensorData(bme680_2, "Sensor 2");
// Reading from the third BME680 sensor
tca.openChannel(2);
readAndPrintSensorData(bme680_3, "Sensor 3");
delay(2000);
}
void initializeBME680(Seeed_BME680 &bme, uint8_t i2cAddress, const char *sensorName) {
bme = Seeed_BME680(i2cAddress);
if (!bme.init()) {
Serial.print("Failed to initialize ");
Serial.print(sensorName);
Serial.println(" BME680 sensor! Can't find device!");
while (1);
}
}
void readAndPrintSensorData(Seeed_BME680 &bme, const char *sensorName) {
if (bme.read_sensor_data()) {
Serial.print("Failed to perform reading on ");
Serial.println(sensorName);
return;
}
Serial.print(sensorName);
Serial.print(" - Temperature: ");
Serial.print(bme.sensor_result_value.temperature);
Serial.print(" C, Pressure: ");
Serial.print(bme.sensor_result_value.pressure / 1000.0);
Serial.print(" KPa, Humidity: ");
Serial.print(bme.sensor_result_value.humidity);
Serial.print(" %, Gas Resistance: ");
Serial.print(bme.sensor_result_value.gas / 1000.0);
Serial.println(" Kohms");
Serial.println();
}