Sunday, June 14, 2015

HMC5883L magnetometer to Raspberry Pi connection notes

Some quick notes for connecting Honeywell's 3-Axis Digital Compass IC
HMC5883L to the I2C bus on a Raspbrry Pi board.

HMC5883L breakout board from SparkFun:
https://www.sparkfun.com/products/10530


Connections

GND -> GND (pin 6 on RPi I/O header)
VCC -> 3.3 V (pin 1 on RPi )
SDA -> SDA1 I2C (pin 3 on RPi)
SCL -> SCL1 I2C (pin 5 on RPi)

Configuration

Enable I2C using "raspi-config". Add user to i2c group.

pi@raspberrypi ~ $ i2cdetect 1
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-1.
I will probe address range 0x03-0x77.
Continue? [Y/n]
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1e --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --



Run

See below for example C code.

pi@raspberrypi ~ $ gcc magnetometer.c   -o magnetometer -lm

pi@raspberrypi ~ $ ./magnetometer
Identification: 'H43' HMC5883L sensor detected
1434246270.000000, -548, -649, 349, 67030.4 nT
1434246270.100000, -547, -650, 349, 67038.5 nT
1434246270.200000, -548, -648, 348, 66951.1 nT
1434246270.300000, -548, -650, 350, 67109.8 nT
1434246270.400000, -548, -650, 350, 67109.8 nT
1434246270.500000, -547, -650, 350, 67066.3 nT
1434246270.600000, -547, -650, 349, 67038.5 nT
1434246270.700000, -548, -650, 350, 67109.8 nT
1434246270.800000, -547, -649, 348, 66959.2 nT
1434246270.900000, -548, -650, 351, 67137.6 nT


Code

C code to read and display the magnetometer data

No comments:

Post a Comment