HMC5883 sensor and LPC1768 example

The Honeywell 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-theart, 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 I2C serial bus allows for easy interface. The HMC5883L is a 3.0×3.0x0.9mm surface mount 16-pin leadless chip carrier (LCC). Applications for the HMC5883L include Mobile Phones, Netbooks, Consumer Electronics, Auto Navigation Systems, and Personal Navigation Devices.

The HMC5883L utilizes Honeywell’s Anisotropic Magnetoresistive (AMR) technology that provides advantages over other magnetic sensor technologies. These anisotropic, directional sensors feature precision in-axis sensitivity and linearity.

These sensors’ solid-state construction with very low cross-axis sensitivity is designed to measure both the direction and the magnitude of Earth’s magnetic fields, from milli-gauss to 8 gauss. Honeywell’s Magnetic Sensors are among the most sensitive and reliable low-field sensors in the industry

Parts List

Label Part Type Properties
MBED1 mbed LPC1768
Part1 Triple Axis Magnetometer – HMC5883 Breakout tilt compensated No




I imported the library and example from the following location to use in the MBed online compiler –

#include "mbed.h"
#include "HMC5883L.h"
HMC5883L compass(p9, p10);
int main(void)
    int16_t data[3] = {0};
    double heading = 0.0f;
        heading = compass.getHeadingXYDeg();
        printf("x: %4d, y: %4d, z: %4d\r\n", data[0], data[1], data[2]);
        printf("heading: %3.2f\r\n", heading);



Using a terminal program such as Teraterm you should see something like the following

x: -404, y: 119, z: -318
heading: 157.04
x: -20, y: 463, z: -36
heading: 81.49
x: 539, y: 212, z: 160
heading: 0.00
x: -450, y: -6, z: -233
heading: 176.32
x: 207, y: 459, z: 22
heading: 69.76
x: 126, y: -454, z: -248
heading: 290.98
x: 297, y: 54, z: 485
heading: 12.32
x: 645, y: -144, z: 96
heading: 350.27
x: 170, y: -34, z: 481
heading: 319.64
x: 102, y: -272, z: 431
heading: 290.19
x: 88, y: -278, z: 424
heading: 287.38
x: 80, y: -280, z: 421
heading: 285.65

The values change as I move the sensor about



HMC5883 GY-271 3V-5V Triple Axis Compass Magnetometer Sensor Module For Arduino