How to connect an Arduino to the ADXL345 board

Hello,
we are beginners. =(
we wonder how to connect an Arduino to the ADXL345 board http://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXL345.pdf
we would appreciate it very much if you could offer any help~!

here is the Code?

#define Register_ID 0
#define Register_2D 0x2D
#define Register_X0 0x32
#define Register_X1 0x33
#define Register_Y0 0x34
#define Register_Y1 0x35
#define Register_Z0 0x36
#define Register_Z1 0x37
#include <Wire.h> 
int ADXAddress = 0xA7 >> 1;  // the default 7-bit slave address
int reading = 0; 
int val=0;
int X0,X1,X_out;
int Y0,Y1,Y_out;
int Z1,Z0,Z_out;
double Xg,Yg,Zg;

void setup() 
{ 
  Wire.begin();      
  Serial.begin(19200);     
  delay(100);
  // enable to measute g data
  Wire.beginTransmission(ADXAddress); 
  Wire.write(Register_2D);
  Wire.write(8);                //measuring enable
  Wire.endTransmission();     // stop transmitting 
} 
void loop() 
{ 
  //--------------X
  Wire.beginTransmission(ADXAddress); // transmit to device 
  Wire.write(Register_X0);
  Wire.write(Register_X1); 
  Wire.endTransmission(); 
  Wire.requestFrom(ADXAddress,2);  
  if(Wire.available()<=2)    
  { 
    X0 = Wire.read();
    X1 = Wire.read();  
    X1=X1<<8;
    X_out=X0+X1;    
  }
//------------------Y
  Wire.beginTransmission(ADXAddress); // transmit to device 
  Wire.write(Register_Y0);
  Wire.write(Register_Y1); 
  Wire.endTransmission(); 
  Wire.requestFrom(ADXAddress,2);  
  if(Wire.available()<=2)    
  { 
    Y0 = Wire.read();
    Y1 = Wire.read();  
    Y1=Y1<<8;
    Y_out=Y0+Y1;
  }  
//------------------Z
  Wire.beginTransmission(ADXAddress); // transmit to device 
  Wire.write(Register_Z0);
  Wire.write(Register_Z1); 
  Wire.endTransmission(); 
  Wire.requestFrom(ADXAddress,2);  
  if(Wire.available()<=2)    
  { 
    Z0 = Wire.read();
    Z1 = Wire.read();  
    Z1=Z1<<8;
    Z_out=Z0+Z1;
  }

  Xg=X_out/256.0;
  Yg=Y_out/256.0;
  Zg=Z_out/256.0;
  Serial.print("X= ");
  Serial.print(Xg);
  Serial.print("       ");
  Serial.print("Y= ");
  Serial.print(Yg);
  Serial.print("       ");
  Serial.print("Z= ");
  Serial.print(Zg);
  Serial.println("  ");
  delay(200);

}

The datasheet shows max voltage ratings of 3.6V so you will need level shifting.

Do you have a raw chip or some kind of breakout board?

Looks like you have chosen the I2C (Wire library) interface. Wire according to Figure 8 on the datasheet.

Thank u for ur reply,
we have a breakout board.
to be frank,we don't understand the Figure 8 on the datasheet,especially the ALT ADDRESS

here is our connection and data:
GND——GND
VCC——3V
CS——3V
SDA——A4
SCL——A5
ALT ADDRESS ?

X= -1.00 Y= -1.00 Z= -1.00
X= -1.00 Y= -1.00 Z= -1.00
X= -1.00 Y= -1.00 Z= -1.00
X= -1.00 Y= -1.00 Z= -1.00
X= -1.00 Y= -1.00 Z= -1.00
X= -1.00 Y= -1.00 Z= -1.00
X= -1.00 Y= -1.00 Z= -1.00
X= -1.00 Y= -1.00 Z= -1.00
X= -1.00 Y= -1.00 Z= -1.00

we have a breakout board.

What breakout board do you have?

to be frank,we don't understand the Figure 8 on the datasheet,especially the ALT ADDRESS

If you want to have two of the same deice on the I2C bus they need to have different addresses.

  Wire.beginTransmission(ADXAddress); // transmit to device 
  Wire.write(Register_X0);
  Wire.write(Register_X1);   ///// You can't request more than one register at a time
  Wire.endTransmission(); 
  Wire.requestFrom(ADXAddress,2);  
 if(Wire.available()<=2)       ///// If Wire.avaialble() < 2 you can't then read two bytes
  { 
    X0 = Wire.read();
    X1 = Wire.read();  
    X1=X1<<8;
    X_out=X0+X1;    
  }

Try something like this instead:

  Wire.beginTransmission(ADXAddress); // transmit to device 
  Wire.write(Register_X0);
  Wire.endTransmission(); 
  Wire.requestFrom(ADXAddress,1);  
 if(Wire.available())
  { 
    X0 = Wire.read();
    Wire.beginTransmission(ADXAddress); // transmit to device 
    Wire.write(Register_X1);
    Wire.endTransmission(); 
    Wire.requestFrom(ADXAddress,1); 
    if (Wire.available())
        {
        X1 = Wire.read();  
        X_out=X0+X1<<8;    
        }
    }

we've taken ur advice and used two 10k pull-up resistors
we finally got correct data
Thank u for ur help~! :slight_smile: