Drift in gyroscope

Hi,

I want eliminate the drift in this sketch. Someone can help me

Thanks!

#include <Wire.h> 
#include <Adafruit_L3GD20.h>

  Adafruit_L3GD20 gyro;

float time = 0;
float oldTime = 0;
int gyroVal;
float angle = 0;
float anglex = 0;

void setup(){
  
  Serial.begin(9600);
  // Try to initialise and warn if we couldn't detect the chip
   if (!gyro.begin(gyro.L3DS20_RANGE_250DPS))
  //if (!gyro.begin(gyro.L3DS20_RANGE_500DPS))
  //if (!gyro.begin(gyro.L3DS20_RANGE_2000DPS))
  {
    Serial.println("Oops ... unable to initialize the L3GD20. Check your wiring!");
    while (1);
  }
}

void loop(){
 gyro.read();
 //store old reading time to get the difference in the times
 oldTime = time;
 //how long the program has been running (in milliseconds - 4000 = 4 seconds)
 time = millis();
 
 float timeDiff = time - oldTime;
 //convert milliseconds
 timeDiff = timeDiff *.001;
 //degrees/second * seconds cancels seconds leaving only degrees
 gyroVal = gyro.data.x * timeDiff;
 //track a stable angle
 angle += gyroVal;
 
 Serial.println(angle);

  delay(100);
}

I don't see any "drift" in your sketch.

What is happening comparing to what you are expecting?