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

46,017

30

43

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.

4 People Made This Project!

Recommendations

  • Arduino Contest 2019

    Arduino Contest 2019
  • Tape Contest

    Tape Contest
  • Trash to Treasure

    Trash to Treasure

43 Discussions

0
None
JakeK74

1 year 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 1 year 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 1 year ago

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

0
None
Ducati1299BurkayKirnik

Reply 7 days 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

9 days 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

6 weeks 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 7 weeks ago

Why the val 255? I'm using arduino mega2560

2 answers
0
None
BurkayKirnikegis2

Answer 7 weeks ago

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

0
None
JacobN43BurkayKirnik

Reply 6 weeks 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

2 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

11 months 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 7 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 8 months ago

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

0
None
HarbakshG

8 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 8 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.

0
None
VannarongT

8 months ago on Step 3

Hi Ahmet Burkay KIRNIK! I am newer of mpu can u descrip me:
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);

I really want to understands each. I hope you will

1 reply
0
None
BurkayKirnikVannarongT

Reply 8 months ago

It's been long time since I wrote this code and as I remember,


Hi Ahmet Burkay KIRNIK! I am newer of mpu can u descrip me:
Wire.beginTransmission(MPU_addr);

Wire.write(0x3B);

Wire.endTransmission(false);

Wire.requestFrom(MPU_addr,14,true);

this part sets and I2C connection between mpu and arduino.

AcX=Wire.read()<<8|Wire.read();

AcY=Wire.read()<<8|Wire.read();

AcZ=Wire.read()<<8|Wire.read();

this part reads raw x, y, z values from mpu and saves them to AcX , AcY and AcZ respectively.

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

this part translates raw values to degree. As I remember, I found that calculation part from internet and translated it to Arduino code.

0
None
ElvioS2

1 year ago on Step 3

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

0
None
OraH2

1 year ago

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