Arduino leonardo with MPU6050_DMP

The following is raw code to get the data vectors, it originally came from either a library and/or sketch I forget , I tore it apart and unlike most people READ THE DATA SHEET.

I tried using the interrupt function also but couldn't use it cause my wiring didn't allow me access to a interrupt pin. Instead set up a timer to poll the chip every X amount of time for new data.

It reads the vectors as a string of bytes, then you just chuck the bytes into the correct variable.

// variables
uint8_t IMUAddress = 0x68;

//setup
i2cWrite(IMUAddress,0x6B,0x00); // disable sleep mode

if(i2cRead(IMUAddress,0x75,1)[0] != 0x68) // confirms that it is IC2 address 68
while(1);

if (i2cRead(IMUAddress,0x19,1)[0] != 0x07)
i2cWrite(IMUAddress,0x19,0x07); //sample rate 1KHz 8k/(7+1) = 1000 Hz

if (i2cRead(IMUAddress,0x1A,1)[0] != 0x05)
i2cWrite(IMUAddress,0x1A,0x05); // see page 13 of data sheet, Register 26/1A Hex CONFIG

if (i2cRead(IMUAddress,0x1B,1)[0] != 0x18)
i2cWrite(IMUAddress,0x1B,0x18); // Register 27 Gyroscope Configuration GYRO_CONFIG +- 2000

if (i2cRead(IMUAddress,0x1C,1)[0] != 0x18)
i2cWrite(IMUAddress,0x1C,0x18);

//some subroutine in the program
uint8_t* data = i2cRead(IMUAddress,0x3B,14); // this data is stored in register 3B(Hex)
accX = ((data[0] << 8) | (data[1]));
accY = ((data[2] << 8) | (data[3]));
accZ = ((data[4] << 8) | data[5]);
temp = ((data[6] << 8) | data[7]);
gyroY = ((data[8] << 8) | data[9]);
gyroX = ((data[10] << 8) | data[11]);
gyroZ = ((data[12] << 8) | data[13]);
//// Calculate the angls based on the different sensors and algorithm /////
accYangle = ((double)atan2(accY,accZ)+PI)*RAD_TO_DEG; // angle calculated soley from the accelometer, no drift but unstable
accXangle = ((double)atan2(accX,accZ)+PI)*RAD_TO_DEG;
temp = (temp+512)/340 + 35;
//////// calculate gyrorate //////////////////////////////////////////
gyroXrate = -(double)(gyroX/16.4); // 16.4 is from the spec sheet for proper calibration at 2000deg/s on gyros
gyroYrate = -(double)(gyroY/16.4);
gyroZrate = (double)(gyroZ/16.4);

//end of program subroutines to be called on data read/writes to 6050
void i2cWrite(uint8_t Address, uint8_t registerAddress, uint8_t data){

Wire.beginTransmission(Address); // address 68
Wire.write(registerAddress); // the address
Wire.write(data);
Wire.endTransmission(); // Send stop

}
uint8_t* i2cRead(uint8_t Address, uint8_t registerAddress, uint8_t nbytes) {
uint8_t data[nbytes];

Wire.beginTransmission(Address);
Wire.write(registerAddress);
Wire.endTransmission(false); // Don't release the bus
Wire.requestFrom(Address, nbytes); // Send a repeated start and then release the bus after reading
for(uint8_t i = 0; i < nbytes; i++)
data = Wire.read();

  • return data;*
    }[/quote]