Angle Measurement MPU6050 Arduino Tutorial

Posted on

MPU6050 Arduino Angle Measurement

How to Read Data mpu6050 arduino angle measurement – In the previous tutorial gy 521 arduino, I have written an article about the itg mpu 6050 with 6 examples of basic program code.

However, the data read is raw data from the accelerometer and gyroscope module sensor. So if you want to use the mpu6050 gyroscope to calculate the angle in degrees, it will need an additional line of programming.

In this article I have provided a program that is used to measure angles in degrees mpu6050 imu Arduino.

For example, the following is a screenshot:

Angle Measurement MPU6050 Arduino Tutorial

MPU6050 Light Library

The source code for the program and library used is the MPU6050 light from rfetick, which in this library has been optimized the program code to make it easier for us to read angles in degrees.

If you don’t have the MPU6050 library, please download it at the button below:

Download the MPU6050 Light Library

After downloading, please enter the library into the Arduino IDE by:

  • Click the Sketch menu
  • Then select Include Library
  • Click Add .zib Library
  • Then find and select the downloaded library .zip file
  • Done.

MPU6050 Arduino Code

Now, let’s program. I have provided a program to measure the degrees of the angle below, please copy and paste it into your Arduino IDE, then upload it.

/* Get tilt angles on X and Y, and rotation angle on Z
   Angles are given in degrees

   License: MIT
*/

#include "Wire.h"
#include <MPU6050_light.h>

MPU6050 mpu(Wire);
unsigned long timer = 0;

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

  byte status = mpu.begin();
  Serial.print(F("MPU6050 status: "));
  Serial.println(status);
  while (status != 0) { }

  Serial.println(F("While calculating the offsets value, do not move the MPU6050 sensor"));
  delay(1000);
  // mpu.upsideDownMounting = true; // Comment on this line if MPU6050 is installed backwards
  mpu.calcOffsets();
  Serial.println("Done!\n");
}

void loop() {
  mpu.update();

  if ((millis() - timer) > 10) { // print data every 10ms
    
    Serial.print("X : ");
    Serial.print(mpu.getAngleX());
    Serial.print("90\xc2\xb0");
    
    Serial.print("\tY : ");
    Serial.print(mpu.getAngleY());
    Serial.print("90\xc2\xb0");
    
    Serial.print("\tZ : ");
    Serial.print(mpu.getAngleZ());
    Serial.println("90\xc2\xb0");
    
    timer = millis();
  }
}

After the upload is successful, click serial monitor. If it is open, then wait a moment Arduino will calculate the offset value.

This offset value aims to find the 0 point or commonly referred to as calibration. So during this calendar day, please do not move the mpu6050 gyro sensor.

After the calibration is complete, then you will see the degree data that appears, please move the sensor, the degree value will change.

Congratulations, you have succeeded. Now you can create other projects from this program.

This chip mpu6050 price is cheap. Try visiting the digikey mpu6050 or mpu6050 mouser. If you want to get mpu 6050 module, try visiting mpu6050 amazon.

If you find this program useful, please share this mcu6050 article with other.

Have a nice day.

Similar search terms:

4 comments

  1. I love the program you have provided. Unlike most of the example codes I have tried with the MPU6050, the reported angles have far less noise.
    I am interested in a solution that reports angles over a longer term, for example as a safety tool to monitor if a machine is on a level slope or is tilting.
    When looking at X and Y values over time, they appear relatively stable, however the Z value changes/drifts over a period of hours. It appears to changing at a rate of over 30 degrees per hour.

    Would you also expect the X and Y values to “drift” over time, and is there a way to compensate for that?

  2. Hi,
    Thank you for providing this information, I really appreciate it.
    I haven’t tried this code run for a long period of time. So I can’t give an answer for now.
    I’ll try to fix it and when it’s done I’ll update the code.

Leave a Reply

Your email address will not be published. Required fields are marked *