I2C Comunication

Hi, i’m trying to read data from a IMU (inertial movement unit http://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXL345.pdf),
which commuicates with the I2C protocol to my arduino uno.

I’m using the following code:

#include <Wire.h>

byte a;

void setup()
void loop()
  // z-axis accelerometer
  Wire.beginTransmission(0xA6); // the last bit of the address is write
  Wire.send(0x36);   //arduino seeks the correct register (z-axis value)
  Wire.requestFrom(0xA7,1); //arduino requests the value from the register of z-axis
                                        //the last bit of the address is read
  a=Wire.receive();  //the slave (IMU) sends the data 

I can’t understand why the serial output of arduino is always 0. i made the correct links and i don’t know if the mistake is about the serial.println part or about the I2C protocol part.

Could someone help me??

There is a link to a bildr tutorial on sparkfun's page for the accelerometer. Did you look at that?

You have the incorrect I2C address for the part. The confusing bit (no pun intended) is that the Wire library takes care of the read/write bit automatically for you. This means that you need to drop the low bit and shift the address bits to the right by one position. In your case the address you would need to use is 0x53 (0xA6 or 0xA7 shifted right one position after dropping the low bit).

See http://www.arduino.cc/en/Reference/Wire for more details.

Thank you! you are very helpful

But i changed the code as you suggested, and the output of the z-axis is always zero.

Now the address that I’using is 0x53.

I also write the comand Wire.available() and it shows me that the IMU wants to send to arduino a byte, but the read value is zero, and this is impossible because it should read 9.81m/s^2.

Maybe I use the wrong register, but i checked it many times on the data sheet (linked above) and the z-axis register is 0x36 (for the first byte, the second one needs 0x37).

To call this register I use the comand Wire.send(0x36) is it correct??

I founded the problem! it was a realy silly thing:)

the accelerometer has a power switch inside, and before using it you have to switch it on (inside the "void setup" part of the code).

If some one else has the same problem i suggest this reading: Live Fast - Code Young: ADXL345 accelerometer breakout board + Arduino and Processing

Thanks for the help