Introduction: Magnetic Field Measurement Using HMC5883 and Arduino Nano

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 Arduino nano.

Step 1: Hardware Required:

The materials that we need for accomplishing our goal includes the following hardware components:

1. HMC5883

2. Arduino Nano

3. I2C Cable

4. I2C Shield For Arduino Nano

Step 2: Hardware Hookup:

The hardware hookup section basically explains the wiring connections required between the sensor and the arduino nano. 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: Arduino Code to Measure Magnetic Field Intensity:

Let us start with the Arduino code now.

While using the sensor module with the Arduino, we include Wire.h library. "Wire" library contains the functions which facilitate the i2c communication between the sensor and the Arduino board.

The entire Arduino code is given below for the convenience of the user:

<p>#include<Wire.h></p><p>// HMC5883 I2C address is 0x1E(30)</p><p>#define Addr 0x1E
void setup()</p><p>{  </p><p>// Initialise I2C communication as MASTER  </p><p>Wire.begin();  </p><p>// Initialise Serial Communication, set baud rate = 9600  </p><p>Serial.begin(9600);    </p><p>// Start I2C Transmission  </p><p>Wire.beginTransmission(Addr);  </p><p>// Select configure register A  </p><p>Wire.write(0x00);  </p><p>// Set normal measurement configuration, data output rate = 0.75Hz  </p><p>Wire.write(0x60);  </p><p>// Stop I2C Transmission  </p><p>Wire.endTransmission();</p><p>// Start I2C Transmission  </p><p>Wire.beginTransmission(Addr);  </p><p>// Select Mode register  </p><p>Wire.write(0x02);  </p><p>// Set continuous measurement  </p><p>Wire.write(0x00);  </p><p>// Stop I2C Transmission  </p><p>Wire.endTransmission();  </p><p>delay(300);</p><p>}
void loop()</p><p>{  </p><p>unsigned int data[6];  </p><p>// Start I2C Transmission  </p><p>Wire.beginTransmission(Addr);  </p><p>// Select data register  </p><p>Wire.write(0x03);  </p><p>// Stop I2C Transmission  </p><p>Wire.endTransmission();</p><p>// Request 6 bytes of data  </p><p>Wire.requestFrom(Addr, 6);    </p><p>// Read 6 bytes of data  </p><p>// xMag msb, xMag lsb, zMag msb, zMag lsb, yMag msb, yMag lsb  </p><p>if(Wire.available() == 6)  </p><p>{    </p><p>data[0] = Wire.read();    </p><p>data[1] = Wire.read();    </p><p>data[2] = Wire.read();    </p><p>data[3] = Wire.read();   </p><p>data[4] = Wire.read();    </p><p>data[5] = Wire.read();  </p><p>}  </p><p>delay(300);          </p><p>// Convert the data   </p><p>int xMag = ((data[0] * 256) + data[1]);  </p><p>int zMag = ((data[2] * 256) + data[3]);  </p><p>int yMag = ((data[4] * 256) + data[5]);    </p><p>// Output data to serial monitor  </p><p>Serial.print("Magnetic Field in X-Axis : ");  </p><p>Serial.println(xMag);  </p><p>Serial.print("Magnetic Field in Y-Axis : ");  </p><p>Serial.println(yMag);  </p><p>Serial.print("Magnetic Field in Z-Axis : ");  </p><p>Serial.println(zMag);  </p><p>delay(300);</p><p>}</p>

In wire library Wire.write() and Wire.read() is used to write the commands and read the sensor output. Following part of the code illustrates the reading of sensor output.

<p>// Read 6 bytes of data  <br>// xMag msb, xMag lsb, zMag msb, zMag lsb, yMag msb, yMag lsb  
if(Wire.available() == 6)  
{    
data[0] = Wire.read();    
data[1] = Wire.read();    
data[2] = Wire.read();    
data[3] = Wire.read();   
data[4] = Wire.read();    
data[5] = Wire.read();  
}</p>

Serial.print() and Serial.println() is used to display the output of the sensor on the serial monitor of the Arduino IDE.

The output of the sensor 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.