Help with TCA9548A with four ADXL345 run by Arduino UNO

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);
}

If the ADXL345 boards have address pins, then you don't need an I2C muxer.
The address pin can be used as chip select pin.
Set one of the boards to 0x53 and the other three to 0x1D (with four Arduino output pins).
Then talk to 0x53.
Leo..

I have four ADXL345 sensors. Based on my research to run four sensors together and receive the data individually I will need 4 unique addresses. However, ADXL345 only has 2 unique address. Which is why I choose to use the TCA9548A multiplexer.

No, you only need two unique addresses for any mumber of sensors.
As said, set one board to the address you talk to, and the other boards to a different address.
Leo..

Do you have a sample code for me? I can't seem to understand, thank you for understanding. I only have one Arduino UNO board, four ADXL345 sensors and a TCA9548A multiplexer

Post a link to the ADXL345 boards you have. There are many different ones.
Do they have an address select or chip select pin.
Do they have logic level shifting onboard.
Did you use one board first, before scaling up to four boards.
Did that work.
Leo..

Wawa is suggesting an alternative method for accessing similar devices rather than using an I2C mux. It's not as commonly discussed, but completely valid, if you have access to the address pins of the Accelerometer. See the description of the SDO pin on the page he linked to.
If such a pin is not accessible, then the I2C mux may be your only alternative.

This is the type of ADXL345 that I am using.

https://sg.cytron.io/p-adxl345-3-axis-accelerometer-breakout?r=1&gad=1&gclid=Cj0KCQjwpc-oBhCGARIsAH6ote8zIwGxg8ULzU6pqkyq9UK9NVO_bdvo_IbNsYmVAYJfxJyKnGIcvMkaAmp8EALw_wcB

This is the type of TCA9548A multiplexer that I am using.

https://shopee.sg/product/56539845/10335131306?gclid=Cj0KCQjwpc-oBhCGARIsAH6ote8kyXnXAVgehqKFL9ng3Fp1_AYOF5bH_Ul5Y1GtmflRPwBP3_zc_HwaAlwrEALw_wcB

That's a 3-3volt-logic-only version of the board without the required level shifters to connect it to a 5volt-logic Arduino like the Uno. You shouldn't connect this version of the board to an Uno directly.

If you had bought ADXL345 boards with level shifters, then you wouldn't have needed the muxer. But in this case you should use the TCA9548, because that muxer has built-in logic level shifters.

You don't have to use different addresses for the sensors, since the muxer only will talk to one sensor at the time anyway.
Leo..

1 Like

How can I do that? Please guide me. I need help with the code and hardware connection. Above is the code that I tried doing.

I don't like that you didn't mention that you also have posted this on another site.
Good luck.
Leo..

I apologise that I didn't mention I posted this on another side. I posted it on two site as I am trying out the forum.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.