Hi all,
I need to hook up 8 (but will start with 2) ADXL345 accelerometers. I thought of using the library by loveelectronics:
https://www.loveelectronics.co.uk/Tutorials/12/adxl345-accelerometer-arduino-tutorial
which works fine (after minor updates) for one accelerometer. The output of the sensor in flat position is:
Raw 1: 24 -4 247 Scaled 1: X: 0.09G Y: -0.02G Z: 0.96G
which is correct according to the datasheet.
But i need to hook up some more sensors, so I tried instantiating another accelerometer in my code and pass it the alternate address as given in the datasheet on page 18:
http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf which is 0x53.
But now the output becomes (both sensors in flat position):
Raw 2: 1024 51 -13050 Scaled 2: 3.99G 0.20G -7.98G
Raw 1: 18 4 40 Scaled 1: 0.07G 0.02G 0.16G
Raw 2: 1024 51 -13050 Scaled 2: 3.99G 0.20G -7.98G
Raw 1: 18 2 40 Scaled 1: 0.06G 0.02G 0.16G
Raw 2: 1024 51 -13050 Scaled 2: 3.99G 0.20G -50.90G
Raw 1: 18 4 42 Scaled 1: 0.07G 0.02G 0.16G
Raw 2: 1024 51 -13050 Scaled 2: 3.99G 0.20G -50.91G
It seems that readings from one sensor are multiplied, subtracted or divided by the reading from the other sensor.
the wiring is like this:
i just connected the SDA and SDL pins from both sensors to pin 20 and 21 on the arduino. My guess is that i went wrong here.
Then the code for reference:
// Include the Wire library so we can start using I2C.
#include <Wire.h>
// Include the Love Electronics ADXL345 library so we can use the accelerometer.
#include <ADXL345.h>
// Declare a global instance of the accelerometer.
ADXL345 accel;
ADXL345 accel_2;
// Set up a pin we are going to use to indicate our status using an LED.
int statusPin = 2; // I'm using digital pin 2.
void setup()
{
// Begin by setting up the Serial Port so we can output our results.
Serial.begin(9600);
// Start the I2C Wire library so we can use I2C to talk to the accelerometer.
Wire.begin();
// Ready an LED to indicate our status.
pinMode(statusPin, OUTPUT);
// Create an instance of the accelerometer on the default address (0x1D)
accel = ADXL345();
accel_2 = ADXL345(0x53);
// Check that the accelerometer is infact connected.
if(accel.EnsureConnected())
{
Serial.println("Connected to sensors.");
digitalWrite(statusPin, HIGH); // If we are connected, light our status LED.
}
else
{
Serial.println("Could not connect to ADXL345.");
digitalWrite(statusPin, LOW); // If we are not connected, turn our LED off.
}
// Set the range of the accelerometer to a maximum of 2G.
accel.SetRange(4, true);
accel_2.SetRange(4, true);
// Tell the accelerometer to start taking measurements.
accel.EnableMeasurements();
accel_2.EnableMeasurements();
}
void loop() {
if(accel.IsConnected) // If we are connected to the accelerometer.
{
// Read the raw data from the accelerometer.
AccelerometerRaw raw = accel.ReadRawAxis();
AccelerometerRaw raw_2 = accel_2.ReadRawAxis();
AccelerometerScaled scaled = accel.ReadScaledAxis();
AccelerometerScaled scaled_2 = accel_2.ReadScaledAxis();
Output(raw, raw_2, scaled, scaled_2);
}
}
void Output(AccelerometerRaw raw, AccelerometerRaw raw_2, AccelerometerScaled scaled, AccelerometerScaled scaled_2)
{
// Tell us about the raw values coming from the accelerometer.
Serial.print("Raw 1:\t");
Serial.print(raw.XAxis);
Serial.print(" ");
Serial.print(raw.YAxis);
Serial.print(" ");
Serial.print(raw.ZAxis);
// Tell us about the this data, but scale it into useful units (G).
Serial.print(" \tScaled 1:\t");
Serial.print(scaled.XAxis);
Serial.print("G ");
Serial.print(scaled.YAxis);
Serial.print("G ");
Serial.print(scaled.ZAxis);
Serial.println("G");
// Tell us about the raw values coming from the accelerometer.
Serial.print("Raw 2:\t");
Serial.print(raw_2.XAxis);
Serial.print(" ");
Serial.print(raw_2.YAxis);
Serial.print(" ");
Serial.print(raw_2.ZAxis);
// Tell us about the this data, but scale it into useful units (G).
Serial.print(" \tScaled 2:\t");
Serial.print(scaled_2.XAxis);
Serial.print("G ");
Serial.print(scaled_2.YAxis);
Serial.print("G ");
Serial.print(scaled_2.ZAxis);
Serial.println("G");
}
Any help on getting multiple I2C sensors working is appriciated!
Jorrit