Ihave arduinoI have Arduino mega2560 and MPU6050.
I connected to Vcc pin of the gyro to +5V on the mega, the SDA to SDA pin, SDL to SDL pin, AD0 and GND to 2 GND pins.
#include<Wire.h>
const int MPU=0x68; //I2C address of MPU
int GyX,GyY,GyZ;
float pitch=0;
float roll=0;
float yaw=0;
float v_pitch;
float v_roll;
float v_yaw;
float a_pitch;
float a_roll;
float a_yaw;
void setup(){
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B); //power management register 1
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop() {
Wire.beginTransmission(MPU);
Wire.write(0x43);
Wire.endTransmission(false);
Wire.requestFrom(MPU,6,true);
GyX=Wire.read()<<8|Wire.read();
GyY=Wire.read()<<8|Wire.read();
GyZ=Wire.read()<<8|Wire.read();
v_pitch=(GyX/131);
if(v_pitch==-1) {
v_pitch=0;
}
v_roll=(GyY/131);
if(v_roll==1) {
v_roll=0;
}
v_yaw=GyZ/131;
a_pitch=(v_pitch*0.046);
a_roll=(v_roll*0.046);
a_yaw=(v_yaw*0.045);
pitch= pitch + a_pitch; roll= roll + a_roll;
yaw= yaw + a_yaw;
Serial.print(" | pitch = ");
Serial.print(pitch);
Serial.print(" | roll = ");
Serial.print(roll);
Serial.print(" | yaw = ");
Serial.println(yaw);
}
But the Valuevalues I am reading are not angles at all, plus when I move the X axes for 45 degrees, the value is not like that at all.
I don't want to use the pre made library as I want to understand how to read from Gyroscope. Any idea ideas?
