# How to Measure Angle With MPU-6050(GY-521)

72,178

36

53

## Introduction: How to Measure Angle With MPU-6050(GY-521)

In this Instructable, we will measure angle with an Arduino. We need some cables, an Arduino UNO and GY-521(MPU-6050) in order to measure angle.

## Step 1: Connecting MPU-6050 to Arduino UNO

We need some male-female cables, an Arduino UNO and GY-521(MPU-6050) sensor to measure angle. We have to connect MPU-6050 to Arduino UNO like shown in the picture. So,

• VCC to 5V(MPU-6050 works with 3.3V but GY-521 increases it to 5V.),
• GND to GND,
• SCL to A5,
• SDA to A4,
• INT to digital pin 2.

## Step 2: Code

Here is the code. It uses I2C. I took some parts of code from internet.(I2C part)

//Written by Ahmet Burkay KIRNIK
//Measurement of Angle with MPU-6050(GY-521)

#include<Wire.h>

int minVal=265; int maxVal=402;

double x; double y; double z;

Serial.print("AngleX= "); Serial.println(x);

Serial.print("AngleY= "); Serial.println(y);

Serial.print("AngleZ= "); Serial.println(z); Serial.println("-----------------------------------------"); delay(400); }

## Step 3: Done!

It's done! If it doesn't work or you need help, please leave a comment or send an email. You can find my email adress from comments. By the way, my English is not so good so I apologise for my bad English.

Ahmet Burkay KIRNIK

Istanbul/TURKEY

Edit: After 2 years, my English got better so I corrected some mistakes.

32 2.1K
225 18K
24 2.3K
152 12K

## 53 Discussions

Hi Ahmet
Thanks for this tutorial .... I am new to coding , and have successfully run this code on UNO and on Nodemcu (12E) 1.0 ( later to post on WebServer IoT to create a display )
Have you any idea how to only print the max and min values on repeated angular movements ( ie repeated up and down on the x - axis )
I have used 90 degrees as my neutral position but just for max value I only get the max value printed every 400 millis until the max value is increased
I can't stop printing when the values decrease
When I create an array it doesn't work either say for 20 sample readings it just keeps going!
Do you want my whole code to see what I have done ?

Thanks
Charles

Thanks for taking the time to post this. unfortunately, this doeasn't seem to work for me. I already have a different code working (but with some lag), but was hoping this would help, but all I get is:
-----------------------------------------
AngleX= 354.51
AngleY= 354.68
AngleZ= 225.92
-----------------------------------------
AngleX= 355.23
AngleY= 354.80
AngleZ= 222.54
-----------------------------------------
AngleX= 355.03
AngleY= 354.81
AngleZ= 223.72
-----------------------------------------
Moving the gyro doesn't seem to affect the results.

Thanks Ahmet.
This worked fantastically

Greetings from India

Hi.. you used only accelerometer values, didnt taken gyroscope value.. can you have idea about it?

Why the val 255? I'm using arduino mega2560

I found the solution when I use a Arduino uno this setup works just fine, but this problem arises when I use the Arduino Mega. instead of attaching the SDA and SCL to A5 and A4 attach them to the SDA an SCL pins. they may be a little tricky to spot at first but both the uno and Mega have those pins. so look on your board for SDA and SCL pins on the Mega they are in the location digital pins 20 and 21.

I have the same problem. All connections are correct, but when the code is run the output is :
17:07:10.612 -> -----------------------------------------
17:07:10.984 -> AngleX= 225.00
17:07:11.020 -> AngleY= 225.00
17:07:11.020 -> AngleZ= 225.00
17:07:11.052 -> -----------------------------------------
17:07:11.427 -> AngleX= 225.00
17:07:11.427 -> AngleY= 225.00
17:07:11.460 -> AngleZ= 225.00
17:07:11.460 -> -----------------------------------------

even when the gyro is being moved around.
Any ideas on how to fix this ?

I have the same problem. All connections are correct, but when the code is run the output is :
17:07:10.612 -> -----------------------------------------
17:07:10.984 -> AngleX= 225.00
17:07:11.020 -> AngleY= 225.00
17:07:11.020 -> AngleZ= 225.00
17:07:11.052 -> -----------------------------------------
17:07:11.427 -> AngleX= 225.00
17:07:11.427 -> AngleY= 225.00
17:07:11.460 -> AngleZ= 225.00
17:07:11.460 -> -----------------------------------------

even when the gyro is being moved around.
Any ideas on how to fix this ?

I found the solution when I use a Arduino uno this setup works just fine, but this problem arises when I use the Arduino Mega. instead of attaching the SDA and SCL to A5 and A4 attach them to the SDA an SCL pins. they may be a little tricky to spot at first but both the uno and Mega have those pins. so look on your board for SDA and SCL pins on the Mega they are in the location digital pins 20 and 21.

Sir I've tried the same code but the output is like this
⸮⸮~⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮怘⸮fff⸮⸮⸮⸮f⸮⸮⸮⸮⸮⸮⸮~⸮f⸮⸮f⸮x⸮⸮x怘⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮怘⸮fff⸮⸮⸮⸮f⸮⸮⸮⸮⸮⸮⸮~⸮f⸮⸮f⸮x⸮⸮x怘⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮怘⸮fff⸮⸮⸮⸮f⸮⸮⸮⸮⸮⸮⸮~⸮f⸮⸮f⸮x⸮⸮x怘⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮怘⸮⸮⸮~⸮ff⸮⸮x`⸮⸮⸮⸮⸮x怘⸮⸮⸮~⸮fff⸮⸮⸮⸮x⸮x怘⸮⸮⸮~⸮f⸮⸮f⸮x⸮⸮x⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮怘⸮⸮⸮~⸮ff⸮⸮xf⸮`⸮⸮⸮⸮⸮~⸮fff⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮~⸮f⸮⸮f⸮x⸮⸮⸮⸮怘⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮怘⸮⸮⸮~⸮ff⸮⸮xf⸮x⸮⸮⸮⸮⸮⸮⸮~⸮fff⸮⸮⸮⸮fx怘⸮⸮⸮~⸮f⸮⸮f⸮x⸮⸮x怘⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮怘⸮⸮⸮~⸮ff⸮⸮xf⸮怘⸮⸮⸮~⸮fff⸮⸮⸮⸮⸮怘⸮⸮⸮~⸮f⸮⸮f⸮x⸮
what may be the problem?

Navin, it seems to be a frequency problem.

Try to put the same rate value into Serial.begin(9600) AND the Serial Monitor's baud option (see image). That is, put 9600 in both of them.

Hope it helps

Salam Alaykum!
Thankyou for instructions,
I wanted to ask what are these angles with respect to?
How do we convert them into usable form?
I'm just a newbie and your response will be much appreciated.

Hey well done! This is some of the simplest 6050 code I ve been able to find anywhere

Hi Burkay, is it possible to have an LED light and a Piezo buzzer sound when a certain angle is reached?

Yes, it is! You have to add some lines to code to do this. I can help you if you want.

would be awesome if you do a code for this

contact me on e-mail. My email address: burkaykirnik@gmail.com

Hello,

I realize this is an old thread, however mayby someone is still interested and involved, so here goes,,,,,,,,

I am using arduino uno and mpu6050 and would like to turn an output on when reaching a certain degree of roll, say turn on output 10 when at 90 degrees, turn it off at 70 degrees.
Any help would be great!

Thanks, Doug

Hello,
I am using arduino uno and would like to control an output based on a value of roll.
Example would be turn on output 10 at 90 degrees and turn it off at 70 degrees.
Could you help with this?

Sincerely, Doug