0

I'm working with MPU-6050 with Arduino Uno.

I copied the code below from Arduino Playground - MPU6050.

Code :

  // MPU-6050 Short Example Sketch
  // By Arduino User JohnChi
  // August 17, 2014
  // Public Domain
  #include<Wire.h>
  const int MPU_addr = 0x68; // I2C address of the MPU-6050
  int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
  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, 14, true); // request a total of 14 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)
    Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
    GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
    GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
    GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
    Serial.print("AcX = "); Serial.print(AcX);
    Serial.print(" | AcY = "); Serial.print(AcY);
    Serial.print(" | AcZ = "); Serial.print(AcZ);
    Serial.print(" | Tmp = "); Serial.print(Tmp / 340.00 + 36.53); //equation for temperature in degrees C from datasheet
    Serial.print(" | GyX = "); Serial.print(GyX);
    Serial.print(" | GyY = "); Serial.print(GyY);
    Serial.print(" | GyZ = "); Serial.println(GyZ);
    delay(333);
  }

Firstly, I want to know every details about this code, AcX, AcY, AcZ, GyX, GyY, GyZ what all these mean ?

It will be nice with some visual representation.

Secondly, If I change the delay to 500 from 333, will it cause a problem ?

This type of stupid question because, I can't find any relatable figure with 333 except 1000/3

  • Ac{XYZ} refers to the acceleration reported by the sensor in x, y, and z direction. Gy{XYZ} refers to the rotation (per second) reported by the sensor along the x, y, and z axis. – Christian Lindig Mar 31 at 20:09
  • The 333ms delay is just a number, you can make it 2 or 20000 or whatever. – Jot Mar 31 at 21:06
  • I can't find any relatable figure with 333 .... think about it a little bit ..... what is the purpose of loop()? ..... what happens if you put a 333ms delay in loop()? – jsotola Mar 31 at 21:27
  • thanks everyone ... – Maifee Ul Asad Apr 1 at 2:49
  • How to filter value in real time ? Can anyone tell me ? – Maifee Ul Asad Apr 1 at 2:50
1
AcX = Accelerometer data in X axis in floating point. 
AcY = Accelerometer data in Y axis in floating point. 
AcZ = Accelerometer data in Z axis in floating point. 
GyX = Gyroscope data in X axis in floating point. 
GyY = Gyroscope data in X axis in floating point.
GyZ = Gyroscope data in X axis in floating point

.

Secondly study regarding I2C communication and MPU 6050 datasheet.

Third you have any value for delay as it is how fast you want to fetch data from all axis from IMU.

  • Thanks, I was trying to move and rotate an object in Unity using Arduino as input . Now I have this , rotation is perfect but, position isn't . Maybe you can help me with that. – Maifee Ul Asad Apr 22 at 8:46
1

I think that AcX, AcY, AcZ, GyX, GyY, GyZ are raw acceleration and gyroscopic values recieved from mpu6050 in the corresponding directions. in order to recieve them a procedure is followed.

 Wire.read();

means two registers are read and stored in the same variable

   accelerometer_x = Wire.read()<<8 | Wire.read(); 

reads registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L) that is , it deals with bitwise operation to shift the values 8 bits left (multiplying it by 256). This enables addition ("|" or "ored") of two datas in order to continuously output the accelerometer data !! Without this you would not be able to see the increasing and decreasing sloping values . Further, the print just displays the instantaneous values on screen.

If you want to go in deapth of the code, you can visit this site for line to line description !!

https://www.mschoeffler.de/2017/12/09/example-application-gy-521-module-mpu-6050-breakout-board-and-arduino-uno/

 delay(500);

Totally depends on your project, i don't see any forecoming issues if you will make 333 go 500 !! It will just stablize the output on screen there for a bit longer (for .5 seconds)

  • How to process these raw value to actual value ? – Maifee Ul Asad Apr 1 at 2:41
  • @MaifeeUlAsad You can always use the mpu6050 library by jeffrowberg !! He has done an excellent job in reverse engineering the mpu6050 aspects and put them in perspective. You can use mpu6050_dmp6 sketch to get real processed values. – Hrithik Baishakhiya Apr 1 at 5:02
  • you can also include the library and use some functions like mpu.getmotion6(&ax, &ay,&az,&gx,&gy,&gz,) to get these precise values. – Hrithik Baishakhiya Apr 1 at 5:06
  • Thanks, I was trying to move and rotate an object in Unity using Arduino as input . Now I have this , rotation is perfect but, position isn't . Maybe you can help me with that. – Maifee Ul Asad Apr 22 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.