Saturday, June 13, 2020

Arduno Uno + GY-521 module (MPU-6050)

GY-521 module is a breakout board for the MPU-6050 - with I2C interface features a 6-Axis sensors (gyroscope and accelerometer) and temperature sensor.



The MPU-60X0 is the world’s first integrated 6-axis MotionTracking device that combines a 3-axis
gyroscope, 3-axis accelerometer, and a Digital Motion Processor™ (DMP) all in a small 4x4x0.9mm
package. With its dedicated I2C sensor bus....

...Additional features include an embedded temperature sensor...

Read it's details from MPU-6000 and MPU-6050 Product Specification, and Product Page.

Notice on the Product Page, stated that it is in production, but Not Recommended for New Design. Recommended Alternate is ICM-20602 (Interchangeability is not guaranteed.)


Anyway, I have a sample unit of GY-521 on hand. It's a exercise of using Arduino Uno to read GY-521 via I2C, using MPU6050_tockn, using Arduino library for easy communication with MPU6050.

Connect Arduino Uno with GY-521:



- Start Arduino IDE, make sure you select the correct board and port.

- Menu : Sketch > Include Library > Manage Library... to open Library Manager.

- Search MPU6050 to install MPU6050_tockn library.


- Menu : File > Examples > MPU6050_tockn > GetAllData



- Upload to Uno, and open Serial Monitor. To check the output.


Now we modify a little bit, such that we can see the result more easy visually.


#include <MPU6050_tockn.h>
#include <Wire.h>

MPU6050 mpu6050(Wire);

long timer = 0;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);
  Serial.println();
}

void loop() {
  mpu6050.update();

  if(millis() - timer > 200){

    Serial.print(mpu6050.getAngleX());
    Serial.print(",");
    Serial.print(mpu6050.getAngleY());
    Serial.print(",");
    Serial.println(mpu6050.getAngleZ());
    timer = millis();
    
  }

}



- Save As another file, Verify and Upload.

- Open Serial Plotter instead of Serial Monitor


- Now you can rotate the module and check the reading graphically.


DONE

No comments:

Post a Comment