

The HMC5883L is a digital compass sensor equipped with a three-axis magnetometer for measuring magnetic fields. It is widely used in navigation, orientation, and motion-tracking applications. The sensor communicates with microcontrollers via the I2C protocol, making it easy to integrate into various projects. Its compact size and low power consumption make it ideal for portable devices and embedded systems.








Below are the key technical details of the HMC5883L:
| Parameter | Value |
|---|---|
| Operating Voltage | 2.16V to 3.6V |
| Communication Protocol | I2C |
| Measurement Range | ±1.3 to ±8 Gauss |
| Resolution | 12-bit ADC |
| Operating Temperature | -40°C to +85°C |
| Power Consumption | 100 µA (typical) |
| Dimensions | 3mm x 3mm x 0.9mm |
The HMC5883L has 8 pins, but only 4 are typically used for operation. Below is the pinout description:
| Pin | Name | Description |
|---|---|---|
| 1 | VCC | Power supply input (2.16V to 3.6V) |
| 2 | GND | Ground connection |
| 3 | SCL | I2C clock line |
| 4 | SDA | I2C data line |
| 5-8 | NC | Not connected (leave unconnected in typical usage) |
To use the HMC5883L with an Arduino UNO, follow these steps:
Below is an example Arduino sketch to read data from the HMC5883L:
#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 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(0xA0); // Set gain to 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 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) {
// Read data and combine high and low bytes
x = (Wire.read() << 8) | Wire.read();
z = (Wire.read() << 8) | Wire.read();
y = (Wire.read() << 8) | Wire.read();
// 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 or incorrect readings from the sensor.
Readings are inconsistent or fluctuate significantly.
Arduino cannot communicate with the sensor.
Q: Can the HMC5883L measure the Earth's magnetic field?
A: Yes, the HMC5883L is designed to measure the Earth's magnetic field and is commonly used in electronic compasses.
Q: How do I calibrate the HMC5883L?
A: Calibration involves rotating the sensor in all directions to collect data and then applying algorithms to correct for distortions. Libraries like "Adafruit HMC5883L" include built-in calibration functions.
Q: Can I use the HMC5883L with a 5V microcontroller?
A: Yes, but you must use a level shifter to step down the 5V logic to 3.3V to avoid damaging the sensor.
Q: What is the maximum range of the HMC5883L?
A: The sensor can measure magnetic fields in the range of ±8 Gauss, which is sufficient for most navigation and orientation applications.