Contents

Interfacing GY-87 IMU MPU6050 HMC5883L BMP085 Module with Arduino

GY-87 IMU Module Features

GY-87 IMU module is a very high accuracy module compared to similar models such as MPU9250 and can provide acceleration, change of angle and strength of the magnetic field in three axes x, y and z. This module is actually a combination of 3 sensors. MPU6050, HMC5883L and a barometric temperature and pressure sensor. The barometric temperature and pressure sensor can be either BMP180 or BMP085. The communication protocol is I2C.

GY-87 Module Pinout

This module has 8 pins:

  • VCC_IN: Module power supply – 5V
  • 3V: Module power supply – 3.3V
  • GND: Ground
  • SCL: Serial Clock for I2C protocol
  • SDA: Serial Data for I2C protocol
  • FSYNC: Synchronous digital input (I2C speed setting)
  • INTA: MPU6050 IC interrupt pin
  • DRDY: Data ready pin for HMC5883L IC

Required Materials

Hardware Components

Arduino UNO R3 × 1
GY-87 10DOF IMU Sensor Module × 1
Male to Female jumper wire × 1

Software Apps

Arduino IDE

Interfacing GY-87 Module with Arduino

Step 1: Circuit

The following circuit shows how you should connect Arduino to the GY-87 module. Connect wires accordingly.

Step 2: Library

To access all sensors in GY-87 module, you need to use the corresponding library. Download these libraries below. Then go to the Include Library and install them.

Then go to the Include Library section and install them.

Step 3: Code

Upload the following code to your Arduino. After that, open the Serial Monitor.

/*
  Modified on Mar 16, 2021
  Modified by MehranMaleki from Arduino Examples
  https://electro
  peak.com/learn/
*/

#include "I2Cdev.h"
#include "MPU6050.h"
#include <Adafruit_BMP085.h>
#include <HMC5883L_Simple.h>


MPU6050 accelgyro;
Adafruit_BMP085 bmp;
HMC5883L_Simple Compass;


int16_t ax, ay, az;
int16_t gx, gy, gz;

#define LED_PIN 13
bool blinkState = false;

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

  // initialize devices
  Serial.println("Initializing I2C devices...");

  // initialize bmp085
  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP085 sensor, check wiring!");
    while (1) {}
  }

  // initialize mpu6050
  accelgyro.initialize();
  Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  accelgyro.setI2CBypassEnabled(true); // set bypass mode for gateway to hmc5883L
  
  
  // initialize hmc5883l
  Compass.SetDeclination(23, 35, 'E');
  Compass.SetSamplingMode(COMPASS_SINGLE);
  Compass.SetScale(COMPASS_SCALE_130);
  Compass.SetOrientation(COMPASS_HORIZONTAL_X_NORTH);


  // configure Arduino LED for checking activity
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  Serial.print("Temperature = ");
  Serial.print(bmp.readTemperature());
  Serial.println(" *C");

  Serial.print("Pressure = ");
  Serial.print(bmp.readPressure());
  Serial.println(" Pa");
  
  // Calculate altitude assuming 'standard' barometric
  // pressure of 1013.25 millibar = 101325 Pascal
  Serial.print("Altitude = ");
  Serial.print(bmp.readAltitude());
  Serial.println(" meters");
  Serial.print("Pressure at sealevel (calculated) = ");
  Serial.print(bmp.readSealevelPressure());
  Serial.println(" Pa");
  Serial.print("Real altitude = ");
  Serial.print(bmp.readAltitude(101500));
  Serial.println(" meters");

  
  // read raw accel/gyro measurements from device
  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  
  // display tab-separated accel/gyro x/y/z values
  Serial.print("a/g:\t");
  Serial.print(ax); Serial.print("\t");
  Serial.print(ay); Serial.print("\t");
  Serial.print(az); Serial.print("\t");
  Serial.print(gx); Serial.print("\t");
  Serial.print(gy); Serial.print("\t");
  Serial.println(gz);


  float heading = Compass.GetHeadingDegrees();
  Serial.print("Heading: \t");
  Serial.println( heading );


  // blink LED to indicate activity
  blinkState = !blinkState;
  digitalWrite(LED_PIN, blinkState);

  delay(500);
}

In the above code, various information such as temperature, pressure, altitude, acceleration and magnetic field strength are received from the module and displayed on the Serial Monitor.

The output is as follows.

