- All products
- Sensors & Modules
- Magnetic
- Compass Magnetic Electronic 3-Axis Module GY-271 HMC5883L
- Magnetic
Features
- 3-axis magnetic field sensing for accurate heading detection
- Compact size, suitable for space-constrained projects
- Digital I2C interface for easy microcontroller integration
- High sensitivity for low-field magnetic measurements
- Ideal for robotics, UAVs, and navigation applications
Specifications
| Parameter | Details |
|---|---|
| Chipset | HMC5883L |
| Sensor Type | 3-Axis Magnetic Electronic Compass |
| Communication | I2C Protocol |
| Operating Voltage | 3V – 5V DC |
| Measuring Range | ±1.3 to ±8 Gauss |
| Output | Digital magnetic field data (X, Y, Z axes) |
Pinout

- VCC: Power supply (3V to 5V)
- GND: Ground
- SCL: I2C Clock
- SDA: I2C Data
- DRDY (Data Ready): Goes LOW for ~250µs when new data is available (internally pulled up)
How to Be Used:
- Connect the module to your microcontroller using the I2C interface.
- Supply power to the module through the VCC and GND pins (3V–5V supported).
- Connect SDA to the microcontroller’s I2C data pin and SCL to the I2C clock pin.
- Upload the example code to initialize the HMC5883L and read magnetic field data.
- Place the module away from strong magnetic sources (motors, speakers, metal frames) for accurate readings.
- Use the X and Y axis data to calculate heading (compass direction) in degrees.
Connections (Arduino Uno)

| GY-271 Pin | Arduino Uno |
|---|---|
| VCC | 5V |
| GND | GND |
| SDA | A4 |
| SCL | A5 |
Library Requirement
- Install Adafruit HMC5883 Unified library from the Arduino Library Manager.
- Also ensure the Adafruit Unified Sensor library is installed.
Example Code (Arduino)
#include "Wire.h"
#include "Adafruit_Sensor.h"
#include "Adafruit_HMC5883_U.h"
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
void setup() {
Serial.begin(9600);
Wire.begin();
if (!mag.begin()) {
Serial.println("HMC5883L not detected!");
while (1);
}
}
void loop() {
sensors_event_t event;
mag.getEvent(&event);
float heading = atan2(event.magnetic.y, event.magnetic.x);
heading = heading * 180 / PI;
if (heading < 0) {
heading += 360;
}
Serial.print("Heading: ");
Serial.print(heading);
Serial.println(" degrees");
delay(500);
}
Code Explanation
- Initializes I2C communication and the HMC5883L sensor.
- Reads magnetic field data from X and Y axes.
- Calculates compass heading using arctangent math.
- Converts the heading value into degrees (0°–360°).
- Outputs the heading to the Serial Monitor.
Applications
- Digital compass and navigation systems
- Robotics orientation and heading control
- UAV and drone stabilization
- Direction sensing in embedded systems
Features
- 3-axis magnetic field sensing for accurate heading detection
- Compact size, suitable for space-constrained projects
- Digital I2C interface for easy microcontroller integration
- High sensitivity for low-field magnetic measurements
- Ideal for robotics, UAVs, and navigation applications
Specifications
| Parameter | Details |
|---|---|
| Chipset | HMC5883L |
| Sensor Type | 3-Axis Magnetic Electronic Compass |
| Communication | I2C Protocol |
| Operating Voltage | 3V – 5V DC |
| Measuring Range | ±1.3 to ±8 Gauss |
| Output | Digital magnetic field data (X, Y, Z axes) |
Pinout

- VCC: Power supply (3V to 5V)
- GND: Ground
- SCL: I2C Clock
- SDA: I2C Data
- DRDY (Data Ready): Goes LOW for ~250µs when new data is available (internally pulled up)
How to Be Used:
- Connect the module to your microcontroller using the I2C interface.
- Supply power to the module through the VCC and GND pins (3V–5V supported).
- Connect SDA to the microcontroller’s I2C data pin and SCL to the I2C clock pin.
- Upload the example code to initialize the HMC5883L and read magnetic field data.
- Place the module away from strong magnetic sources (motors, speakers, metal frames) for accurate readings.
- Use the X and Y axis data to calculate heading (compass direction) in degrees.
Connections (Arduino Uno)

| GY-271 Pin | Arduino Uno |
|---|---|
| VCC | 5V |
| GND | GND |
| SDA | A4 |
| SCL | A5 |
Library Requirement
- Install Adafruit HMC5883 Unified library from the Arduino Library Manager.
- Also ensure the Adafruit Unified Sensor library is installed.
Example Code (Arduino)
#include "Wire.h"
#include "Adafruit_Sensor.h"
#include "Adafruit_HMC5883_U.h"
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
void setup() {
Serial.begin(9600);
Wire.begin();
if (!mag.begin()) {
Serial.println("HMC5883L not detected!");
while (1);
}
}
void loop() {
sensors_event_t event;
mag.getEvent(&event);
float heading = atan2(event.magnetic.y, event.magnetic.x);
heading = heading * 180 / PI;
if (heading < 0) {
heading += 360;
}
Serial.print("Heading: ");
Serial.print(heading);
Serial.println(" degrees");
delay(500);
}
Code Explanation
- Initializes I2C communication and the HMC5883L sensor.
- Reads magnetic field data from X and Y axes.
- Calculates compass heading using arctangent math.
- Converts the heading value into degrees (0°–360°).
- Outputs the heading to the Serial Monitor.
Applications
- Digital compass and navigation systems
- Robotics orientation and heading control
- UAV and drone stabilization
- Direction sensing in embedded systems