

The GY-511 is a digital compass module based on the HMC5883L sensor, designed to provide precise heading and orientation information. It measures the Earth's magnetic field in three axes (X, Y, and Z) and outputs the data digitally via the I2C communication protocol. This module is widely used in robotics, navigation systems, drones, and other applications requiring accurate directional data.








Below are the key technical details of the GY-511 module:
| Parameter | Value |
|---|---|
| Sensor Model | HMC5883L |
| Manufacturer Part ID | MP085 |
| Communication Protocol | I2C |
| Operating Voltage | 3.3V to 5V |
| Operating Current | 100 µA (typical) |
| Measurement Range | ±1.3 to ±8 Gauss |
| Resolution | 0.73 mG/LSB (at ±1.3 Gauss range) |
| Dimensions | 14mm x 13mm x 3.5mm |
| Operating Temperature | -40°C to +85°C |
The GY-511 module has 4 pins for interfacing. The table below describes each pin:
| Pin | Name | Description |
|---|---|---|
| 1 | VCC | Power supply input (3.3V to 5V). Connect to the power source. |
| 2 | GND | Ground. Connect to the ground of the circuit. |
| 3 | SDA | I2C data line. Connect to the SDA pin of the microcontroller. |
| 4 | SCL | I2C clock line. Connect to the SCL pin of the microcontroller. |
0x1E. Ensure no other devices on the I2C bus conflict with this address.Below is an example of how to interface the GY-511 with an Arduino UNO using the Wire library:
#include <Wire.h>
// HMC5883L I2C address
#define HMC5883L_ADDRESS 0x1E
void setup() {
Wire.begin(); // Initialize I2C communication
Serial.begin(9600); // Start serial communication for debugging
// Initialize the HMC5883L sensor
Wire.beginTransmission(HMC5883L_ADDRESS);
Wire.write(0x00); // Select configuration register A
Wire.write(0x70); // Set 8-average, 15 Hz default, normal measurement
Wire.endTransmission();
Wire.beginTransmission(HMC5883L_ADDRESS);
Wire.write(0x01); // Select configuration register B
Wire.write(0xA0); // Set gain = 5
Wire.endTransmission();
Wire.beginTransmission(HMC5883L_ADDRESS);
Wire.write(0x02); // Select mode register
Wire.write(0x00); // Continuous measurement mode
Wire.endTransmission();
}
void loop() {
int16_t x, y, z;
// Request 6 bytes of data from the sensor
Wire.beginTransmission(HMC5883L_ADDRESS);
Wire.write(0x03); // Start reading from data output X MSB register
Wire.endTransmission();
Wire.requestFrom(HMC5883L_ADDRESS, 6);
if (Wire.available() == 6) {
x = (Wire.read() << 8) | Wire.read(); // Combine MSB and LSB for X-axis
z = (Wire.read() << 8) | Wire.read(); // Combine MSB and LSB for Z-axis
y = (Wire.read() << 8) | Wire.read(); // Combine MSB and LSB for Y-axis
}
// Print the raw magnetometer data
Serial.print("X: ");
Serial.print(x);
Serial.print(" Y: ");
Serial.print(y);
Serial.print(" Z: ");
Serial.println(z);
delay(500); // Wait for 500ms before the next reading
}
No Data Output
0x1E.Inaccurate Readings
I2C Communication Errors
Q: Can the GY-511 operate at 5V?
A: Yes, the GY-511 module supports both 3.3V and 5V power supplies.
Q: How do I calibrate the GY-511?
A: Rotate the module in all directions while collecting data. Use the readings to calculate offsets and scaling factors to correct for distortions.
Q: What is the maximum range of the GY-511?
A: The HMC5883L sensor on the GY-511 can measure magnetic fields up to ±8 Gauss.
Q: Can I use the GY-511 with microcontrollers other than Arduino?
A: Yes, the GY-511 can be used with any microcontroller that supports I2C communication, such as Raspberry Pi, ESP32, or STM32.