Using a HMC5883L magnetometer/compass with the #RaspberryPi

This blog post describes getting an HMC5883L magnetic compass sensor to work with a Raspberry Pi. The pictures below show the sensor breakout board in question.


P1370736

P1370746

P1370741

For Arduino Leonardo

If you do not have an Arduino, or just want the instructions for the Raspberry Pi, please skip this section.

I followed instructions from bildr.org to connect the board up to an Arduino Leonardo clone (the Dreamer Nano v4). For the Leo, the I2C ports are D2 (for SDA) and D3 (for SCL). Power supply for the board is 3v3 and you also need to connect up the ground pin. The DRDY pin is not used. I downloaded the Arduino library and examples package from the bottom of that post (http://code.bildr.org/download/976.zip) and wired it up like so:

  • VCC -> 3.3v
  • GND -> Ground
  • SDA -> D2
  • SCL -> D3

I used the Arduino IDE to upload the example sketch and opened the serial monitor from which I got the following readings:

Raw: 31 407 -587 Scaled: 28.52 374.44 -540.04 Heading: 1.54 Radians 88.26 Degrees

As I rotated the breadboard on which the sensor was mounted, the readings changed.

For Raspberry Pi

First of all, make sure your Pi has I2C installed and enabled. This is a bit of a trial if you don’t know what you’re doing, but if you have any problems, leave me a comment. Follow these instructions to find out how to do it. After running the last command:
i2detect -y <port>
you should get a value in one of the “cells”. For instance, I get a “1e” reading in the 10/e cell.

Secondly, install the ThinkBowl I2C libraries. Please note, take notice of the fact that for these libraries you need to use Python 3, not just plain Python.

Next, find your magnetic declination by using this site.

Now connect up the sensor board. I used a breadboard and a Hobbytronics GPIO breakout connector, but you can connect it directly to the Pi’s GPIO pins. Here are the pin-to-pin mappings from the Pi to the board. Remember that Pin 1 is the 3v power connector on the Pi’s GPIO so you can count from there.

Pi pin number       Pi pin name        HMC5883L pin name
--------------------------------------------------------
1                   3V3                VCC
6                   Ground             GND
3                   SDA                SDA
5                   SCL                SCL

Next, use the basic Python example from here (or see below) and run it with python3 (for example python3 test-sensor.py) and you should get X,Y,Z axis readings and your heading in degrees and minutes. Rotate the board and you should get different readings

#!/usr/bin/python3
from i2clibraries import i2c_hmc5883l
hmc5883l = i2c_hmc5883l.i2c_hmc5883l(1)
hmc5883l.setContinuousMode()
hmc5883l.setDeclination(9,54)
print(hmc5883l)

NB: If you get an input/output error, try changing the first line to:

hmc5883l = i2c_hmc5883l.i2c_hmc5883l(0)

Lastly, if you have problems getting these instructions to work, please leave a comment or use my contact form
to ask questions.

Here, for interest, is what my Pi looks like when connected up to a breadboard with the sensor board on it.

P1370748

Resources used for this blog post

 

20 comments for “Using a HMC5883L magnetometer/compass with the #RaspberryPi

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.