ADXL345 I2C

219 views
Skip to first unread message

Craig C

unread,
Apr 23, 2012, 4:06:15 PM4/23/12
to robocontroller
I am trying to use the AX-12 servos to control a self-leveling gimbal.
I have an ADXL345 accelerometer attached to the base of the gimbal,
and want to use the data from the ADXL to calculate the tip and
rotation needed for the upper portion of the gimbal. I would like to
communicate between the Arbotix and ADXL using I2C, but the code I
have been using in Arduino (1.0) doesn’t seem to work on your board. I
am using Arduino -0023 for writing to the Arbotix, as I thought I saw
somewhere that 1.0 was not supported.
Is there specific method to utilize the dedicated I2C port on the
Arbotix?


I have been using analog pins 4 & 5 on an Uno to use the ADXL. My
initialization and read functions are:

To Initialize:
________________________________________________________
int address=0x53; //i2c adx1345 address, pg10 of manual

void initI2C(){
Wire.beginTransmission(address);
Wire.send(0x2D); //0x2D is the address to the POWER_CTRL
Wire.send(0x08); //0x08 is the address to turn on measurement mode,
get out of sleep mode
Wire.endTransmission();

delay(10);

Wire.beginTransmission(address);
Wire.send(0x31); //0x31 is the data format address
Wire.send(0x08); //set to full resolution
Wire.send(0x01); //set to a +-4g range
Wire.endTransmission();

delay(10);

Wire.beginTransmission(address);
Wire.send(0x2C); //0x2C is the address for the capture rate
Wire.send(0x0A); //set to 100HZ
Wire.endTransmission();

delay(10);
}

-----------------------------------------------------------------------------------------------------------
To read:

-----------------------------------------------------------------------------------------------------------
void readI2C()
{
int i=0; //counter for loop in function
byte buff[6]; //buffer to contain 6 bytes of accleration data, 2
bytes per axis
Wire.beginTransmission(address);
Wire.send(0x32); //address for first byte of x axis acceleration
Wire.endTransmission();
Wire.beginTransmission(address);
Wire.requestFrom(address,6); //tells the accelerometer to ready to
send 6 bytes of data
while(Wire.available()){
buff[i]=Wire.receive(); //read one byte at a time until 6 bytes
are collected
i++;
}
Wire.endTransmission();
if(i==6){
//convert from a 2byte number to a single integer
x=(((int)buff[1]) <<8) | buff[0]; //pulls index 0 and 1 from and
combines them into a single integer
y=(((int)buff[3]) <<8) | buff[2];
z=(((int)buff[5]) <<8) | buff[4];
}
else{
Serial.print("Error:Acceleration Data");
}
}

Thanks for your help!
Craig

Scott

unread,
May 8, 2012, 10:13:51 PM5/8/12
to robocon...@googlegroups.com
I am currently beginning working on using the RM-G146 to control some servo's.  I see some information on the web page saying there is a sonar example using I2C but I can't find any mention of it any where else and no example code.  I did find a example called "I2CFirmata" that has some information in it.  If I make any progress on this I will post it here, if anyone has any suggestions/ideas, I would enjoy seeing them as well.

Thanks,
Scott
Reply all
Reply to author
Forward
0 new messages