Go Down

Topic: I2C Comunication (Read 480 times) previous topic - next topic

andre2412

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()
{
  Serial.begin(9600);
  Wire.begin();
}
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.endTransmission();
  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
  Serial.println("z-axis");
  Serial.println(a,BIN);
}


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??

PaulS

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

vodkatonic

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.




andre2412

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??

andre2412

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: http://codeyoung.blogspot.com/2009/11/adxl345-accelerometer-breakout-board.html

Thanks for the help

Go Up