The Adafruit HMC5883L is a surface-mount, multi-chip module designed for low-field magnetic sensing with a digital interface for applications such as low-cost compassing and magnetometry. The HMC5883L includes our state-of-the-art, high-resolution HMC118X series magneto-resistive sensors plus an ASIC containing amplification, automatic degaussing strap drivers, offset cancellation, and a 12-bit ADC that enables 1° to 2° compass heading accuracy. The I²C serial bus allows for easy interface.
Pin Number | Name | Description |
---|---|---|
1 | VDD | Power supply (2.16V to 3.6V) |
2 | GND | Ground |
3 | SCL | Serial Clock Line for I²C |
4 | SDA | Serial Data Line for I²C |
5 | DRDY | Data Ready (optional use) |
Powering the Device:
I²C Communication:
Data Ready Pin (Optional):
#include <Wire.h>
#include <HMC5883L.h>
HMC5883L magnetometer;
void setup() {
Serial.begin(9600);
Wire.begin();
// Initialize HMC5883L
magnetometer.initialize();
// Verify connection
if (magnetometer.testConnection()) {
Serial.println("HMC5883L connected");
} else {
Serial.println("HMC5883L connection failed");
}
}
void loop() {
// Read magnetometer values
int16_t mx, my, mz;
magnetometer.getHeading(&mx, &my, &mz);
// Output the results via the serial port
Serial.print("X: "); Serial.print(mx);
Serial.print(" Y: "); Serial.print(my);
Serial.print(" Z: "); Serial.println(mz);
delay(500);
}
Q: What is the I²C address of the HMC5883L? A: The default I²C address of the HMC5883L is 0x1E.
Q: Can the HMC5883L be used with a 5V microcontroller? A: Yes, but a level shifter is recommended for the I²C lines to ensure compatibility with the HMC5883L's voltage levels.
Q: How do I know if the HMC5883L is functioning correctly?
A: Run the testConnection()
method in the setup routine to verify communication with the device. If it returns true, the device is functioning correctly.
Q: How often should I calibrate the magnetometer? A: Calibration should be performed whenever the device is placed in a new environment or if there is a significant change in the surrounding magnetic field.