Introduction: Magnetic Field Measurement Using HMC5883 and Raspberry Pi

The HMC5883 is a digital compass designed for low-field magnetic sensing.This device has a wide magnetic field range of +/-8 Oe, and an output rate of 160 Hz. The HMC5883 sensor includes automatic degaussing strap drivers, offset cancellation, and a 12-bit ADC that enables 1° to 2° compass heading accuracy. All I²C Mini Modules are designed to operate at 5VDC.

In this tutorial we are going to explain the detailed working of HMC5883 with Raspberry pi and its programming using the java programming language.

Step 1: Hardware Required:

Picture of Hardware Required:

Hardware that is required to accomplish the task is as follows:

1. HMC5883

2. Raspberry Pi

3. I2C Cable

4. I2C Shield For Raspberry Pi

5. Ethernet Cable

Step 2: Hardware Hookup:

Picture of Hardware Hookup:

The hardware hookup section basically explains the wiring connections required between the sensor and the raspberry pi. Ensuring correct connections is the basic necessity while working on any system for the desired output. So, the requisite connections are as follows:

The HMC5883 will work over I2C . Here is the example wiring diagram, demonstrating how to wire up each interface of the sensor.

Out-of-the-box, the board is configured for an I2C interface, as such we recommend using this hookup if you’re otherwise agnostic. All you need is four wires!

Only four connections are required Vcc, Gnd, SCL and SDA pins and these are connected with the help of I2C cable.

These connections are demonstrated in the pictures above.

Step 3: Java Code to Measure Magnetic Field Intensity:

Picture of Java Code to Measure Magnetic Field Intensity:

The advantage of using raspberry pi is, that is provides you the flexibility of the programming language in which you want to program the board in order to interface the sensor with it. Harnessing this advantage of this board, we are demonstrating here its programming in the Java. The java code for HMC5883 can be downloaded from our github community that is Control Everything Community.

As well as for the ease of the users, we are explaining the code here also:

As the first step of coding you need to download the pi4j library in case of java, because this library supports the functions used in the code. So, to download the library you can visit the following link:

You can copy the working java code for this sensor from here also:




public class HMC5883


public static void main(String args[]) throws Exception


// Create I2C bus

I2CBus Bus = I2CFactory.getInstance(I2CBus.BUS_1);

// Get I2C device, HMC5883 I2C address is 0x1E(30)

I2CDevice device = Bus.getDevice(0x1E);

// Select Configuration register A

// Normal measurement configuration, data rate o/p = 0.75 Hz

device.write(0x00, (byte)0x60);

// Select Mode register

// Continuous measurement mode

device.write(0x02, (byte)0x00);


// Read 6 bytes of data from 0x03(3)

// xMag msb, xMag lsb, zMag msb, zMag lsb, yMag msb, yMag lsb

byte[] data = new byte[6];, data, 0, 6);

// Convert the data

int xMag = ((data[0] & 0xFF) * 256 + (data[1] & 0xFF));

if(xMag > 32767)


xMag -= 65536;

} int zMag = ((data[2] & 0xFF) * 256 + (data[3] & 0xFF));

if(zMag > 32767)


zMag -= 65536;


int yMag = ((data[4] & 0xFF) * 256 + (data[5] & 0xFF));

if(yMag > 32767)


yMag -= 65536;


// Output data to screen

System.out.printf("Magnetic field in X-Axis : %d %n", xMag);

System.out.printf("Magnetic field in Y-Axis : %d %n", yMag);

System.out.printf("Magnetic field in Z-Axis : %d %n", zMag);



Write() and read() functions are used to write the commands and read the sensor output respectively. Following part illustrates the reading of magnetic field values.

 // Read 6 bytes of data from 0x03(3) 

// xMag msb, xMag lsb, zMag msb, zMag lsb, yMag msb, yMag lsb

byte[] data = new byte[6];, data, 0, 6);

The output is shown in the picture above.

Step 4: Applications:

Picture of Applications:

HMC5883 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. Its one to two degree high level accuracy and precision enables Pedestrian Navigation and LBS Applications.