Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

How to Use Adafruit HMC5883L Triple-axis Magnetometer: Examples, Pinouts, and Specs

Image of Adafruit HMC5883L Triple-axis MagnetometerDesign with Adafruit HMC5883L Triple-axis Magnetometer in Cirkit Designer

Introduction

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.

Explore Projects Built with Adafruit HMC5883L Triple-axis Magnetometer

Use Cirkit Designer to design, explore, and prototype these projects online. Some projects support real-time simulation. Click "Open Project" to start designing instantly!
Image of Arduino Uno R3 and HMC5883L Compass Interface: A project utilizing Adafruit HMC5883L Triple-axis Magnetometer in a practical application
Arduino Uno R3 and HMC5883L Compass Interface

This circuit connects an HMC5883L compass module to an Arduino Uno R3 for the purpose of reading magnetic field data. The Arduino is programmed to initialize the compass module, continuously read its X, Y, and Z magnetometer data, and output the readings to the Serial Monitor. The compass module is interfaced with the Arduino via I2C communication, using the SDA and SCL lines, and powered through the Arduino's VIN pin.

Open Project in Cirkit Designer

Common Applications and Use Cases

Technical Specifications

Key Technical Details

Pin Configuration and Descriptions

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)

Usage Instructions

How to Use the Component in a Circuit

  1. Powering the Device:

    • Connect VDD to a 2.16V to 3.6V power source.
    • Connect GND to the ground of your power supply.
  2. I²C Communication:

    • Connect SCL to the I²C clock line on your microcontroller.
    • Connect SDA to the I²C data line on your microcontroller.
  3. Data Ready Pin (Optional):

    • Connect DRDY to a digital input on your microcontroller if you wish to use the data ready feature.

Important Considerations and Best Practices

Example Code for Arduino UNO

#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);
}

Troubleshooting and FAQs

Common Issues Users Might Face

Solutions and Tips for Troubleshooting

FAQs

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.