Blog

GY-271 HMC5883L Triple Axis Magnetometer Sensor Module

GY-271 HMC5883L Triple Axis Magnetometer

High-Sensitivity Digital Compass Module for Arduino Projects

Introduction

The GY-271 HMC5883L is a 3-axis digital compass module featuring the Honeywell HMC5883L magnetometer IC. With 1° to 2° heading accuracy and a resolution up to 5 milli-gauss, it's ideal for navigation systems, robotics, and orientation detection applications.

HMC5883L Module

Key Features

🧭 Triple Axis

Measures X, Y, Z magnetic fields simultaneously

📡 High Sensitivity

1-2 milli-gauss resolution (up to 8 Gauss range)

🔋 Low Power

100μA operation current (0.5μA standby)

🔌 I2C Interface

Standard digital output (3.3V or 5V compatible)

Technical Specifications

Measurement Range ±1.3 to ±8.1 Gauss (selectable)
Resolution 0.73 to 4.35 milli-gauss (depending on range)
Update Rate Up to 160Hz
Supply Voltage 2.16V to 3.6V (5V tolerant I/O)
I2C Address 0x1E (fixed)
Accuracy 1°-2° heading (typical)

Pin Configuration

HMC5883L Pinout
Pin Label Description Arduino Connection
1 VCC Power (3.3V recommended) 3.3V
2 GND Ground GND
3 SCL I2C Clock A5 (Uno) or SCL
4 SDA I2C Data A4 (Uno) or SDA
5 DRDY Data Ready (Optional) Any digital pin
Important: Requires 3.3V power (use voltage regulator if needed)

Wiring Diagram (Arduino Uno)

// Basic Connections:
// VCC → 3.3V (use regulator if needed)
// GND → GND
// SCL → A5 (or SCL)
// SDA → A4 (or SDA)
// DRDY → (optional interrupt pin)

Basic Compass Example

#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);
  if(!mag.begin()) {
    Serial.println("Could not find HMC5883L");
    while(1);
  }
}

void loop() {
  sensors_event_t event; 
  mag.getEvent(&event);
  
  float heading = atan2(event.magnetic.y, event.magnetic.x);
  float declinationAngle = 0.22; // Adjust for your location
  heading += declinationAngle;
  
  // Correct for when signs are reversed
  if(heading < 0) heading += 2*PI; if(heading > 2*PI) heading -= 2*PI;
  
  float headingDegrees = heading * 180/M_PI;
  
  Serial.print("Heading: ");
  Serial.print(headingDegrees);
  Serial.println("°");
  delay(500);
}
Library Required: Install “Adafruit HMC5883 Unified” via Arduino Library Manager

Advanced Configuration

Measurement Range

// Set measurement range (default ±1.3 Ga)
mag.setRange(HMC5883_RANGE_1_3GA); 
// Other options: 1.9GA, 2.5GA, 4.0GA, 4.7GA, 5.6GA, 8.1GA

Data Rate

// Set output data rate (default 15Hz)
mag.setDataRate(HMC5883_DATARATE_15HZ);
// Other options: 0.75, 1.5, 3, 7.5, 30, 75, 220Hz

Measurement Mode

// Set continuous measurement mode
mag.setMeasurementMode(HMC5883_CONTINUOUS);
// Other options: SINGLE (power saving), IDLE

Calibration

// Hard iron calibration offsets
mag.setOffset(-10.35, 58.95, -40.59); 
// Obtain values from calibration routine

Troubleshooting

No Device Detected

  • Verify I2C connections (SDA/SCL)
  • Check with I2C scanner sketch
  • Confirm 3.3V power supply

Incorrect Readings

  • Keep away from magnetic sources (motors, speakers)
  • Perform hard/soft iron calibration
  • Set proper declination for your location

Erratic Values

  • Add 0.1μF decoupling capacitor near VCC
  • Ensure stable power supply
  • Check for electromagnetic interference