Arduino Digital Magnetic Compass (HMC5883L - 2 Display Modes)

124K16879

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

Hello!!!

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: http://www.ardumotive.com/digital-compass.html

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: 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 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: youtu.be/i7GaVqMSzMA (make sure to click like button ;-) )

www.ardumotive.com find Greek Arduino Projects

62 Comments

Comments are from so long ago, I am worried if I start there will be no support to get me through.
Since this is my first attempted project, I want to find a community that is more active on a project dealing with compass, display, UNO.
Would it be a mistake to start with this one? All answers appreciated!

Gee, so long. I hope that you do try it out. The original project will work, then you can tweak it to your liking.

I had a great deal of fun making this. I changed it to suit my needs, of course, I used a different magnetometer and I changed the display a bit. I found and fixed a couple of bugs in the code, one of which is very evident in the photos already posted. The testDrawRow (or DrawRow) function is missing a bounds check on x. The result is that the rows are not completed on the right half of the display. For example, the "N" is missing from the right side of this image: <https://content.instructables.com/FS3/LA2X/I7CCG70...> above.

The fix is to add the missing bounds check: if (x < 0) x += 120; Then, when pointing west, the strip correctly displays "N" on the right.
Nice. Bro. Could I use on oled display??

You *could* use an oled display, but the ATMega has too little ram to properly draw graphics. You might try something like a Nano Every.

EDIT

I made one with an oled display. I had to be very frugal with memory use to leave enough memory for the display buffer. But it ran and looked good. Indoors. The oled display is not daylight readable.

How do I solve this problem, it says this on the screen


ARDUMOTIVE
ArduinoBased
compassn.Compass
Michalis Vas

try this instead, it DOES work! :)

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

Thank you! It does compile!
Hi, after opening your "arduino_compass.ino" file, I check the code, and get this message: "Arduino: 1.8.17 Hourly Build 2021/09/06 02:33 (Windows 10), Board: "Arduino Pro or Pro Mini, ATmega328P (5V, 16 MHz)"
FJT7PE9I7CCG6CH:27:40: error: variable 'arrow_bmp' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
static unsigned char PROGMEM arrow_bmp[] ={B00100000, B00100000, B01110000, B01110000, B11111000,};
^
Multiple libraries were found for "SPI.h"
Used: C:\Program Files\arduino-nightly-windows\arduino-nightly\hardware\arduino\avr\libraries\SPI
Not used: C:\Program Files\arduino-nightly-windows\arduino-nightly\libraries\SPI
Multiple libraries were found for "Wire.h"
Used: C:\Program Files\arduino-nightly-windows\arduino-nightly\hardware\arduino\avr\libraries\Wire
Not used: C:\Program Files\arduino-nightly-windows\arduino-nightly\libraries\Wire
exit status 1
variable 'arrow_bmp' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
"
So, what is to be done?
Hey! can i use Hmc5883l to detect electric wires in the wall,and what code should i write for that???
hey! guy i need help i have a problem

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

The circuit board behind the lcd is a homemade arduino uno

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

Hello I need a help for my project .Actually I add all the libraries for lcd, wire and button but sad to say, it doesn't work. I need some advice how to make my project successfull.
Check pinout of Nokia5110.
Depends on where you bought, pin out is different.
If you did not witness any blue light, you connected to wrong pin.

Hi,

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!

More Comments