Arduino Digital Magnetic Compass (HMC5883L - 2 Display Modes)





Introduction: Arduino Digital Magnetic Compass (HMC5883L - 2 Display Modes)


Today I will show you how to make your own arduino compass by using the MHC5883L 3-axis digital compass board.

Before you start, make sure that you have:

  • Arduino uno
  • HMC5883L sensor board
  • Nokia 5110 display (pcb version)
  • 2x push-button
  • 1x on/off switch

(you can use breadboard to build it easier)

I prefer to build my own arduino based circuit by using the ATmega328p uno possessor, but this is optional for you. Buttons are used for turn on or off display leds and to change display mode of compass.

Official page:

Watch video on YouTube:

Step 1: About HMC5883L Board

The HMC5883L sensor is a 3-axis digital magnetometer IC designed for low-field magnetic sensing. The sensor has a full-scale range of +8 to -8 Gauss and a resolution of up to 5 milli-Gauss. Communication with the HMC5883L is simple and all done through an I2C interface. That means you will need to connect power, ground and only two cables to Arduino uno board (SDA,SCL).

The HMC5883L board can be powered up by 5V or 3.3V pins of Arduino uno board. No need to add any capacitors or resistors to your circuit.

Pinout and connection with uno:

  • Vcc to 5V or 3.3V
  • GND to GND
  • SDA to A4
  • SCL to A5
  • DRDY to nothing...

Because this is a magnetic compass if you put it near to battery, motors, metallic surface or magnetic field, the result will diverge from the actual. Also make sure to use it on a flat surface!

Step 2: Breadboard Schematic Circuit

Here is the circuit on breadboard schematic. Make sure to read "pinout" notes on image and connect lcd and sensor correct with arduino uno.

Step 3: Libraries and Code

Before open code file "arduino_compass.ino" install the necessary libraries to arduino ide libraries folder.

Just open the libraries.rar file and extract / copy all files to libraries folder.

If you have windows os and you don't know where this folder is, watch this video on YouTube:

For some parts of code, I want to thank Markus Ulsab and Galin Dimitrov who shared their own arduino compass projects.

What I did?

  • Changed Markus project to support HMC5883L sensor
  • Combined two projects for "two display modes" function
  • Correct some minor bugs
  • Add comments in my code

Step 4: Building the 'box' - Tips

This is an optional procedure!

To build the box i used one thin balsa sheet. You can easily cut it with a small cutter and paint it with marker.

See the images above to take an idea, but keep in mind that you can design and build your own box.

If you will use a battery, do not put it near HMC5883L sensor.

Some tips:

  • Make marks of components on balsa sheet
  • Cut carefully with a small cutter - Be patient and make sure that you will not cut your self!
  • Paint it with your favorite color marker
  • Put components on it
  • Use some glue to close the box

Watch video on YouTube: (make sure to click like button ;-) ) find Greek Arduino Projects

3 People Made This Project!


  • Clocks Contest

    Clocks Contest
  • Make it Move Contest

    Make it Move Contest
  • Oil Contest

    Oil Contest

We have a be nice policy.
Please be positive and constructive.





Will the magnetic compass sensor work for the detection of strokes made by a pen. strokes like vertical, horizontal, back slash, forward slash, etc.??

For those of you having issues getting the 5110 to display, change the pins to

12 SCLK, 11 DIN, 10 D/C, 9 CS, 8 RST

Finally got it to upload to my Uno but nothing on the screen? Very frustrated, I think I shall search for one that I can get to work!

1 reply

Change the 5110 pinout to Adafruit_PCD8544 display = Adafruit_PCD8544(12, 11, 10, 9, 8);

Seems to be conflicting with something using the other pinout.

For those who are still batteling this to make it work, I found the error that I was having, and now I can get it working just fine: I looked above the error and found that the Arduino App was using an old version of the library files. When I went and removed the old versions (based on the create date) and the old version of the Adafruit sensor library things began working once again. Guess that's what I get for always transfering my librarary files to new computers when I set them up. Thanks a million to the fellow who has taken so much flack over his wonderful work, this compass will be installed in a project box and mounted in our Class A Motor Home that we spend the winters, and much of the Spring/Fall months traveling in. For some strange reason the unit came with no compass on board. Perhaps it is the fact that she is 12 years old, who knows. Anyhow thanks again, you do great work!

I recently downloaded and tried compiling the sketch but all I am getting are the following errors.

(sketch file) Digital Compass Dual Display.ino:25:27: error: use of undeclared identifier 'PULLUP'
Button buttonA = Button(2,PULLUP); //Button to turn on/off display light

(sketch file) Digital Compass Dual Display.ino:26:27: error: use of undeclared identifier 'PULLUP'
Button buttonB = Button(8,PULLUP); //Button for changing display mode

Can you please help


I emailed the author and got a reply that he knows about a couple of bug but has not got around to fix then... So I did !!!!

I'll post all on my blog later today.


Well it looked like a nice simple project... but hell..I even make the lib's separate like this: #include "j:/compass/SPI/SPI.h" .

But still get so many errors I cannot even load it..


In file included from C:\Users\John\Documents\Arduino\arduino_compass\arduino_compass.ino:13:0:

j:/compass/Adafruit_HMC5883_U/Adafruit_HMC5883_U.h:99:10: error: conflicting return type specified for 'virtual void Adafruit_HMC5883_Unified::getEvent(sensors_event_t*)'

void getEvent(sensors_event_t*);



This is just one error..

I'm using IDE v 1.6.8

I am using HMC5883L for my project. I have interfaced it
with the arduino and executed the program given in the website. But the problem is it is
showing the same angle (66.68) when I rotate & if I do it in other places I
am facing the same problem. I have also added declination angle of that place.
So please help me how to make it work properly.


The compass board photo does not match the wiring diagram, and it's also different from the one in the photos of the device you built!

These things also say they require 3.3volts... hooking them to 5 will supposedly destroy them? Did you use a logic converter?

Made it, got it working, thanks to you, nodoubtman.

1 reply

can u send me the code?

I need a little clarification. Your parts lists shows a Arduino-Uno board, Nokia display and a HMC5883L magnotometer . What is the board you show connected to the Display LCD? I have no experience with Arduino.

try this instead, it DOES work! :)

const static unsigned char PROGMEM arrow_bmp[] =
{ B00100000, B00100000, B01110000, B01110000, B11111000};

1 reply

Nice compass!

Ardumotive_com, try
to calibrate your HMC5883L. Without calibration the measurement of the
magnetic field vector is not accurate (especially if your location is
the north or south of the Earth).

See my instructable about it:

1 reply

I installed all libraries into arduino folder but still not working. Below is the error

arduino_compass.ino:27:40: error: variable 'arrow_bmp' must be const
in order to be put into read-only section by means of

Error compiling.

Any solution? or any other similar code?

2 replies

hey dude? Did you see my solution?


I believe that is a problem with LCD and library. Try to search web to find a library that is compatible with your lcd screen. Thanks