Tag Archives: hmc5883

One of the things I was looking into is a digital compass module.  These are modules that return a direction it is pointing relative to magnetic north.

I started by using the Bus Pirate to access and read the module.  I did that as below:

m <– for menu
4 <– for I2C
3 <– for 100KHz

I2C>(1) <– I2C Address Search
Searching I2C address space. Found devices at:
0x3C(0x1E W) 0x3D(0x1E R)

I2C>[0x3c 0x00 0x70] <–Set measurement mode to 8-average, 15Hz
I2C START BIT
WRITE: 0x3C ACK
WRITE: 0x00 ACK
WRITE: 0x70 ACK
I2C STOP BIT

I2C>[0x3c 0x01 0xa0] <– Set gain to 5
I2C START BIT
WRITE: 0x3C ACK
WRITE: 0x01 ACK
WRITE: 0xA0 ACK
I2C STOP BIT

I2C>[0x3c 0x02 0x01] <– Set to single-measurement mode
I2C START BIT
WRITE: 0x3C ACK
WRITE: 0x02 ACK
WRITE: 0x01 ACK
I2C STOP BIT
I2C>[0x3d r:6] <– Read 6 bytes
I2C START BIT
WRITE: 0x3D ACK
READ: 0xFF ACK 0xFD ACK 0xFF ACK 0x98 ACK 0x00 ACK 0x5F
NACK
I2C STOP BIT

This returned 0xFF, 0xFD for X, 0xFF, 0x98 for Y, and 0x00, 0x5F for Z.  This is Twos Compliment Form, which is best explained by this Stack Overflow Community Wiki.

X Coordinate (0xFF): Binary 1111 1111 + 0xFD: Binary 1111 1101

Y Coordinate (0xFF): Binary 1111 1001 + 0x98: Binary 1001 1000

Z Coordinate (0x00): Binary 0000 0000 + 0x5F: Binary 0101 1111

Ultimately, this is where I stopped.  I know it works, I have some ideas on how changes affect it.  Now on to an Arduino.

Arduino Micro Setup

I plugged this into an Arduino Micro on a breadboard as below.  The 5V, ground, pin 2/SDA, and pin 3/SCL were connected as shown (note that the compass on the left can be ignored – I have two and they are both in the breadboard just to keep them somewhere safe).

2014-09-14 22.39.54

One of the more difficult parts of this is ensuring you’re in the correct pins on the Micro – the numbers are small and not centered on the pin.

The code is below.  I started with code from Sparkfun and modified it as necessary.

Could not embed GitHub Gist fd3b2a86a38f2d8f54c1: Not Found

Right now, this is functional.  There will be more to come.

-73-


Category: Equipment

This is the new server