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); }
Attachments
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 KINIK
Istanbul/TURKEY
Edit: After 2 years, my English got better so I corrected some mistakes.
62 Comments
1 year ago
Hi, I run correct, but are you using radians?
6 years ago
I have a problem the sensor only measures 255 and doesnt change ?
Reply 6 years ago
Maybe you connected a wire incorrect. Could you check it?
Reply 1 year ago
Maybe solder joints are not correct
Reply 5 years ago
I have the same problem, any ideas?
2 years ago
hello burkay,
I used the same code for esp32 changing the pins to D21 and D22 for SCl and SDA respectively but i am getting 225 as angle for x,y,z can you help me with it ?
3 years 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.
Reply 2 years ago
i am also getting connstant values ,no change so did u solved it plz tell me how can i solve it
Reply 2 years ago
I ended up using a combination of 2 other peoples code and combining them into my own instructable: https://www.instructables.com/Arduino-MPU6050-Based-Digital-Spirit-Level/
5 years ago
I used the same code..
It is showing the value =225 for all three angles.
Can you please solve my problem.
Reply 5 years 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.
Reply 2 years ago
i am getting the same problem on arduino uno please tell me how to solve it
4 years 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 ?
Reply 2 years ago
i get exactly same output please tell me how did u solved it ,i am using nano
Reply 4 years 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.
Question 2 years ago
Bro why does the angles are going to 300 degrees !!!!
2 years ago
Works great. Thanks!
troubleshooting tips: if it doesn't work for you make sure you switch to pins 20-21 if using MEGA, and that there's no short circuit in the connectors.
Question 3 years 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
3 years ago
Thanks Ahmet.
This worked fantastically
Greetings from India
3 years ago
Hi.. you used only accelerometer values, didnt taken gyroscope value.. can you have idea about it?