How to use HMC5883L Compass Sensor

HMC5883L is a 3-Axis Digital Compass made by Honeywell. Three-axis means this sensor can detect movement on X, Y, Z-axis or usually we called that 3 axis magnetometer. So it’s not only the direction in the degree that we are able to extract.

In this tutorial, we will learn how to use the HMC5883L sensor module. From the datasheet, it said that this sensor has

  • 12 bit ADC
  • 1-2 degrees compass heading accuracy
  • I2C communication
  • 160Hz max data rate
  • Range of -8 to +8 Gauss

This module is GY-273. We can see on the center the HMC5883L IC. This module already has SCL and SDA pins. So this is an I2C sensor module.

Wiring Diagram

HMC5883L with arduino
HMC5883L with arduino

As always, it’s so easy to use an I2C device with an Arduino. Just connect the power pins that are VCC to 5V GND to GND and the SDA to A4 and SCL to A5. For a clearer view, this is the wiring diagram.

Library and Code

If wiring is completed now open the Arduino IDE. First, you need to download the library. Type HMC5883L on library manager and choose the HMC5883L library by adafruit. Choose the last version and click install. After that, you should be able to open the example file in the library. Choose the example file named mag sensor.

Or you can download the library manually here.

You don’t need to edit anything just upload, and after the upload is completed. Open the serial monitor.

HMC5883L data on serial monitor

As we can see on the serial monitor. We got the values, they are X, Y, Z, and of course the heading degree.

You can try to move the sensor and you should get the data according to the sensor movement.

If The sensor always return 0 data

If the sensor data always return 0. There’s a big change that your sensor is actually QMC5883L but labeled as HMC5883L. Try using this tutorial to solve this problem.

Video Tutorial