Liked What You See?​
Get Updates And Learn From The Best​

Comments (16)

  • Orlando Capetillo Reply

    Hi I am trying to connect an GY-87-12C IC 10 DOF MPU6050 to my mega
    will try your code
    if you send your email
    i will forward the results back

    October 5, 2022 at 5:18 pm
  • Orlando Capetillo Reply

    your library “SIMPLE” does not compile

    October 5, 2022 at 5:52 pm
  • sujal Reply

    does your code work
    pls send the results along with the code

    January 4, 2023 at 7:44 am
    • Ali Abdolmaleki Reply

      Hi dear
      it should be work.
      All codes are tested before uploading

      March 1, 2023 at 6:44 am
  • code Reply

    Actually, the magnetometer date is stuck and does not give any correct value. You can also see in your example output the value has not changed.

    March 6, 2023 at 6:35 pm
    • Mohammad Damirchi Reply

      Hi.
      If the sensor gets close to the magnet/electromagnet field, chances are it will be harmed

      April 16, 2023 at 12:12 pm
  • Martin Reply

    Hello, how can I fix the error : “MPU6050 connection failed” ?

    June 30, 2023 at 8:50 am
    • Mohammad Damirchi Reply

      Hi Martin,
      This error is usually for incorrect wiring of the module.
      Notice that this board uses I2C and you should find the I2C pins on your micro and connect the wires to them.
      You can also find your module ID by using the I2C Scanner example in the Arduino IDE examples section and uploading it to the board. Change this ID if necessary.

      July 1, 2023 at 6:46 am
  • Anonymous Reply

    I am getting “Could not find a valid BMP085 sensor, check wiring!”

    March 9, 2024 at 1:20 pm
    • Mohammad Damirchi Reply

      Hello,
      To determine the ID of your sensor, you can utilize the I2C Scanner example.
      If you don’t receive any ID, it’s advisable to verify your wiring.
      Keep in mind that the I2C pin varies for each Arduino model or any board. Once you obtain the ID, compare it with the default ID specified in the library.
      You may need to modify the ID to ensure proper data retrieval.

      March 10, 2024 at 4:55 am
  • Javra Reply

    Hello, I’m glad everything works fine except to the mpu5060 sensor library part, it seems I cant download the file as it says “sorry, file not found”. To compensate to the missing libraries, I tried to add every possible MPU6050 libraries I could and test them out. But to no avail, it never works and always says MPU5060 connection failed. I also tried the solution of “You can also find your module ID by using the I2C Scanner example in the Arduino IDE examples section and uploading it to the board. Change this ID if necessary” by Mohammad Damirchi. I believe the code for that was #define MPU6050_ADDRESS 0x77 .Still doesn’t work, please help I’m really frustrated and been finding the solution for almost 14 hours. I woul really appreicate the help, thanks!

    April 22, 2024 at 1:45 am
    • Mohammad Damirchi Reply

      Hello Javra,
      The issue with the download link has been resolved. You can now try downloading the file without any problems. Additionally, you can access the library used for this article through the following link.

      April 23, 2024 at 5:46 am
      • Javra Reply

        Thank you for the reply, really means a lot. I just wanna say that the download link are working fine, but it seems that the MPU6050 Sensor Library gave me a file named HMC5883L_Simple. This is the same file to the HMC5883L_Simple library. I checked both libraries content, and still the same.

        April 25, 2024 at 8:55 am
        • Mohammad Damirchi Reply

          Hi javra,
          I’ve updated the file again. If you encounter any further issues, you can download it from this link.

          April 27, 2024 at 5:53 am
          • John

            Hello!May I ask if the a/g in the code refers to the acceleration/magnetic rotation ratio?
            Why didn’t I find any data on magnetic field strength?
            If I want to measure the size of a magnetic field, how should the code be written?

            June 21, 2024 at 1:02 am
          • Mohammad Damirchi

            Hello John,

            The “a/g” refers to accelerometer/gyroscope (as noted in the code comment).
            To obtain magnetic readings, we utilize the HMC5883L sensor and retrieve the heading in degrees using `GetHeadingDegrees`. If you need to gather all the data at once, use this command:

            // Read compass values
            compass.read();
            x = compass.getX();
            y = compass.getY();
            z = compass.getZ();

            For further details, please consult the associated article.

            June 22, 2024 at 8:30 am

Leave a Reply

Your email address will not be published. Required fields are marked *