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

Picture of 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

Picture of 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

Picture of 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

Picture of 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


Nishkotak (author)2017-01-17


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

JasonR4 (author)2016-11-30

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

jerry.ericsson2 (author)2016-03-18

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!

JasonR4 (author)jerry.ericsson22016-11-30

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.

jerry.ericsson2 made it! (author)2016-08-07

I made it, and it works good for me. Perhaps a bit slow in reaction to movements but I don't think I got the sensor level. All wiring was done with cat-5 scrap wire. I used a nano instead of uno to save space, and mounted it on a perfboard, a second perfboard was used for the switch and buttons, all mounted in a small black project box, the front being cut out with a knife to allow the screen mounting. The board and sensor are held in place with hot glue, screen mounted with two stand offs (I only had two of that size) and I am using a USB cable for power, and in case I need to adjust the programing.

jerry.ericsson2 (author)2016-08-05

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!

stuartyh (author)2016-06-30

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


jonastephenson (author)2016-04-02

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.


jonastephenson (author)2016-03-26

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

Sanjana H K (author)2016-03-11

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.

cndg (author)2016-02-17

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?

ES1GB. (author)2015-10-16

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

mtaha4 (author)ES1GB.2016-01-26

can u send me the code?

JeffR9 (author)2015-09-21

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.

nodoubtman (author)2015-06-27

try this instead, it DOES work! :)

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

Thanks for this comment!

YuriMat (author)2015-08-28

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:

Ardumotive_com (author)YuriMat2015-09-01

Thanks! ;)

nageshsp (author)2015-06-11

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?

nodoubtman (author)nageshsp2015-08-20

hey dude? Did you see my solution?


Ardumotive_com (author)nageshsp2015-06-15

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

PinoPollera (author)2015-04-17

I tried to make the compass but by this error, which means?


Arduino: 1.6.3 (Windows 7), Scheda: "Arduino Uno"

arduino_compass.ino: 27: 40: errore: 'arrow_bmp' variabile deve essere const per essere messo in sola lettura sezione attraverso '__attribute __ ((progmem))' Errore Durante la COMPILAZIONE

nodoubtman (author)PinoPollera2015-06-27

see up :)

Install additional libraries first.

Murpheus! (author)Ardumotive_com2015-05-12

I have the same problem...!!!
I have reinstall the Libraries again about 4 times and the same error again and again..!!!
What do you mean "Install additional libraries first."..??
If you want you can send me pm in Greek..!!

Ardumotive_com (author)Murpheus!2015-05-13

You must copy paste all libraries that you will find in "" file. If you did this step and you still take this error I can't help you. Something is going wrong with your lcd 5110 and library. Try to search google about this error.

Murpheus! (author)Ardumotive_com2015-05-15

Thanks for the reply..!!
i did it..!!and still the same error....!!
the LCD 5110 works ok..with demo..!!
Adafruit_PCD8544 is the Library for 5110 that you give!
Google does not give an answer about that.
What version of arduino ide you use? ex. 1.6.3??

Installed, it does not work anyway.

Did you install all libaries? Below someone else has the same problem and when finally install (all) libraries, problem solved.

kyokorn (author)2015-06-02

Arduino: 1.6.4 (Windows 8.1), Board: "Arduino Nano, ATmega328"

arduino_compass.ino:10:20: fatal error: Button.h: No such file or directory
compilation terminated.
Error compiling

I have in the same place all the libraries, but only say error with Button.h

Ardumotive_com (author)kyokorn2015-06-15

Hi! You must install "Button" library too.

eskai.kenshin (author)2015-04-28

Sir..I would like to ask a link examples to make arduino based circuit..I want to try to make..Thank you :)

Hello, you can find more projects and examples with arduino uno here (Greek) and here (English).

English Ardumotive is under developing and it will have more tutorials and projects in English very soon, so stay online! ;)

Tip: You can translate Greek ardumotive by using google chrome, google translate or bing translate :D

lacornique (author)2015-04-20


TKS for sharing this is what I am looking for.

Unfortunately when rotating the sensor far from magnetic devices, and 90° at once, the displlay does not show 90° variation but something like





And the needle is wrong too.

Declination is set for my location in the sketch.

I use the Libraries from this site

Can you please help ?



Just make sure that HMC5883L is on flat surface.

If you watch my video you will see that works fine ;)

TKS for prompt answer. It is on a flat surface.

Generaly problems with arduino come from unadequate libraries.

Would you be so kind to provide links to the right ones



Try to re-install all libraries. You can find a download link on step 3. Let me know if you succeed ;)


Finaly I found I have to calibrate the HMC5883.

I used the infos found here:

For instance I had to modify X and Y offset and now it is allright.

I tried with two HMC5883 and they have different offsets.

TeguhI (author)2015-04-19

I need a module , any module can be sent via email


thank you

lemarikoleksi (author)2015-04-08

i will try make this project but I dont understand with this circuit behind the LCD, i can not find in this tutorial, what the function of that circuit

Hi! It's Arduino based circuit. You can use Arduino uno! ;)

marc.deloor.5 (author)2015-04-06

Tried x-times before to make a working compass of that MHC, never worked...

Most had to be calibrated as soon as I left my desk. Gonna try this one out! Thanks for sharing!

TeguhI (author)2015-04-03

This program error sir,,, help
arduino_compass.ino:27:40: error: variable 'arrow_bmp' must be const in order to be put into read-only section by means of '__attribute__((progmem))'

Error compiling.

Ardumotive_com (author)TeguhI2015-04-04

Install additional libraries first.

TeguhI (author)Ardumotive_com2015-04-04

done sir :)

TeguhI (author)2015-04-01

where diagram blok Arduino Digital Magnetic Compass..?

Ardumotive_com (author)TeguhI2015-04-04

You can find breadboard circuite schematic on step 2.

u971 (author)2015-03-27

Easy to make looks cool,thanks for the idea.

tomatoskins (author)2015-03-18

This is awesome! I would have never guessed that this could be done!

Thanks!!! In the world of arduino, all are possible! :D

About This Instructable




More by Ardumotive_com:MIKE - Arduino Line Following Robot for Beginners "GRECO" - Arduino Object Avoiding Robotfor BeginnersArduino 3D Printed Digital Thermometer With DHT-22 Sensor
Add instructable to: