GY88 (altimeter, compass, gyro) Arduino Interface

Edward A. Kimble, PhD, Purdue '77

/* ===========Interfacing code for the GY-88 ===============
 Code from multiple sources including, Jim Lindblom,
 Jordan McConnell, SparkFun Electronics, and John Chi
 Program exposes most data from the GY-88
 Not shown, By adding SPI and SD header files, data can be printed to disk
 instead of monitor by using command "myfile.print()" for rocket altimeter
 or for RF telemetry.
 =================================================== */
#include <Wire.h>

#define BMP085_ADDRESS 0x77  // I2C address of BMP085 baraometer
#define MAG_ADDRESS 0x1E // I2C address of HMC5883 magnetometer
//alternate code representation below
const int MPU=0x68;  // I2C address of the MPU-6050 gyroscope
//========= barometer variables ============
float inhg; //inches of mercury, barometer
const unsigned char OSS = 0;  // Oversampling Setting
// Calibration values
int ac1,ac2,ac3,b1,b2,mb,mc,md,b5;
unsigned int ac4,ac5,ac6;
// b5 is calculated in bmp085GetTemperature(...), this variable 
//is also used in bmp085GetPressure(...)
// so ...Temperature(...) must be called before ...Pressure(...).

//====== MPU-6050 variables======
int AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

//=======Magnetometer variables=========
int x,y,z; //triple axis data
//void bmp085Calibration(); //prototype for debugging (don't ask)
void setup(){

void loop()
{//============= Barometer/Altimeter ============
  float temperature = bmp085GetTemperature(bmp085ReadUT()); //MUST be called first
  float pressure = bmp085GetPressure(bmp085ReadUP());
  float atm = pressure / 101325; // "standard atmosphere"
  float altitude = calcAltitude(pressure); //Uncompensated caculation - in Meters 

  Serial.print("Temperature: ");
  Serial.print(temperature, 2); //display 2 decimal places
  Serial.print("deg C  ");
  Serial.print(((temperature*9.0/5.0)+32.0), 2); //display 2 decimal places
  Serial.println("deg F");
  Serial.print("Pressure: ");
  Serial.print(pressure, 0); //whole number only.
  Serial.println(" Pa");
  inhg=(float)pressure*0.0002953+1.14;//1.14 is fudge factor for local compensation
  Serial.print(inhg, 2); //whole number only.
  Serial.println(" Inches of Hg");
  Serial.print("Standard Atmosphere: ");
  Serial.println(atm, 4); //display 4 decimal places
  Serial.print("Altitude: ");
  Serial.print(altitude, 2); //display 2 decimal places
  Serial.println(" M");
  Serial.println();//line break
//============= Gyro and Accelerometer ============
//initialization of module 
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  //data collection 
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.requestFrom(MPU,14,true);  // request a total of 14 registers<<8|;  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)<<8|;  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)<<8|;  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)<<8|;  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)<<8|;  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)<<8|;  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)<<8|;  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  Serial.print("AcX = "); Serial.print(AcX);
  Serial.print(" | AcY = "); Serial.print(AcY);
  Serial.print(" | AcZ = "); Serial.print(AcZ);
  //equation for temperature in degrees C from datasheet
  Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53);
  Serial.print(" | GyX = "); Serial.print(GyX);
  Serial.print(" | GyY = "); Serial.print(GyY);
  Serial.print(" | GyZ = "); Serial.println(GyZ);
  // =================  magnetometer ================
  Wire.begin();//Put the HMC5883 IC into the correct operating mode
  Wire.beginTransmission(MAG_ADDRESS); //open communication with HMC5883
  Wire.write(0x02); //select mode register
  Wire.write(0x00); //continuous measurement mode
  Wire.write(0x03); //select register 3, X MSB register
 //Data collection from each axis, 2 registers per axis
  Wire.requestFrom(MAG_ADDRESS, 6);
    x =<<8; //X msb
    x |=; //X lsb
    z =<<8; //Z msb
    z |=; //Z lsb
    y =<<8; //Y msb
    y |=; //Y lsb
  //Print out values of each axis
  Serial.print("x: ");
  Serial.print("  y: ");
  Serial.print("  z: ");
// ====================== End of Program ================

// ============== begin altimeter subroutines =============
// Stores all of the bmp085's calibration values into global variables
// Calibration values are required to calculate temp and pressure
// This function should be called at the beginning of the program
void bmp085Calibration()
{ ac1 = bmp085ReadInt(0xAA);
  ac2 = bmp085ReadInt(0xAC);
  ac3 = bmp085ReadInt(0xAE);
  ac4 = bmp085ReadInt(0xB0);
  ac5 = bmp085ReadInt(0xB2);
  ac6 = bmp085ReadInt(0xB4);
  b1 = bmp085ReadInt(0xB6);
  b2 = bmp085ReadInt(0xB8);
  mb = bmp085ReadInt(0xBA);
  mc = bmp085ReadInt(0xBC);
  md = bmp085ReadInt(0xBE);

// Calculate temperature in deg C
float bmp085GetTemperature(unsigned int ut){
  long x1, x2;
  x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  x2 = ((long)mc << 11)/(x1 + md);
  b5 = x1 + x2;
  float temp = ((b5 + 8)>>4);
  temp = temp /10;
  return temp;

// Calculate pressure given up
// calibration values must be known
// b5 is also required so bmp085GetTemperature(...) must be called first.
// Value returned will be pressure in units of Pa.
long bmp085GetPressure(unsigned long up){
  long x1, x2, x3, b3, b6, p;
  unsigned long b4, b7;
  b6 = b5 - 4000;
  // Calculate B3
  x1 = (b2 * (b6 * b6)>>12)>>11;
  x2 = (ac2 * b6)>>11;
  x3 = x1 + x2;
  b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
  // Calculate B4
  x1 = (ac3 * b6)>>13;
  x2 = (b1 * ((b6 * b6)>>12))>>16;
  x3 = ((x1 + x2) + 2)>>2;
  b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
  b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  if (b7 < 0x80000000)
    p = (b7<<1)/b4;
   p = (b7/b4)<<1;
  x1 = (p>>8) * (p>>8);
  x1 = (x1 * 3038)>>16;
  x2 = (-7357 * p)>>16;
  p += (x1 + x2 + 3791)>>4;
  long temp = p;
  return temp;

// Read 1 byte from the BMP085 at 'address'
char bmp085Read(unsigned char address)
{ unsigned char data;
  Wire.requestFrom(BMP085_ADDRESS, 1);

// Read 2 bytes from the BMP085
// First byte will be from 'address'
// Second byte will be from 'address'+1
int bmp085ReadInt(unsigned char address)
{ unsigned char msb, lsb;
  Wire.requestFrom(BMP085_ADDRESS, 2);
  while(Wire.available()<2) ;
  msb =;
  lsb =;
  return (int) msb<<8 | lsb;

// Read the uncompensated temperature value
unsigned int bmp085ReadUT(){
  unsigned int ut;
  // Write 0x2E into Register 0xF4
  // This requests a temperature reading
  // Wait at least 4.5ms
  // Read two bytes from registers 0xF6 and 0xF7
  ut = bmp085ReadInt(0xF6);
  return ut;

// Read the uncompensated pressure value
unsigned long bmp085ReadUP(){
  unsigned char msb, lsb, xlsb;
  unsigned long up = 0;
  // Write 0x34+(OSS<<6) into register 0xF4
  // Request a pressure reading w/ oversampling setting
  Wire.write(0x34 + (OSS<<6));
  // Wait for conversion, delay time dependent on OSS
  delay(2 + (3<<OSS));
  // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
  msb = bmp085Read(0xF6);
  lsb = bmp085Read(0xF7);
  xlsb = bmp085Read(0xF8);
  up = (((unsigned long) msb << 16) | 
((unsigned long) lsb << 8) | 
(unsigned long) xlsb) >> (8-OSS);
  return up;

void writeRegister(int deviceAddress, byte address, byte val) {
  Wire.beginTransmission(deviceAddress); // start transmission to device 
  Wire.write(address);       // send register address
  Wire.write(val);         // send value to write
  Wire.endTransmission();     // end transmission

int readRegister(int deviceAddress, byte address){
  int v;
  Wire.write(address); // register to read
  Wire.requestFrom(deviceAddress, 1); // read a byte
  while(!Wire.available()) {
    // waiting
  v =;
  return v;

float calcAltitude(float pressure){
  float A = pressure/101325;
  float B = 1/5.25588;
  float C = pow(A,B);
  C = 1 - C;
  C = C /0.0000225577;
  return C;

The Gy88 is an inexpensive flight controller board for any suitable low cost flight computer such as an Arduino or Raspberry Pi. This type of board is popular for controlling hobby drones as it allows orientation, height, and position (via some calculus) to be determined for joystick control and with comparative ease. I bought this module from Banggood but similar boards exist such as this at Adafruit, and any number of Asian vendors currently have this board or similar. Check Ebay, sparkfun, and the other low cost electronics guys, you will be pleasantly surprised. I've tried to emphasize the word currently as this sort of technology is fluid, changing daily. The good news, the chips themselves have been stable for several years.
These sensors use the popular I2C bus on the Arduino, which has been designated as analog pins 4 and 5 plus the power supply, in this case the 5.0 volt supply pin and ground from the Arduino. Yes,supposedly this version of the series has a 3.3 V supply and level shift built in. I've only run it for a few days so I can't swear to it's ever sweet goodness. I have tried quite a number of subroutines for reading the barometer chip. And this is about the best I've found. Many examples either fail utterly, giving totally bogus values, lose calibration slowly, drifting over several hours fail, or they simply fail. You may still note some minor error in the readings here, but the good news, the accelerometer detects gravity and points down, the barometer reads better than my living room barometer ( +/- .02 inches of mercury), and the compass points north, yea rah!!, but also showing the declination or incidence angle of the magnetic field to the earth, giving a good clue on most days as to current longitude. Dutton's book on navigation needs seriously updated smile !!
The first picture below shows a printout from this monster, and some things will need further web search. For example, the accelerometer currently reads from 0 to 16384 for accelerations of 0g to 1g, probably not optimum for rockets. But note the chip allows range to be selected up to 16g, I just haven't tried it. And note, rockets can produce hundreds of g's on launch or impact. Still, the barometer in my living room reads 30.02 In Hg, my house is not North South but askew, temperature is a sizzling 72.7 degrees F identical to our wall thermometer. The elevation here is about 287 meters, close to board value, minus the current barometric pressure. In short, all the readings are good enough for a house that sits in the middle of a hayfield in Indiana. But still, caveat emptor, cheap board, lazy programmer, and this is just a home kluge, not science, you enter at your own risk smile The bottom picture shows the wiring, not much drama there...

Below is the wiring diagram. If this format looks familiar, I reworked it from a diagram posted here.

Edward Kimble, PhD click here to e-mail me at:
Edited May 1, 2016