Raspberry Pi working with UBlox 6M and HMC5883l digital compass board from Airbot!…

I’ve managed to get serial comms to my airbot ublox 6M GPS unit, and I2C comms to the onboard 3D compass! Here I talk over the code…

Thought I’d document this here as there are no tutorials out there that cover it.

Getting raw NMEA messages over Serial

As I previously mentioned, I’ve connected my Raspberry Pi to my ublox breakout card via a Gertboard.

Firstly I used wiringPi to access the serial comms to the GPS unit. The code for this is in ubloxp.c in this Gist.

This does several things:-

  • Opens a serial port to /dev/ttyAMA0 at 9600 baud (higher values won’t work)
  • Loops continuously reading the NMEA raw messages and prints them to screen

This works a treat.

Decoding NMEA with nmealib

I wanted to decode these though, and didn’t want to write my own parser. I headed over to nmelib and grabbed their parser and sample code.

I now have ublox.c which uses the NMEA parser to just report the WGS84 (EPSG4326) longitude and latitude. Other things like altitude are available, but I’ve not printed them out.

Code does the same as above, but decodes messages as they arrive.

Accessing the Compass via I2C

This was the trickiest part. Had to download and install the i2c code on my Pi using these instructions.

Be sure to recompile wiringPi once you have this installed before running gpio.

Then each boot I use:-

gpio load i2c

i2cdetect -y 1

This reports my device is at 0x1e (1e on the screen) – this is the address to use to communicate with it. Your address may vary depending on revision. Rest of the code should work though.

I then headed over to this URL to grab the manual for the chip concerned.

I then connected the remaining 2 pin connector from my ublox to the SCL and SDA pins on my Gertboard.

Wiring Pi didn’t want to work at all with I2C. Can’t figure out why. I ended up just using the Linux i2c-dev user space code which worked first time!!!

To use this, download the i2c-tools i2c-dev.h header file and add to your include path.

Now review the code in ublox-i2c.c This does the following:-

  • Open the device /dev/i2c-1 for read/write
  • Instruct the address (device 0x1e) that it is a slave
  • Place the compass in ‘continuous mode’ by writing 0x00 to the 0x02 register (mode register)
  • Fetch and print the 6 data values (technically 3 values, high and low bits – not converted them to a single output field each for X, Y, Z yet)
  • Fetch and print the status code (status 3)

All works wonderfully well! I now have enough to create a position and orientation aware robot. 8o)

The GPS is very accurate even indoors. Seems to be only 2-4m variability with 11 satellites indoors, occassionally 13.

The compass too is very sensitive and responsive.

Hopefully you’ll all find this useful!

Have a read of this link for linux i2c info. Manuals for the compass (NOT gps) can be found on the sparkfun website. Best guide is the datasheet link. This has very detailed register information for all functionality in it.



    1. Hi! Not compensating the readings yet. May not actually use the compass at all in this application. Found a smaller GPS unit (GP-635T – https://www.sparkfun.com/products/11571 ) which works nicely. Only reason to use the compass is on the instructor’s (followers) device. They’re used to holding it steady like a compass anyway.
      Just about to write a blog page about the radios. Got them working!…

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