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
Software Apps
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.
MPU6050 Sensor Library
HMC5883L_Simple library
BMP085 Sensor Library
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.
Comments (16)
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
your library “SIMPLE” does not compile
does your code work
pls send the results along with the code
Hi dear
it should be work.
All codes are tested before uploading
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.
Hi.
If the sensor gets close to the magnet/electromagnet field, chances are it will be harmed
Hello, how can I fix the error : “MPU6050 connection failed” ?
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.
I am getting “Could not find a valid BMP085 sensor, check wiring!”
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.
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!
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.
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.
Hi javra,
I’ve updated the file again. If you encounter any further issues, you can download it from this link.
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?
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.