

The HMC5883L is a digital compass module equipped with a three-axis magnetometer that measures magnetic fields. It is capable of providing precise heading information, making it an essential component for navigation, orientation, and motion-tracking applications. The module communicates via the I2C protocol, making it easy to interface with microcontrollers like Arduino, Raspberry Pi, and others.








The HMC5883L module is designed for low-power, high-accuracy magnetic field sensing. Below are its key technical details:
| Parameter | Value |
|---|---|
| Operating Voltage | 3.0V to 5.0V |
| Operating Current | 100 µA (typical) |
| Communication Interface | I2C (7-bit address: 0x1E) |
| Measurement Range | ±1.3 to ±8 Gauss |
| Resolution | 12-bit ADC |
| Output Data Rate (ODR) | 0.75 Hz to 75 Hz |
| Operating Temperature | -40°C to +85°C |
| Dimensions | 14mm x 13mm x 3.5mm |
The HMC5883L module typically has four pins for interfacing. Below is the pinout description:
| Pin | Name | Description |
|---|---|---|
| 1 | VCC | Power supply input (3.0V to 5.0V) |
| 2 | GND | Ground connection |
| 3 | SDA | I2C data line |
| 4 | SCL | I2C clock line |
To use the HMC5883L with an Arduino UNO, follow these steps:
VCC pin to the Arduino's 5V pin.GND pin to the Arduino's GND.SDA pin to the Arduino's A4 pin (I2C data line).SCL pin to the Arduino's A5 pin (I2C clock line).Below is an example Arduino sketch to read data from the HMC5883L and display the heading on the Serial Monitor:
#include <Wire.h>
// HMC5883L I2C address
#define HMC5883L_ADDRESS 0x1E
void setup() {
Wire.begin(); // Initialize I2C communication
Serial.begin(9600); // Start serial communication
// Initialize HMC5883L
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(0x20); // Set gain to 1.3 Gauss
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 HMC5883L
Wire.beginTransmission(HMC5883L_ADDRESS);
Wire.write(0x03); // Start reading from data output 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
}
// Calculate heading in degrees
float heading = atan2(y, x) * 180 / PI;
if (heading < 0) {
heading += 360; // Ensure heading is positive
}
// Print heading to Serial Monitor
Serial.print("Heading: ");
Serial.print(heading);
Serial.println("°");
delay(500); // Wait for 500ms before the next reading
}
No Data or Incorrect Readings
0x1E.Fluctuating or Unstable Readings
Heading Stuck at a Fixed Value
Q: Can the HMC5883L measure altitude?
A: No, the HMC5883L is a magnetometer and only measures magnetic fields. For altitude, you would need a barometric pressure sensor.
Q: How do I calibrate the HMC5883L?
A: Calibration involves collecting raw data while rotating the module in all directions and applying offset and scaling corrections. Libraries like Adafruit's HMC5883L library include calibration functions.
Q: Can I use the HMC5883L with a 3.3V microcontroller?
A: Yes, the HMC5883L supports a voltage range of 3.0V to 5.0V, making it compatible with 3.3V systems.
By following this documentation, you can effectively integrate the HMC5883L into your projects for accurate navigation and orientation sensing.