SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By nm1213
#194332
Hello,

I'm making a two-player game, where each player uses a controller with a SparkFun ADXL345 accelerometer to control their character on screen. I have everything working well when connecting a single ADXL345 sensor for one of the players. The sensor is connected using I2C.

I know there is an alternate address mode where I can tie the SDO pin to VCC instead of GND. However, how do I use the SparkFun_ADXL345.h library with multiple sensors? Could you give me an example?

I can see the device being initiated as: ADXL345 adxl = ADXL345();

Then, the raw values are read as such: adel.readAccel (&x, &y, &z);

How do I do the same with a second ADXL345 sensor?

Thank you in advance,

N.
By n1ist
#194335
Briefly looking at the code, it doesn't look easy. They define the I2C address as

#define ADXL345_DEVICE (0x53) // Device Address for ADXL345

To allow multiple devices, you would need to make this a variable and add an accessor function to let you change this at run time.

Alternatively, you could use SPI instead and just give each device a separate chip select.

/mike
By nm1213
#194349
Hello Mike,

I did try using the I2Cdev library for the ADXL345 sensors, and it all works just fine. The two sensors are connected to I2C, one of them had the SDO pin tied to GND (address of 0x53), and the other had the SDO pin tied to VCC (address of 0x1D).

I was then able to define two instances of the sensor as follows:

ADXL345 accel_A = ADXL345(0x53);
ADXL345 accel_B = ADXL345(0x1D);

The sensors are then initialised in setup as follows:

accel_A.initialize();
accel_A.initialize().

Finally, I'm able to read the sensor values as follows:

accel_A.getAcceleration (&ax, &ay, &az);
accel_B.getAcceleration (&bx, &by, &bz);

where I had previously declared the variables as follows:

int16_t ax, ay, az;
int16_t bx, by, bz;

In summary, there is no need to use SPI for multiple ADXL345 sensors. Can be done easily with I2C using this library.

Thanks!

N.