Magnetic Field Measurement Using HMC5883 and Raspberry Pi

Introduction: Magnetic Field Measurement Using HMC5883 and Raspberry Pi

About: We are a group of makers. We work in IoT, IOS app, android app, embedded design, sensor design, raspberry pi, arduino, beaglebone, particle electron, particle photon, Bluetooth.

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:

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:

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:

The advantage of using raspberry pi is, that 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 it's programming in Java. The java code for HMC5883 can be downloaded from our github community that is Dcube Store.

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:

<p>import;</p><p>import;</p><p>mport;</p><p>import;<br>public class HMC5883</p><p>{	</p><p>public static void main(String args[]) throws Exception	</p><p>{		</p><p>// Create I2C bus		</p><p>I2CBus Bus = I2CFactory.getInstance(I2CBus.BUS_1);		</p><p>// Get I2C device, HMC5883 I2C address is 0x1E(30)		</p><p>I2CDevice device = Bus.getDevice(0x1E);</p><p>// Select Configuration register A		</p><p>// Normal measurement configuration, data rate o/p = 0.75 Hz 		</p><p>device.write(0x00, (byte)0x60);		</p><p>// Select Mode register		</p><p>// Continuous measurement mode		</p><p>device.write(0x02, (byte)0x00);		</p><p>Thread.sleep(500);</p><p>// Read 6 bytes of data from 0x03(3)		</p><p>// xMag msb, xMag lsb, zMag msb, zMag lsb, yMag msb, yMag lsb		</p><p>byte[] data = new byte[6];		</p><p>, data, 0, 6);</p><p>// Convert the data		</p><p>int xMag = ((data[0] & 0xFF) * 256 + (data[1] & 0xFF));		</p><p>if(xMag > 32767)		</p><p>{			</p><p>xMag -= 65536;		</p><p>}
int zMag = ((data[2] & 0xFF) * 256 + (data[3] & 0xFF));		</p><p>if(zMag > 32767) 		</p><p>{			</p><p>zMag -= 65536;		</p><p>}
		</p><p>int yMag = ((data[4] & 0xFF) * 256 + (data[5] & 0xFF));		</p><p>if(yMag > 32767) 		</p><p>{			</p><p>yMag -= 65536;		</p><p>}</p><p>// Output data to screen		</p><p>System.out.printf("Magnetic field in X-Axis : %d %n", xMag);		</p><p>System.out.printf("Magnetic field in Y-Axis : %d %n", yMag);		</p><p>System.out.printf("Magnetic field in Z-Axis : %d %n", zMag);	</p><p>}</p><p>}</p>

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) 
<p>// xMag msb, xMag lsb, zMag msb, zMag lsb, yMag msb, yMag lsb	</p><p>byte[] data = new byte[6];	</p><p>, data, 0, 6);</p>

The output is shown in the picture above.

Step 4: 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.

Be the First to Share


    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge
    • Make It Bridge

      Make It Bridge
    • Big and Small Contest

      Big and Small Contest