will be read-only starting December 31st, 2018. For more info pleae look at this Forum Post

Interfacing Gyros

This is just a basic code snippet for the time being, but please feel free to contribute more information

Gyroscopes measure the rate of change of a particular axis at the current moment in time. This means that to keep track of our angle, we need to sum all of the rates of change over a given period of time. We're essentially looking for the integral of our gyro data. In the sample code below we'll be looking at just one gyro axis.

First off, you'll want to set the voltage of your gyro (typically 3.3v or 5v) for the gyroVoltage variable. This will setup how the values are scaled.

Next you'll need to know the zero voltage of your gyro. You'll be able to find this in the gyro's datasheet. It is sometimes called the Null voltage. In this example, I set the gyroZeroVoltage to 2.5. This is the voltage your gyro normally outputs when it's not rotating.

The other value you'll need from the datasheet is the sensitivity. This is expressed in mV/degree/second. Since this is mV, we need to convert to volts. You'll see in the examples, my gyro had a sensitivity of 7mV/deg/sec.

The Arduino has 10 bit ADC which can represent a voltage (0-5V) in values from 0 to 1023. Unfortunately you'll definitely see error building up over time since we can never get a perfectly accurate voltage reading from the gyro and our numbers never divide out evenly. You can set the rotationThreshold to a minimum degree/second that the gyro has to be detected rotating at. If this degree/second rate is not met, the data will be discarded.

/* Keep track of gyro angle over time
 * Connect Gyro to Analog Pin 0
 * Sketch by eric barch /
 * v. 0.1 - simple serial output

int gyroPin = 0;               //Gyro is connected to analog pin 0
float gyroVoltage = 5;         //Gyro is running at 5V
float gyroZeroVoltage = 2.5;   //Gyro is zeroed at 2.5V
float gyroSensitivity = .007;  //Our example gyro is 7mV/deg/sec
float rotationThreshold = 1;   //Minimum deg/sec to keep track of - helps with gyro drifting

float currentAngle = 0;          //Keep track of our current angle

void setup() {
  Serial.begin (9600);

void loop() {
  //This line converts the 0-1023 signal to 0-5V
  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 we are running in a 10ms loop (1000ms/10ms)
    gyroRate /= 100;
    currentAngle += gyroRate;

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