I am new to programming and I am working on this project. I have to run four ADXL345 sensors and collect the data from the individual sensor. However, based on my research ADXL345 only has 2 unique I2C addresses which is 0x53 and 0x1D. Therefore, I am using TCA9548A multiplexer to help me. But I can't seem to get the code to work. The expected output would be 12 different values (3axis * 4 sensors) separated by commas and a continuous live data. Please help me out, thank you. Following attached is the code I've been trying to work on. It will be good if someone can guide me on the hardware connection and the code.
#include <Wire.h>
#include <TCA9548A.h>
TCA9548A I2CMux;
int ADXL345Lstart = 0x53;
int ADXL345Rstart = 0x1D;
int ADXL345Lend = 0x53;
int ADXL345Rend = 0x1D;
int TCA9548A = 0x70;
float Xls_out, Yls_out, Zls_out, Xrs_out, Yrs_out, Zrs_out, Xle_out, Yle_out, Zle_out, Xre_out, Yre_out, Zre_out;
void setup() {
Serial.begin(9600);
Wire.begin();
I2CMux.begin(Wire);
// Connect ADXL345Lstart to Channel 2
I2CMux.openChannel(2);
Wire.beginTransmission(ADXL345Lstart);
Wire.write(0x2D);
Wire.write(8);
Wire.endTransmission();
delay(10);
I2CMux.closeChannel(2);
// Connect ADXL345Rstart to Channel 3
I2CMux.openChannel(3);
Wire.beginTransmission(ADXL345Rstart);
Wire.write(0x2D);
Wire.write(8);
Wire.endTransmission();
delay(10);
I2CMux.closeChannel(3);
// Connect ADXL345Lend to Channel 4
I2CMux.openChannel(4);
Wire.beginTransmission(ADXL345Lend);
Wire.write(0x2D);
Wire.write(8);
Wire.endTransmission();
delay(10);
I2CMux.closeChannel(4);
// Connect ADXL345Rend to Channel 5
I2CMux.openChannel(5);
Wire.beginTransmission(ADXL345Rend);
Wire.write(0x2D);
Wire.write(8);
Wire.endTransmission();
delay(10);
I2CMux.closeChannel(5);
// Send channel labels once at the beginning
Serial.println("CH1,CH2,CH3,CH4,CH5,CH6,CH7,CH8,CH9,CH10,CH11,CH12");
}
void loop(void) {
I2CMux.openChannel(2);
Wire.beginTransmission(ADXL345Lstart);
Wire.write(0x32);
Wire.endTransmission(false);
Wire.requestFrom(ADXL345Lstart, 6, true);
Xls_out = (Wire.read() | Wire.read() << 8);
Xls_out = Xls_out / 256;
Yls_out = (Wire.read() | Wire.read() << 8);
Yls_out = Yls_out / 256;
Zls_out = (Wire.read() | Wire.read() << 8);
Zls_out = Zls_out / 256;
// Print data from ADXL345Lstart
Serial.print(Xls_out, 4);
Serial.print(",");
Serial.print(Yls_out, 4);
Serial.print(",");
Serial.print(Zls_out, 4);
// Read data from ADXL345Rstart (Channel 3)
I2CMux.openChannel(3);
Wire.beginTransmission(ADXL345Rstart);
Wire.write(0x32);
Wire.endTransmission(false);
Wire.requestFrom(ADXL345Rstart, 6, true);
Xrs_out = (Wire.read() | Wire.read() << 8);
Xrs_out = Xrs_out / 256;
Yrs_out = (Wire.read() | Wire.read() << 8);
Yrs_out = Yrs_out / 256;
Zrs_out = (Wire.read() | Wire.read() << 8);
Zrs_out = Zrs_out / 256;
// Print data from ADXL345Rstart
Serial.print(",");
Serial.print(Xrs_out, 4);
Serial.print(",");
Serial.print(Yrs_out, 4);
Serial.print(",");
Serial.print(Zrs_out, 4);
// Read data from ADXL345Lend (Channel 4)
I2CMux.openChannel(4);
Wire.beginTransmission(ADXL345Lend);
Wire.write(0x32);
Wire.endTransmission(false);
Wire.requestFrom(ADXL345Lend, 6, true);
Xle_out = (Wire.read() | Wire.read() << 8);
Xle_out = Xle_out / 256;
Yle_out = (Wire.read() | Wire.read() << 8);
Yle_out = Yle_out / 256;
Zle_out = (Wire.read() | Wire.read() << 8);
Zle_out = Zle_out / 256;
// Print data from ADXL345Rstart
Serial.print(",");
Serial.print(Xle_out, 4);
Serial.print(",");
Serial.print(Yle_out, 4);
Serial.print(",");
Serial.print(Zle_out, 4);
// Read data from ADXL345Rend (Channel 5)
I2CMux.openChannel(5);
Wire.beginTransmission(ADXL345Rend);
Wire.write(0x32);
Wire.endTransmission(false);
Wire.requestFrom(ADXL345Rend, 6, true);
Xre_out = (Wire.read() | Wire.read() << 8);
Xre_out = Xre_out / 256;
Yre_out = (Wire.read() | Wire.read() << 8);
Yre_out = Yre_out / 256;
Zre_out = (Wire.read() | Wire.read() << 8);
Zre_out = Zre_out / 256;
// Print data from ADXL345Rend
Serial.print(",");
Serial.print(Xre_out, 4);
Serial.print(",");
Serial.print(Yre_out, 4);
Serial.print(",");
Serial.print(Zre_out, 4);
Serial.println();
delay(100);
}