Home / Projects / Arduino Projects / Arduino Compass with HMC5883L Magnetometer

# Arduino Compass with HMC5883L Magnetometer

In general terms, a magnetometer measures the magnetism of objects or places. In fact, a compass is a type of magnetometer. This project demonstrates how to use the HMC5883L magnetometer as a compass with an Arduino microcontroller.

### Hardware Setup

I have setup this project on a mini breadboard using the parts:

The HMC5883L magnetometer is fairly common but for this project, I used the 10 DOF IMU from DFRobot. This module contains not only the HMC5883L but also a BMP280 barometric pressure sensor, ITG-3205 gyroscope and ADXL345 accelerometer into one device. Note that the ordinary HMC5883L magnetometer module will still work with this project. Moreover, the Nokia 3310/5110 LCD is used for display and the Arduino Pro Mini is the brains of the project.

### Needed Libraries

For this project's sketch to run, you'll need the following libraries:

You can learn more about using the Nokia 3310/5110 LCD and its libraries in a separate article.

### Developing the Project

#### Wiring

The project is not that hard because of the readily available libraries. The wiring is also straightforward:

Nokia LCD to Pro Mini Connection:

Nokia 3310 LCDArduino Pro Mini
1 (RST)D3
2 (CE)D4
3 (DC)D5
4 (DIN)D6
5 (CLK)D7
6 (VCC)VCC
7 (LIGHT)GND
8 (GND)GND

HMC5883L to Pro Mini Connection:

HMC5883LArduino Pro Mini
SCLA5
SDAA4
VINVCC
GNDGND

#### Reading Data from HMC5883L

The first step was to make sure the HMC5883L is giving out the correct heading. According to the example provided in the HMC5883L library, the range, measurement mode, data rate and samples must be set before reading out data from the magnetometer. Thus, I created this function:

The library has a neat begin() function which checks out if the sensor is connected or not, which I used as seen above.

#### Correcting for Magnetic Declination

Heading is the term used to measure your position from the north pole. North pole is actually the north geographic pole while compasses detect the north magnetic pole which is a different place although not that far from the other north pole. The distance between each pole is really not the issue; it's the magnetic declination between the two. Thus, there's a need to correct for this declination but first you must find out your area's declination through here.

For example, the magnetic declination in our area is - 0° 53' (negative 0 degrees and 53' minutes). This declination must be converted into radians:

$declination = -(0 + 53/60) * \pi / 180 = -0.0154$

Let's say the reading of the compass is 2.0 radians. The correct heading, accounting declination, is now:

$correctheading = 2 + (-0.0154) = 1.9846\: rad$

#### Drawing the Compass

Now that I have the correct heading, the next part is to display that heading on the LCD. Showing the heading as a number is too easy; I wanted an arrow that will always point to the north like a real compass.

The GFX library I pointed out above has a line function:

Here, the x0, y0 are the coordinates of the origin of the line while x1, y1 are those of the end point.

Specifying the origin of the arrow for the compass is just writing the center of the LCD which is (41, 23). The end point of the line is based on the heading. To illustrate:

The end point could be computed as:

$x = rsin\theta\;;y=rcos\theta$

In geometry, coordinate (0,0) is at the center. But here, our center is at (41, 23) thus the axes must be translated. Moreover, I wanted to draw the North line, not the heading line. Finally, the direction of the arrow must be corrected according to how the HMC5883L board is connected to my breadboard.

### Full Sketch

Here is now the full Arduino Sketch for this project:

Here you can see how I calculated the heading in degrees accounting magnetic declination and how I drew a line that always point north. I had to make the line thicker so I needed to draw the line three times with each coordinates incremented by a pixel. Also, I drew a circle to make it more compass-like.

### Video

Here's the demonstration of the project:

Note that the HMC5833L must be calibrated before acquiring a more stable data. To calibrate, make figure 8's with the device.

That's it! Feel free to comment if you have any questions about this project. Happy building!

## Wireless Controller for Bluetooth RC Car

This project demonstrates how two bluetooth modules connect with each other. I’ll be featuring DFRobot’s …

### 7 comments

1. George Pasion Jr.

Sir how to do it if we want the N needle to be a cross which display the W E and S too? Please help us sir.

2. Hi, Thanks for sharing! I have one issue, cannot find Vector when compiling. What header am I missing?

• Sorry!
My mistake. I can answer my own question, Vector is part of DFRobot_QMC5883.h. I was looking to use an LSM303, to get tilt compensation.

Thanks,

Dave.

3. hi , thank you for sharing , it is very good , now , how to run a fluxgate magnometer sensor ( fg3 or flc100 or ems100) with ardunio and send data to pc ? thank you

4. hi, thank you for sharing , I want run a flc100 fluxgate magnometer sensor with ardunio nano , can yuo help me ? thank you

This site uses Akismet to reduce spam. Learn how your comment data is processed.