Arduino Digital Magnetic Compass – HMC5883L

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.

Arduino Digital Magnetic Compass

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!

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: youtu.be/CdjUUo_owIc

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 codeThis 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

Source: Arduino Digital Magnetic Compass – HMC5883L

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top