I2C 6 DOF IMU

Hello everyone,

I’m new to the forums so I wasn’t sure which section to put this under, but I think I have the hardware hooked up correctly so it is a matter of interfacing through I2C. I bought this 6 DOF IMU http://www.sparkfun.com/products/10121, and I hooked it up as per http://tronixstuff.wordpress.com/2010/10/20/tutorial-arduino-and-the-i2c-bus/, I tried to follow the I2C tutorial to the best of my ability, and used the I2C addresses posted on the sparkfun website (in the schematic) but the when I read the 0xA7 address I do not get any data. This is my code:

#include “Wire.h”

#define gyroWrite 0xD0
#define gyroRead 0xD1
#define accelWrite 0xA6
#define accelRead 0xA7

#define accelX0 0x32
#define accelX1 0x33
#define accelY0 0x34
#define accelY1 0x35
#define accelZ0 0x36
#define accelZ1 0x37

void setup () {
Serial.begin(9600);
Wire.begin();
}

void loop () {
byte accelXYZ[6];
Wire.beginTransmission (accelRead);
Wire.send (accelX0);
Wire.endTransmission();
Wire.requestFrom(accelRead, 6);
for (int ii=0; ii < 6; ii++) {
accelXYZ[ii] = Wire.receive();
Serial.print(“accelXYZ[”);
Serial.print(ii);
Serial.print("]: ");
Serial.print(accelXYZ[ii]);
Serial.println();
}
Serial.println();
delay(1000);
}

Using this hardware is a bit more complicated than you think i use this sensors a lot, you can find a lot of well made tutorials, google fabio varesano or starlino for example.
Btw for the adxl345 you have to:

//Define byte to read:
#define ACC (0x53)            
#define A_TO_READ (6)   

// Init the sensor
writeTo(ACC, 0x2D, 0);      
writeTo(ACC, 0x2D, 16);
writeTo(ACC, 0x2D, 8);

//Use functions to get data out (following functions were made i think by fabio varesano)  

void getAccelerometerData(int * result) {
  int regAddress = 0x32;                                   //first axis-acceleration-data register on the ADXL345
  byte buff[A_TO_READ];
  
 

readFrom(ACC, regAddress, A_TO_READ, buff); //read the acceleration data from the ADXL345
  
  //each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
  //thus we are converting both bytes in to one int
  result[0] = (((int)buff[1]) << 8) | buff[0];   
  result[1] = (((int)buff[3])<< 8) | buff[2];
  result[2] = (((int)buff[5]) << 8) | buff[4];
}

//reads num bytes starting from address register on device in to buff array
void readFrom(int DEVICE, byte address, int num, byte buff[]) {
  Wire.beginTransmission(DEVICE); //start transmission to device 
  Wire.send(address);        //sends address to read from
  Wire.endTransmission(); //end transmission
  
  Wire.beginTransmission(DEVICE); //start transmission to ACC
  Wire.requestFrom(DEVICE, num);    // request 6 bytes from ACC
  
  int i = 0;
  while(Wire.available())    //ACC may send less than requested (abnormal)
  { 
    buff[i] = Wire.receive(); // receive a byte
    i++;
  }
  Wire.endTransmission(); //end transmission
}




//Writes val to address register on device
void writeTo(int DEVICE, byte address, byte val) {
   Wire.beginTransmission(DEVICE); //start transmission to device 
   Wire.send(address);        // send register address
   Wire.send(val);        // send value to write
   Wire.endTransmission(); //end transmission
}

There are libraries for those sensors you can use if you want to simplify your work, remember the first rule of coding is not to reinvent the weel!
Cheers

Okay cool, I figured it out. In retrospect the information was all there, I just didn’t know what to look for and how to use it. One thing I can’t figure out though is how to appropriately scale the output data for gyro and the temperature sensor. Acceleration was easy - 9.8m/s2 from earth’s axis always ended up being a 256 integer.

My code:

#include "Wire.h"

#define gyroDat 0x1B
#define gyroDev 0x69
#define gyroPwr 0x2D
byte    gyroTemp_B[2];
byte    gyroMNO_B[6];
float   gyroTemp_f;
float   gyroMNO_f[3];
int     gyroTemp_i;
int     gyroMNO_i[3];

#define accelDat 0x32
#define accelDev 0x53
#define accelPwr 0x2D
byte    accelXYZ_B[6];
float   accelXYZ_f[3];
int     accelXYZ_i[3];

void setup () {
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission (accelDev);
  Wire.send (accelPwr);
  Wire.send (0);
  Wire.endTransmission();
  Wire.begin();
  Wire.beginTransmission (accelDev);
  Wire.send (accelPwr);
  Wire.send (16);
  Wire.endTransmission();
  Wire.begin();
  Wire.beginTransmission (accelDev);
  Wire.send (accelPwr);
  Wire.send (8);
  Wire.endTransmission();
}

void loop () {
  Wire.beginTransmission (accelDev);
  Wire.send (accelDat);
  Wire.endTransmission();
  Wire.beginTransmission(accelDev);
  Wire.requestFrom(accelDev, 6);
  for (int ii=0; ii < 6; ii++) {
    accelXYZ_B[ii] = Wire.receive();
  }
  Wire.endTransmission();
  
  Wire.beginTransmission (gyroDev);
  Wire.send (gyroDat);
  Wire.endTransmission();
  Wire.beginTransmission(gyroDev);
  Wire.requestFrom(gyroDev, 8);
  for (int ii=0; ii < 8; ii++) {
    if (ii<2) {
      gyroTemp_B[ii] = Wire.receive();
    }
    else {
      gyroMNO_B[ii] = Wire.receive();
    }
  }
  Wire.endTransmission();
    
  accelXYZ_i[0] = (((int)accelXYZ_B[1]) << 8) | accelXYZ_B[0];   
  accelXYZ_i[1] = (((int)accelXYZ_B[3])<< 8) | accelXYZ_B[2];
  accelXYZ_i[2] = (((int)accelXYZ_B[5]) << 8) | accelXYZ_B[4];
    
  gyroTemp_i = (((int)accelXYZ_B[1]) << 8) | accelXYZ_B[0];   
  
  gyroMNO_i[0] = (((int)accelXYZ_B[3])<< 8) | accelXYZ_B[2];
  gyroMNO_i[1] = (((int)accelXYZ_B[5]) << 8) | accelXYZ_B[4];
  gyroMNO_i[2] = (((int)accelXYZ_B[7]) << 8) | accelXYZ_B[6];
  
  accelXYZ_f[0] = -1*accelXYZ_i[0]*9.80665/256;
  accelXYZ_f[1] = -1*accelXYZ_i[1]*9.80665/256;
  accelXYZ_f[2] = accelXYZ_i[2]*9.80665/256;
  
  gyroTemp_f = (gyroTemp_i+13200.0)/280;
  
  gyroMNO_f[0] = gyroMNO_i[0]/14.375;
  gyroMNO_f[1] = gyroMNO_i[1]/14.375;
  gyroMNO_f[2] = gyroMNO_i[2]/14.375;
  
  Serial.print("X: ");
  Serial.print(accelXYZ_f[0]);
  Serial.print("  Y: ");
  Serial.print(accelXYZ_f[1]);
  Serial.print("  Z: ");
  Serial.print(accelXYZ_f[2]);
  Serial.print("  T: ");
  Serial.print(gyroTemp_f);
  Serial.print("  M: ");
  Serial.print(gyroMNO_f[1]);
  Serial.print("  N: ");
  Serial.print(gyroMNO_f[2]);
  Serial.print("  O: ");
  Serial.print(gyroMNO_f[0]);
  Serial.println();
  delay(0);
}

my output, sitting lopsided on my table:

X: 7.16  Y: 3.60  Z: 5.29  T: 46.47  M: 9.60  N: 1050.64  O: -6.54
X: 7.24  Y: 3.64  Z: 5.36  T: 46.47  M: 9.74  N: 1050.64  O: -6.61
X: 7.16  Y: 3.56  Z: 5.25  T: 46.47  M: 9.53  N: -1449.25  O: -6.47
X: 7.16  Y: 3.56  Z: 5.36  T: 46.47  M: 9.74  N: 1050.64  O: -6.47
X: 7.24  Y: 3.64  Z: 5.32  T: 46.47  M: 9.67  N: 1050.64  O: -6.61
X: 7.16  Y: 3.60  Z: 5.32  T: 46.47  M: 9.67  N: -1449.25  O: -6.54
X: 7.16  Y: 3.60  Z: 5.17  T: 46.47  M: 9.39  N: 1050.64  O: -6.54
X: 7.16  Y: 3.68  Z: 5.25  T: 46.47  M: 9.53  N: 1050.64  O: -6.68
X: 7.20  Y: 3.56  Z: 5.29  T: 46.47  M: 9.60  N: 1050.64  O: -6.47
X: 7.16  Y: 3.64  Z: 5.32  T: 46.47  M: 9.67  N: 2080.21  O: -6.61
X: 7.20  Y: 3.56  Z: 5.36  T: 46.47  M: 9.74  N: 1050.64  O: -6.47
X: 7.16  Y: 3.52  Z: 5.25  T: 46.47  M: 9.53  N: 2080.21  O: -6.40
X: 7.24  Y: 3.64  Z: 5.32  T: 46.47  M: 9.67  N: 1050.64  O: -6.61
X: 7.20  Y: 3.60  Z: 5.32  T: 46.47  M: 9.67  N: -1449.25  O: -6.54
X: 7.16  Y: 3.60  Z: 5.29  T: 46.47  M: 9.60  N: 2080.21  O: -6.54
X: 7.16  Y: 3.72  Z: 5.29  T: 46.47  M: 9.60  N: 1050.64  O: -6.75
X: 7.13  Y: 3.60  Z: 5.29  T: 46.48  M: 9.60  N: 1050.64  O: -6.54
X: 7.16  Y: 3.56  Z: 5.21  T: 46.47  M: 9.46  N: 21.08  O: -6.47
X: 7.20  Y: 3.68  Z: 5.32  T: 46.47  M: 9.67  N: 1050.64  O: -6.68
X: 7.13  Y: 3.60  Z: 5.29  T: 46.48  M: 9.60  N: 2080.21  O: -6.54

well, i guess you would have to better understand how these sensor work. I suggest you to read this:

http://www.starlino.com/imu_guide.html

hey mishaparem ,, please if u ahvent mind to explain ur code :)

wedontplay: well, i guess you would have to better understand how these sensor work. I suggest you to read this:

http://www.starlino.com/imu_guide.html

Comments like this add 0 value.