 # Can't make sense of gyro. Just want an angle

Can someone please help me understand where I am going wrong. I am using the above code and have added the timer using millis into the main loop. Its meant to give me the current angle of the gyro but it cycles down very quickly. I expect a bit of drift but this is massive. Heres my output

11670 83.21
11680 83.12
11690 83.02
11700 82.93
11710 82.84
11720 82.75
11730 82.66
11740 82.57
11750 82.48
11760 82.39
11770 82.30
11780 82.21
11790 82.12
11800 82.03
11810 81.94
11820 81.85
11830 81.76
11840 81.67
11850 81.58
11860 81.49
11870 81.40
11880 81.31
11890 81.22

So its drifting down by 1.99 degrees every 220ms. So you can see how this quickly erodes the angle

Heres my code

``````int gyroPin = 4;                 //Gyro is connected to analog pin 4
float gyroVoltage = 3.3;         //Gyro is running at 3.3V
float gyroZeroVoltage = 1.36;    //Gyro is zeroed at 1.36V
float gyroSensitivity = .002;    //Our example gyro is 2mV/deg/sec
float rotationThreshold = 5;     //Minimum deg/sec to keep track of - helps with gyro drifting

float currentAngle = 0;          //Keep track of our current angle
int timer = 0 ;                  //Keep rack of time

void setup() {
Serial.begin (115200);
analogReference(EXTERNAL);
}

void loop()
{
if (millis () - timer >= 0)
{
timer += 10 ;
float gyroRate = (analogRead(gyroPin) * gyroVoltage) / 1023;

//This line finds the voltage offset from sitting still
gyroRate -= gyroZeroVoltage;

//This line divides the voltage we found by the gyro's sensitivity
gyroRate /= gyroSensitivity;

//Ignore the gyro if our angular velocity does not meet our threshold
if (gyroRate >= rotationThreshold || gyroRate <= -rotationThreshold)
{
//This line divides the value by 100 since the timer gives a 10ms loop (1000ms/10ms)
gyroRate /= 100;
currentAngle += gyroRate;
}

//Keep our angle between 0-90 degrees
if (currentAngle < 0)
currentAngle += 90;
else
if (currentAngle > 89)
currentAngle -= 90;

//DEBUG
Serial.print(timer);
Serial.print(" ");
Serial.println(currentAngle);
}
}
``````

I’ve tried loads of examples. all i am after is a simple piece of code which will give me a solid angle in degrees. I only want -90 to +90

Was gyroZeroVoltage = 1.36 determined by experiment or is it a nominal value from the gyro spec? If it's a nominal value you might want to measure the actual voltage. You are currently measuring a rate of -9.05 degrees per second which is 18.1 mV low. Try setting gyroZeroVoltage to 1.3419.

If you want an absolute angle you might want to try a compass instead of rate gyro.