

The HMC5883L is a digital magnetometer manufactured by Adafruit (Part ID: 1746). It is designed to measure magnetic fields in three dimensions (X, Y, and Z axes) and is widely used in navigation, orientation, and compass-based applications. This compact and versatile sensor communicates via I2C, making it easy to integrate into microcontroller-based systems such as Arduino and Raspberry Pi.








The HMC5883L is a high-performance magnetometer with the following key specifications:
| Parameter | Value |
|---|---|
| Manufacturer | Adafruit |
| Part ID | 1746 |
| Operating Voltage | 3.0V to 5.0V |
| Communication Interface | I2C |
| Measurement Range | ±1.3 to ±8.1 Gauss |
| Resolution | 12-bit ADC |
| Output Data Rate | 0.75 Hz to 75 Hz |
| Operating Temperature | -40°C to +85°C |
| Dimensions | 14.8mm x 12.0mm x 3.5mm |
The HMC5883L module typically has 4 pins for interfacing:
| 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:
Below is an example Arduino sketch to read data from the HMC5883L and display it 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(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) {
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 values to the Serial Monitor
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
}
| Issue | Solution |
|---|---|
| No data or incorrect readings | Check wiring connections and ensure proper I2C address (0x1E). |
| Sensor not detected by Arduino | Verify pull-up resistors on SDA and SCL lines. |
| Inconsistent or fluctuating measurements | Calibrate the sensor and avoid magnetic interference. |
| Arduino hangs during data reading | Ensure proper I2C communication and check for loose connections. |
Q: Can the HMC5883L operate at 3.3V?
A: Yes, the HMC5883L supports an operating voltage range of 3.0V to 5.0V, making it compatible with both 3.3V and 5V systems.
Q: How do I calibrate the HMC5883L?
A: Calibration involves rotating the sensor in all directions to collect raw data and then applying an offset correction. Libraries like Adafruit's HMC5883L library can simplify this process.
Q: What is the maximum range of the HMC5883L?
A: The sensor can measure magnetic fields up to ±8.1 Gauss, depending on the selected gain setting.
Q: Can I use the HMC5883L for tilt compensation?
A: Yes, but you will need an accelerometer or gyroscope to calculate tilt angles and combine the data for accurate compensation.
By following this documentation, you can effectively integrate and utilize the HMC5883L Triple-axis Magnetometer in your projects.