Go Down

Topic: Connecting 2 ADXL345 Accelerometer sensors over I2C (Read 276 times) previous topic - next topic

Harinder85

Hi !
I am working on a project and need to connect 2 accelerometers. ADXL345 is the sensor I am working using. I2C Address and alternate address for the same is 0x1D and 0x53.

If i don't give an address while initializing the object, it works, but both sensors give same output
If i give address as 0x1D the sensor gives output as 0
If i Give address as 0x53 there is no output.

I want to read the data from both the sensors.

ADXL345 Accelerometer:
https://robu.in/product/adxl345-tripple-axis-accelerometer-board/?gclid=Cj0KCQiA-8PjBRCWARIsADc18TLzIZjSFGcHNeAvvxHKwt22WEjE3Y-f-jte0CEpljUGM-f55tGH_lMaAhd4EALw_wcB

jremington

#1
Apr 16, 2019, 05:23 am Last Edit: Apr 16, 2019, 05:24 am by jremington
To change the I2C address of one of the modules to 0x53, ground the ALT_ADDRESS pin. Details are in the ADXL345 data sheet.

Harinder85

#2
Apr 16, 2019, 12:37 pm Last Edit: Apr 16, 2019, 12:40 pm by Harinder85
Hi,

I have already connected SDO pin of one sensor to ground and the other sensors SDO pin is connected to 5V.

used I2C scanner sketch and it outputs the addresses as 0x1D and 0x53 for the connected devices.

But it does't work

jremington

According to the github source, the library you are using has a single, hard wired I2C address:

Code: [Select]
#define ADXL345_DEVICE (0x53) // Device Address for ADXL345

This would never work for two devices.

Harinder85

Ok, I got it !

Is there some other library so I can connect 2 ADXL345 Accelerometers?

jremington


Harinder85

#6
Apr 19, 2019, 05:24 pm Last Edit: Apr 19, 2019, 05:25 pm by Harinder85
got it done using wire.h only

Thanks for the help !

jremington

Great! Please post your code, to help others with the same problem.

Harinder85

Following is the code for same


Code: [Select]
#include<Wire.h>

#define accel_module (0x1d)         // SDO-> Vcc
#define accel_module2 (0x53)       // SDO-> GND

byte values[6];
byte values2[6];
char output[512];
char output2[512];

void getAccelration(int accelSensor, int cal)
{
  int xyzregister = 0x32;
  int x, y, z;

  Wire.beginTransmission(accelSensor);
  Wire.write(0x2D);
  Wire.write(0);
  Wire.endTransmission();
  Wire.beginTransmission(accelSensor);
  Wire.write(0x2D);
  Wire.write(16);
  Wire.endTransmission();
  Wire.beginTransmission(accelSensor);
  Wire.write(0x2D);
  Wire.write(8);
  Wire.endTransmission();

  Wire.beginTransmission(accelSensor);
  Wire.write(xyzregister);
  Wire.endTransmission();

  Wire.beginTransmission(accelSensor);
  Wire.requestFrom(accelSensor, 6);

  int i = 0;
  while(Wire.available())
  {
    values[i] = Wire.read();
    i++;
    }
  Wire.endTransmission();

  x = (((int)values[1]) << 8) | values[0];
  y = (((int)values[3])<< 8) | values[2];
  z = (((int)values[5]) << 8) | values[4];
  sprintf(output, "%d",z-cal);
  Serial.print(output);
  }

 
void setup()
{
  Wire.begin();
  Serial.begin(9600);
}

void loop()
{
  getAccelration(accel_module, 205); // ********accel 0x1D*********//
  Serial.print("    From2: ");
  getAccelration(accel_module2, 28);  //*******accel 0x53********//

  Serial.println(); 
 
  delay(200);
}

Go Up