Introduction: HMC5883L Magnetometer With Level Shifter on Arduino Nano

About: Tinkering with digital since 1982

This will get you sensing compass orientation in 3 dimensions in your arduino projects for less than $4!

Did you know: these things are so sensitive, you can use them for highly accurate position sensing with 6-degrees-of-freedom? I'm attaching one to each finger (on gloves), so I can use all 10 fingers in 3D on my Samsung Gear VR headset. I got the idea from my antique (>20 years old now, thus, not patented anymore!) "ascension flock of birds" 3D headset; it's got 3 electromagnets in it, oriented in the X, Y, and Z planes, and it "energises" each of those coils in turn, then reads the resulting changes from the magentometers, which gives you X, Y, Z, Pitch, Roll and Yaw!

I like cheap stuff. The high-res photo you see (click to enlarge) shows an el-cheapo eBay arduino nano compatible (chinese knockoff; ~$2) on the left, an el-cheapo 3v3 to 5v bi-directional level shifter in the middle (99cents), and an el-cheapo 3 axis magnetometer on the right (99cents).

Nowhere on the internet was I able to find out how to hook up this exact Magentometer! So I decided to fix that here!

Weirdly, also nowhere on the internet was I able to easily find how to use this level shifter either... it seems to be an unusual design, so again, this instructable solves that problem as well!

For those unaware - if you hook a 3.3volt thing up to a 5volt arduino, you will destroy the 3v3 thing - which is why we need the level shifter.

Google and you might want to check the part numbers etc on these things - mine are labelled and came in packages reading: IIC I2C SM-BI-578 15/20 SM4116 | 2015-07-20 16:05:32 | 20150717142148371iuy anf ang (level shifter)

And HMC5883L (magentometer)

Step 1: Since There Seems to Be No Place Showing Either This Shifter or Compass, I Thought I'd Put Them Up!

This is what the back of the Compass / Magentometer looks like.

The SDA and SCL are I2C bus lines - they connect to the pins on your arduino which are labelled that exact same way, BUT NOT DIRECTLY - your arduino is 5v, these are 3.3volt ones (aka 3v3), so you have to go through the level shifter.

Step 2: Copy My Wiring!

The photo in my heading is clear enough to see what I have done.

Visit these URLs to learn more about what's going on:

Magnetometer datasheet: Datasheet: http://cdn.sparkfun.com/datasheets/Sensors/Magnet...

More about a similar http://elinux.org/Sparkfun:_HMC5883L_Magnetometer

3v3 to 5v logic level converter:- http://letsmakerobots.com/blog/unixguru/running-b...

Bad example (has 3 different magnetometers, and doesn't explain which one he means, plus omits the level shifter): http://letsmakerobots.com/blog/unixguru/running-b...

If you want to run this magnetometer at 260hz - see here: http://forum.arduino.cc/index.php?topic=143708.0

Step 3: The Code

This is my arduino sketch.

The reason the output looks so boring, is because I wrote a companion program to read this data in and chart it. I use perl on linux and there's basicaly no chance you'll work out the OpenGL dependencies needed to run that, so unless anyone wants to have a go (and if so - ask me for my source) - I won't put that here now.

There are no-doubt a lot of other people who will have written general-purpose RS232 (that's how I send out this data) data visualisation tools; if you know of one, please post in the comments for others to use!

Enjoy!