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,
  • ADO to GND,
  • 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>

const int MPU_addr=0x68; int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

int minVal=265; int maxVal=402;

double x; double y; double z;

void setup(){ Wire.begin(); Wire.beginTransmission(MPU_addr); Wire.write(0x6B); Wire.write(0); Wire.endTransmission(true); Serial.begin(9600); } void loop(){ Wire.beginTransmission(MPU_addr); Wire.write(0x3B); Wire.endTransmission(false); Wire.requestFrom(MPU_addr,14,true); AcX=Wire.read()<<8|Wire.read(); AcY=Wire.read()<<8|Wire.read(); AcZ=Wire.read()<<8|Wire.read(); int xAng = map(AcX,minVal,maxVal,-90,90); int yAng = map(AcY,minVal,maxVal,-90,90); int zAng = map(AcZ,minVal,maxVal,-90,90);

x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI); y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI); z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);

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.

5 People Made This Project!

Recommendations

  • Mason Jar Speed Challenge

    Mason Jar Speed Challenge
  • Bikes Challenge

    Bikes Challenge
  • Remix Contest

    Remix Contest

53 Discussions

0
168charlie
168charlie

Question 19 days ago on Step 3

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

0
phenoxyethanol
phenoxyethanol

22 days ago

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.

0
greatidea
greatidea

3 months ago

Thanks Ahmet.
This worked fantastically

Greetings from India

0
sarathkumar1006
sarathkumar1006

4 months ago

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

0
egis2
egis2

Question 1 year ago

Why the val 255? I'm using arduino mega2560

0
danielsjmail
danielsjmail

Answer 1 year ago

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.

0
BurkayKirnik
BurkayKirnik

Answer 1 year ago

I didn't understand your question. Can you explain it please?

0
JacobN43
JacobN43

Reply 1 year ago

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 ?

0
JacobN43
JacobN43

1 year ago

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 ?

0
danielsjmail
danielsjmail

Reply 1 year ago

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.

0
Navin Kumar A
Navin Kumar A

1 year ago

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怘⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮怘⸮⸮⸮~⸮ff⸮⸮x`⸮⸮⸮⸮⸮x怘⸮⸮⸮~⸮fff⸮⸮⸮⸮x⸮x怘⸮⸮⸮~⸮f⸮⸮f⸮x⸮⸮x⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮怘⸮⸮⸮~⸮ff⸮⸮xf⸮`⸮⸮⸮⸮⸮~⸮fff⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮~⸮f⸮⸮f⸮x⸮⸮⸮⸮怘⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮怘⸮⸮⸮~⸮ff⸮⸮xf⸮x⸮⸮⸮⸮⸮⸮⸮~⸮fff⸮⸮⸮⸮fx怘⸮⸮⸮~⸮f⸮⸮f⸮x⸮⸮x怘⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮怘⸮⸮⸮~⸮ff⸮⸮xf⸮怘⸮⸮⸮~⸮fff⸮⸮⸮⸮⸮怘⸮⸮⸮~⸮f⸮⸮f⸮x⸮
what may be the problem?

0
Retroker
Retroker

Reply 1 year ago

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

from_google.png
0
TalibbK
TalibbK

Question 1 year ago

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.

1
Enzo308
Enzo308

1 year ago on Step 3

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

0
JakeK74
JakeK74

3 years ago

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

0
BurkayKirnik
BurkayKirnik

Reply 3 years ago

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

0
IshaqA1
IshaqA1

Reply 3 years ago

would be awesome if you do a code for this

0
BurkayKirnik
BurkayKirnik

Reply 3 years ago

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

0
Ducati1299
Ducati1299

Reply 1 year ago

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

0
Ducati1299
Ducati1299

1 year ago

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