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

Published

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.

3 People Made This Project!

Recommendations

  • Make it Move Contest

    Make it Move Contest
  • Clocks Contest

    Clocks Contest
  • Casting Contest

    Casting Contest
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

what is the meaning of int xAng = map(AcX,minVal,maxVal,-90,90);

29 Comments

1) Why didn't the I/O pins need to be initialized? Are they defaults in wire.h?

2) Are the min/maxVal variables are unique to your calibration but may be different for different boards? How did you derive these numbers?

3) What is the purpose of connecting to the INT pin? I've disconnected it with no adverse affects.

Note that I've added the following to act more like a tilt sensor by giving the offset from 0 tilt in x and y. Z is meaningless if you are doing simple tilt sensor.

if (x>90) {x=(x-360);}

if (y>90) {y=y-360;}

This program was so simple and efficient! Thanks for sharing!!!

I have a problem the sensor only measures 255 and doesnt change ?

2 replies

I have the same problem, any ideas?

Maybe you connected a wire incorrect. Could you check it?

iyi günler

gerçekten güzel çalışma. devreyi şu anda test ediyorum. bende küçük bir hata var ama. 0-360 arası açıyı ölçerken 260 ile 290 arasında farklı değer gösteriyor. benimde 0-360 arası açıyı ölçmem gerekiyor. nerede hata yapıyorum. yardımcı olabilir misiniz acaba?

3 replies

kabloları yanlış bağlamış olma ihtimaliniz var mı acaba bir kontrol edebilir misiniz?

Teşekkürler ilginiz için Burkay Bey. Koddaki minvalue değerini 235 e çekince düzeldi ölçümler. bugün çözebildik hatayı. bir okul projesi için adroid uygulama geliştirmeye çalışıyoruz. kodlarınız baya yardımcı oldu bize. Teşekkürler yardımlarınız için

Yardımcı olabildiysem ne mutlu bana. İyi günler

Hello, where from do you take the minVal and maxVal ?

1 reply

I am sorry, I actually don't remember

Hi! The program uploads fine but the values I get with your code are strange. I get 180 degree changes on both X and Y axises. Any idea why?

Capture.JPG

Hello Burkay ,

Good Project ,

greetings from afyon ;)

1 reply

Hi Burkay,

thanks for this project, I have replace PC by lcd 16x2 with I2C connection (addr = 0x3F).

I have seen, that if I take off 2 wires ( ADO and INT) , there is no change !

However, Angles X and Y are OK, Angle Z doesn't work ok.

Can you explain this problem ? Thanks.

nice project

I have getting data in first time then I upload another sketch

after few time I upload this project nothing data why ?

2 replies

Check that you have your serial monitor set to the correct baud rate. It should be 9600 for this sketch.

I think this problem is coming from your hardware. Can you check your wires?

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

1 reply

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