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

51,244

30

47

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

  • Beauty Tips Contest

    Beauty Tips Contest
  • Growing Beyond Earth Maker Contest

    Growing Beyond Earth Maker Contest
  • Backyard Contest

    Backyard Contest

47 Discussions

0
None
Navin Kumar A

25 days 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?

1 reply
0
None
RetrokerNavin Kumar A

Reply 15 days 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
None
TalibbK

Question 5 weeks 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.

0
None
Enzo308

7 weeks ago on Step 3

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

0
None
JakeK74

2 years ago

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

4 replies
0
None
BurkayKirnikJakeK74

Reply 2 years ago

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

0
None
BurkayKirnikIshaqA1

Reply 2 years ago

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

0
None
Ducati1299BurkayKirnik

Reply 3 months 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
None
Ducati1299

3 months 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

0
None
JacobN43

4 months 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
None
egis2

Question 4 months ago

Why the val 255? I'm using arduino mega2560

2 answers
0
None
BurkayKirnikegis2

Answer 4 months ago

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

0
None
JacobN43BurkayKirnik

Reply 4 months 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
None
MaximeP34

5 months ago

Thanks a lot for the tutorial. Simple to use and straight to the point. I'll now be able to focus on tinkering with the readings from the gyroscope.

1
None
gowfster

1 year ago

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;}

1 reply
0
None
arnabdutta01gowfster

Reply 10 months ago

1) See LowPowerLab/SI7021/issues/4

2) These are from calibration and differ from board to board.

3) Quoting the MPU-6050 datasheet:

“ The data within the accelerometer sensors’ internal register set is always updated at the Sample Rate. Meanwhile, the user-facing read register set duplicates the internal register set’s data values whenever the serial interface is idle. This guarantees that a burst read of sensor registers will read measurements from the same sampling instant. Note that if burst reads are not used, the user is responsible for ensuring a set of single byte reads correspond to a single sampling instant by checking the Data Ready interrupt. ”

If you don't use the INT pin, then you have no Data Ready interrupts.

0
None
omkar.shrimali.ece16

Question 11 months ago

When I am changing the angle it's not showing any change in the output ?

0
None
HarbakshG

11 months ago

I used the same code..

It is showing the value =225 for all three angles.

Can you please solve my problem.

1 reply
0
None
BurkayKirnikHarbakshG

Reply 11 months ago

It's probably because of incorrect wire connections. Could you please check them? BTW, I suggest you to use the code that I attached there because some things may be changed.