Help with MPU-6050 for school

Hello everyone,

I need some help with my school project and I did not find the answer on Google.

I need a 3-axis Accelerometer for my school project and the only one I was able to order was the MPU 6050. The mpu 6050 is the most complicated accelerometer from the list that they recommended:

  • LilyPad ADXL335 (I also ordered this one, but I still have no message that it is on his way)
  • Adafruit ADXL335
  • mpu-6050 (This is the only one I could order)

The only data that I need from the mpu 6050, at the moment, is the x,y,z data from the accelerometer.

Does someone know if there is a code that is (easy) implementable in an already exciting code? I had a look at the code from the I2Cdev device library but it is too complicated for me to understand and implement in another code.

The preference of this code is to give as output the x,y,z data's in separate floats so that I can easily implement it into the code that I got from school.

Can someone help me with this?

If you need more information, just let me know.

With kind regards,

Stan Wijnen

Or does someone know how I can change the I2Cdev example code to make it easily implementable?

you can use a Sketch like this to get the acceleration Values in X,Y and Z Axis:

#include <Wire.h>

int16_t accX, accY, accZ;

uint8_t i2cData[6];

void setup() {
  Serial.begin(115200);
  Wire.begin();

  i2cData[0] = 7;
  i2cData[1] = 0x00;
  i2cData[2] = 0x00;
  i2cData[3] = 0x00;
  while (i2cWrite(0x19, i2cData, 4, false));
  while (i2cWrite(0x6B, 0x01, true));

  while (i2cRead(0x75, i2cData, 1));
  if (i2cData[0] != 0x68) {
    Serial.print(F("Error"));
    while (1);
  }

  delay(100);
}

void loop() {

  while (i2cRead(0x3B, i2cData, 6));
  accX = ((i2cData[0] << 8) | i2cData[1]);
  accY = ((i2cData[2] << 8) | i2cData[3]);
  accZ = ((i2cData[4] << 8) | i2cData[5]);

  Serial.print(accX); Serial.print("\t");
  Serial.print(accY); Serial.print("\t");
  Serial.print(accZ); Serial.print("\t");

  Serial.print("\r\n");
  delay(50);
}

@dustin_the_wind, the i2cRead() and i2cWrite() are not part of the Arduino Wire library.

@swijnen, this is the shortest way to get the data: Arduino Playground - MPU-6050.

Even shorter:

#include<Wire.h>
const int MPU_addr = 0x68; // I2C address of the MPU-6050
int16_t AcX, AcY, AcZ;
void setup() {
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(9600);
}
void loop() {
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr, 6, true); // request 6 registers
  AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Serial.print("Acc = "); Serial.print(AcX);
  Serial.print(", "); Serial.print(AcY);
  Serial.print(", "); Serial.println(AcZ);
  delay(500);
}