Hi everyone,
I’m working on reading data from 20 BMP280 sensors using a Redboard and 3 TCA9458A multiplexers. I know I could use 2 multiplexers by adjusting sensor adressess but I want to keep the setup very simple.
The problem I’am encountering is related to the number of sensors red. When I’m attempting to read a total of more than 14 sensors regardless how many are connected to multiplexers. All sensors are being initilized correctly but I read only the first 10 of them.
All multiplexers and sensors are working. The wiring is good. I also checked the power supply from the board. The code seems to be good too.
I don’t understand what’s happening. Maybe someone here can help me to solve this issue.
Here is my setup and my code. The first multiplexer connected to the board with tha adress 0X70 the second one 0X71 and the third one 0X72. On the image there is only 17 sensors.
#include <Wire.h>
#include <Adafruit_BMP280.h>
// Define the number of sensors per multiplexer
#define NUMBER_OF_SENSORS_BMP280_MUX1 8
#define NUMBER_OF_SENSORS_BMP280_MUX2 6
#define NUMBER_OF_SENSORS_BMP280_MUX3 0
//Addresses of multiplexers
#define MUX1_ADDR 0x70
#define MUX2_ADDR 0x71
#define MUX3_ADDR 0x72
int counter = 0;
Adafruit_BMP280 bmp280[NUMBER_OF_SENSORS_BMP280_MUX1 + NUMBER_OF_SENSORS_BMP280_MUX2 + NUMBER_OF_SENSORS_BMP280_MUX3]; // BMP280 sensors
// Function to control the TCA9548A multiplexer (with address)
void selectMultiplexer(uint8_t muxAddress, uint8_t bus) {
uint8_t busMask = 0;
if (bus < 8) busMask = 1 << bus; // Bus must be between 0 and 7, otherwise all channels will be disabled
Wire.beginTransmission(muxAddress); // TCA9548A address
Wire.write(busMask); // Select the bus
Wire.endTransmission();
}
void setup() {
Serial.begin(115200);
Wire.begin();
Wire.setClock(400000); // I2C clock speed
// Initialize the sensors on the 1st multiplexer (address 0x70)
for (byte i = 0; i < NUMBER_OF_SENSORS_BMP280_MUX1; i++) {
selectMultiplexer(MUX1_ADDR, i); // Select the bus
if (!bmp280[i].begin(0x76)) { // Initialize the BMP280 sensor
Serial.print("Could not find BMP280 sensor on MUX1 port ");
Serial.println(i);
} else {
Serial.print("BMP280 sensor initialized on MUX1 port ");
Serial.println(i);
}
delay(500);
selectMultiplexer(MUX1_ADDR, 0xFF); // Disable the bus
}
// Initialize the sensors on the 2nd multiplexer (address 0x71)
for (byte i = 0; i < NUMBER_OF_SENSORS_BMP280_MUX2; i++) {
selectMultiplexer(MUX2_ADDR, i); // Select the bus on the 2nd multiplexer
if (!bmp280[i + NUMBER_OF_SENSORS_BMP280_MUX1].begin(0x76)) { // Initialize the BMP280 sensor
Serial.print("Could not find BMP280 sensor on MUX2 port ");
Serial.println(i);
} else {
Serial.print("BMP280 sensor initialized on MUX2 port ");
Serial.println(i);
}
delay(500);
selectMultiplexer(MUX2_ADDR, 0xFF); // Disable the bus
}
// Initialize the sensors on the 3rd multiplexer (address 0x72)
for (byte i = 0; i < NUMBER_OF_SENSORS_BMP280_MUX3; i++) {
selectMultiplexer(MUX3_ADDR, i); // Select the bus
if (!bmp280[i + NUMBER_OF_SENSORS_BMP280_MUX1 + NUMBER_OF_SENSORS_BMP280_MUX2].begin(0x76)) { // Initialize the BMP280 sensor
Serial.print("Could not find BMP280 sensor on MUX3 port ");
Serial.println(i);
} else {
Serial.print("BMP280 sensor initialized on MUX3 port ");
Serial.println(i);
}
delay(500);
selectMultiplexer(MUX3_ADDR, 0xFF); // Disable the bus
}
String headers = "";
// MUX1 Headers
for (byte i = 0; i < NUMBER_OF_SENSORS_BMP280_MUX1; i++) {
headers += " MUX1_S" + String(i);
}
// MUX2 Headers
for (byte i = 0; i < NUMBER_OF_SENSORS_BMP280_MUX2; i++) {
headers += " MUX2_S" + String(i);
}
// MUX3 Headers
for (byte i = 0; i < NUMBER_OF_SENSORS_BMP280_MUX3; i++) {
headers += "MUX3_S" + String(i) + " ";
}
// Send the headers
Serial.println(headers);
}
void loop() {
String output = ""; // Declare the output string
Serial.print(counter);
Serial.print(" | ");
// Read BMP280 sensors on the 1st multiplexer
for (byte i = 0; i < NUMBER_OF_SENSORS_BMP280_MUX1; i++) {
selectMultiplexer(MUX1_ADDR, i); // Select the bus
float pressure = bmp280[i].readPressure(); // Read data from BMP280 sensor
output +=String(pressure) + " ; ";//output += "S" + String(i) + " MUX1 " + String(pressure) + " Pa "; output +=String(pressure) + " ; ";
selectMultiplexer(MUX1_ADDR, 0xFF); // Disable the bus
}
// Read BMP280 sensors on the 2nd multiplexer
for (byte i = 0; i < NUMBER_OF_SENSORS_BMP280_MUX2; i++) {
selectMultiplexer(MUX2_ADDR, i); // Select the bus on the 2nd multiplexer
float pressure = bmp280[i + NUMBER_OF_SENSORS_BMP280_MUX1].readPressure(); // Read data from BMP280 sensor
output +=String(pressure) + " ; ";
selectMultiplexer(MUX2_ADDR, 0xFF); // Disable the bus
}
// Read BMP280 sensors on the 3rd multiplexer
for (byte i = 0; i < NUMBER_OF_SENSORS_BMP280_MUX3; i++) {
selectMultiplexer(MUX3_ADDR, i); // Select the bus on the 2nd multiplexer
float pressure = bmp280[i + NUMBER_OF_SENSORS_BMP280_MUX1 + NUMBER_OF_SENSORS_BMP280_MUX2].readPressure(); // Read data from BMP280 sensor
output +=String(pressure) + " ; ";
selectMultiplexer(MUX3_ADDR, 0xFF); // Disable the bus
}
Serial.println(output);
delay(10);
counter++; // Increment the counter
if (counter >= 10) {
while (1); // Infinite loop after 30 readings
}
}