# L3G4200D Gyro Sensor - Measuring Angle?

I am making an IMU for 6DOF which has L3G4200D 3-Axis Digital Gyroscope.

I've checked so many Blogs, Forums, Application notes and have programmed accordingly to get the proper reading/information from my gyro.

Though I am very near to solve it - a single problem has made me mad since last two nights! I've 16 bit digit - 1 signed bit + 15 bit value

Suppose I want to measure an angle with respect to X-axis at 2000 dps, for that I am doing the following:

while(1)
{
for(g=39;g<42;g++)
{
switch (g)
{
case 40:
l = r;             // LSB part of X_Gyro
break;
case 41:
h = r;            // MSB part of x_Gyro
x = 0;
x = h<<8;
x = x|l;
x = x*70/1000;
UARTprintf("Gyro_X : %5d\n\r",x);
break;
}
}
}


I am getting some values most probably <800 when I move gyro in counter clock direction with respect to X-axis and getting >4000 for the opposite direction!!

I want to have positive angle values while rotating it on counter clockwise direction and negative angle values for the clock wise direction... For that what I should add on my calculations?? [Angles should be in degrees 1 to 180 & (-1) to (-180)]

// in code 0.007 is in mdps which is the value of each LSB of gyro's data

What I should add in could and why I am having >4000 & <800 values right now?

Thank you so much for your valuable time.

• Please add your actual code (copy-paste it) to the question. – Anindo Ghosh Mar 17 '13 at 12:50
• okay, I've edited it. – Jigar4Electronics Mar 17 '13 at 13:10

First, you could use something like this to get your value into a proper signed integer:

uint8_t xl = ...;
uint8_t xh = ...;
int16_t x = *(int8_t *)(&xh);
x *= (1 << 8);
x |= xl;


But x is just a value in Degree per Second. To get your current orientation as angle, you have to integrate this value over time, which will add a lot of drift. To compensate for this, you could then filter it with accelerometer data (PDF!).

• Thank you so much for this... I have edited as you suggest... Now I am getting positive values on counter clock wise and negative at clockwise direction... But surprisingly I am getting in range of +/- 8500 Not constantly but at a higher rate of change in angle it shows the peak of 8000 !! and with normal shake it is +/- 3000 !! in ideal condition I am getting around 40-50 I am using 2000 dps... I've checked it on 250dps, it shows low noise and better stability there... – Jigar4Electronics Mar 17 '13 at 13:30
• Can you tell me what should I do to getting the angles now??? should I take x1 at time t1 and x2 at time t2 and then : X_angle = (x2-x1)/(t2-t1) Am I right??? – Jigar4Electronics Mar 17 '13 at 13:35
• one more question! May I know how this equation will execute? x = *(int8_t *)(&h); – Jigar4Electronics Mar 17 '13 at 13:59
• To get the angle you have to integrate your reading over time. Because the integrate of speed is position. to do this you can just sum up your measurements and at the end divide by the right factor. And yes, you have to continuously read your sensor and at high speed. It's up to you to define what high speed means. It depends on the max angular acceleration possible for your system. – Blup1980 Jul 16 '13 at 5:31