HMC5883L Magnetometer

As with nearly all cool electronic devices that I buy, this one too came from the foreign lands and was off eBay (http://goo.gl/jbcKE6 – Link will expire as with all eBay links)!

One of the features that I’ve wanted to have in my watch that I’d actually find really useful is the ability to have a compass. While my phone offers a compass too, having one mounted on my wrist is just so much cooler so as a bit of a tester, I bought this board (£1.16 at time of purchase) and decided to hook it up to my STM32F0 discovery board. It was also a good way for me to learn the I2C protocol as I’d never used it before.

I’ve always thought that I2C was a really cool protocol having all devices in an open drain configuration and only eve requiring two wires. Some devices can even be powered by the pullup resistors on the I2C bus themselves which is even cooler!

Having become relatively educated in the SPI protocol, I thought that many of the parameters would be the same for I2C but how wrong I was. Firstly most of I2C seems to be sequential flag checking, looking for acknowledge bits and so on. After searching the internet for STM32F0 examples, I couldn’t find any at all which was a bit annoying! So in the end, I dived straight in and after a frustrating hour or so, I got my first magnetometer reading through!

Image
Displaying the accelerometer value on a good ol’ 128×64 OLED screen (SSD1306 based), this picture seemed to be taken right on the change of a frame, hence the ghosting of a value!

The HMC5883L (http://goo.gl/XMYQ31), is really useful as it essentially contains a loop buffer containing all of the read values and the address of this buffer increments with every read meaning that you only need to initialize the register address at register 03, then execute 6 I2C reads to get all XYZ values. Each XYZ read is 12bit and is transmitted as a high and low pair of bits. These bits are in the two’s compliment form and can be easily concatenated with 1 shift by 8 bits and an OR.

I’m yet to get a proper compass application written but that will be soon to follow! My next IC to play with will be the LSM303 which integrates a magnetometer and an accelerometer. Once I figure this IC out, I’ll hopefully be incorporating it into the watch so I can have accelerometer activated wakeup!

If I ever get round to learning to Github, I’ll upload my code for this!

Update: For a more useful implementation of the compass, visit my new post: https://hsel.co.uk/2014/08/13/stm32f0-mini-tutorial-using-the-i2c-peripheral-to-communicate-with-a-hmc5883l-digital-compass-ic/

Advertisements

3 responses to “HMC5883L Magnetometer

  1. Is it possible to get source code of this?
    I am also trying to get I2C working on STM32F0, but I can’t find good examples for it.

  2. Is it possible to get source code for this?
    I am also trying to get I2C working on STM32F0, but can’t find good working examples for this.

    • Hey! I’ve got the source code but I don’t live at home at the moment so I can’t really get hold of my whole electronics supply to test if its working. It was working last time I used it but I can’t guarantee it. I’ll update the post and include the git location 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s