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

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

I will measure angle with an Arduino. We need some cables, an Arduino UNO and GY-521(MPU-6050) for measure angle.

Step 1: Connecting MPU-6050 to Arduino UNO

Picture of Connecting MPU-6050 to Arduino UNO

You need some male-female cables, an Arduino UNO and GY-521(MPU-6050) for measure angle. You have to connect MPU-6050 to Arduino UNO like 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

Picture of Code

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

//Written by Ahmet Burkay KIRNIK
//TR_CapaFenLisesi //Measure Angle with a 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);<<8|;<<8|;<<8|; 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!

Picture of Done!

It has finished! If it doesn't work, please leave a comment. By the way, my English is not so good, sorry for my bad English.

Ahmet Burkay KIRNIK



jean--pierre (author)2017-11-06

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.

aporizga (author)2017-08-04

nice project

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

after few time I upload this project nothing data why ?

agoodchild1 (author)aporizga2017-10-15

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

BurkayKirnik (author)aporizga2017-08-04

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

JakeK74 (author)2017-07-01

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

BurkayKirnik (author)JakeK742017-07-03

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

IshaqA1 (author)BurkayKirnik2017-07-09

would be awesome if you do a code for this

BurkayKirnik (author)IshaqA12017-07-09

contact me on e-mail. My email address:

gabriel.lim_2015 (author)2016-10-03

HI, i got an error in compiling for your code, what should i do now? thanks

at the line #include

replace with # include<wire.h>

I edited the code and added wire.h. ty


OraH2 (author)2017-04-28

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

BurkayKirnik (author)OraH22017-07-03

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

saadmaan made it! (author)2017-06-08

Thanks for getting me started with MPU6050. Works great.

mehdi kiani (author)2017-03-30

very good

FlavienB (author)2017-02-14

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

MartinK189 made it! (author)2017-02-10

I wanted to test my MPU-6050. Thanks!

BillD120 (author)2016-12-02

Thnx Mate! Great project to get me started with the MPU-6050(GY-521), worked perfect first time!



PS. Your English is great, no need to apologies!

DIY Hacks and How Tos (author)2016-06-19

Cool project.I need to get some of these sensors and try them out.

About This Instructable



More by BurkayKirnik:How to Measure Angle With  MPU-6050(GY-521)
Add instructable to: