HMC5883L compass, C programming

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jstampfl
 
Posts: 25
Joined: Mon Sep 17, 2012 2:15 am

HMC5883L compass, C programming

Post by jstampfl »

C programming example of reading values from the HMC5883L compass module via I2C.
Works fine with 3.3v.
Connections:
HMC5883L BBB
VCC P9 – 3
GND P9 – 2
SCL P9 – 17
SDA P9 – 18 //Sorry, Had a typo here, should be P9 - 18 not P9 - 19

Add “BB-I2C1” to uEnv.txt. So the first line looks like:


optargs=quiet drm.debug=7 capemgr.enable_partno=BB-UART1,BB-I2C1

(Note: the “BB-UART1” entry is from my Ultimate GPS example in this forum.)

The following is the C code. (gcc compass.c -o compass)
The contents of the 6 data registers are printed.
==============================================

Code: Select all

//compass.c for the HMC5883L
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <stdlib.h>
#include <time.h>

/* change this definition for the correct port */
#define COMPASS "/dev/i2c-2" //I2C port
//NOTE:  I think I am using i2c-1.  I have connected the HMC5883L to pins 17,18 on P9.
//Hard to argue with i2ctools. 

#define _POSIX_SOURCE 1 /* POSIX compliant source */

main()
{
int fd, c, res;
int daddr = 0x1e; //address of HMC5883L before shifting.
int i;
useconds_t dtime = 8; //delay for usleep
// buffers for commands.
char write0[4]; // command to set reg 0 - AKA Configuration Register A
char write1[4]; // command to set reg 1 - AKA Configuration Register B
char write2[4]; // command to set reg 2 - AKA Mode Register

write0[0] = 0x00; // reg 00
write0[1] = 0x70; // average 8 samples, 15Hz output
write1[0] = 0x01; // reg 01
write1[1] = 0xA0; // gain 5
write2[0] = 0x02; // reg 02
write2[1] = 0x01; // mode single measurement
//
char buf[10];

if ((fd = open(COMPASS, O_RDWR )) < 0) {;
    perror("Failed to open the I2C bus\n");
    exit(1);
}
// Sets the slave address for reads & writes 
// see i2c-dev.h
if (ioctl(fd, I2C_SLAVE,daddr) < 0) {
	printf("Failed to access\n");
	exit(1);
}

//Set reg 0
if (write(fd,write0,2) != 2) {
	printf("Write to zero failed\n");
}
//Set reg 1
if (write(fd,write1,2) != 2) {
	printf("Write to one failed\n");
}
//Set reg 2
if (write(fd,write2,2) != 2) {
	printf("Write to two failed\n");
}

//datasheet says to delay here.
if ( usleep(dtime)  < 0) {
	printf("sleep error\n");
}

//read the 6 data.
if (read(fd, buf, 6) != 6) {
	printf("Read failed\n");
}

for (i = 0;i<6;i++) {
	printf("values %d  %x\n",i,buf[i]);
}

}

use i2cdump to verify the contents.

For some instruction in using the i2c-tools see: Beaglebone: An I2C Tutorial - Interfacing to a BMA180
by Derek Molloy
You can find the video on Youtube.
Last edited by jstampfl on Fri Jun 20, 2014 12:45 am, edited 2 times in total.

User avatar
jstampfl
 
Posts: 25
Joined: Mon Sep 17, 2012 2:15 am

Re: HMC5883L compass, C programming

Post by jstampfl »

Output from the compass program

values 0 ff
values 1 f5
values 2 ff
values 3 f
values 4 0
values 5 6e

Output from i2cdump -f 2 0x1e

0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: 70 a0 03 ff f5 ff 0f 00 6e 03 48 34 33 00 00 3c p??.?.?.n?H43..<
10: 00 00 00 00 00 00 00 00 00 00 00 0f 8d 07 e8 10 ...........?????
20: 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 ........?.......
30: 00 00 00 14 84 58 66 00 a0 00 07 00 00 00 00 00 ...??Xf.?.?.....
40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
80: 70 a0 03 ff f5 ff 0f 00 6e 03 48 34 33 00 00 3c p??.?.?.n?H43..<
90: 00 00 00 00 00 00 00 00 00 00 00 0f 8d 07 e8 10 ...........?????
a0: 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 ........?.......
b0: 00 00 00 14 84 58 66 00 a0 00 07 00 00 00 00 00 ...??Xf.?.?.....
c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................

User avatar
jstampfl
 
Posts: 25
Joined: Mon Sep 17, 2012 2:15 am

Re: HMC5883L compass, C programming

Post by jstampfl »

Sorry, I had a typo in the original post. The SDA line should be connected to P9 -18, not P9 - 19

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: HMC5883L compass, C programming

Post by adafruit_support_mike »

Nice work! Thanks for posting the code. ;-)

Locked
Please be positive and constructive with your questions and comments.

Return to “Beagle Bone & Adafruit Beagle Bone products”