Beaglebone and HMC5983 magnetometer example

In this article we look at an HMC5883L connected to a Beaglebone first of all lets look at the sensor.

The Honeywell HMC5883L is a surface-mount, multi-chip module designed for low-field magnetic sensing with a digital interface for applications such as low-cost compassing and magnetometry.

The HMC5883L includes our state-of-the-art, high-resolution HMC118X series magneto-resistive sensors plus an ASIC containing amplification, automatic degaussing strap drivers, offset cancellation, and a 12-bit ADC that enables 1° to 2° compass heading accuracy.

The I2C serial bus allows for easy interface. The HMC5883L is a 3.0×3.0×0.9mm surface mount 16-pin leadless chip carrier (LCC). Applications for the HMC5883L include Mobile Phones, Netbooks, Consumer Electronics, Auto Navigation Systems, and Personal Navigation Devices.

The HMC5883L utilizes Honeywell’s Anisotropic Magnetoresistive (AMR) technology that provides advantages over other magnetic sensor technologies. These anisotropic, directional sensors feature precision in-axis sensitivity and linearity. These sensors’ solid-state construction with very low cross-axis sensitivity is designed to measure both the direction and the magnitude of Earth’s magnetic fields, from milli-gauss to 8 gauss. Honeywell’s Magnetic Sensors are among the most sensitive and reliable low-field sensors in the industry.

FEATURES

12-Bit ADC Coupled with Low Noise AMR Sensors Achieves 2 milli-gauss Field Resolution in ±8 Gauss Fields
Low Voltage Operations (2.16 to 3.6V) and Low Power Consumption (100 μA)
I2C Digital Interface
Wide Magnetic Field Range(+/-8Oe)
Fast 160Hz Maximum Output Rate

 

Parts Required

 

Name Link
Beaglebone BeagleBone Black TI AM3358 Cortex-A8 development BB-Black Rev.C
HMC5883
Connecting wire Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Schematic/Connection

beaglebone and hmc5883
beaglebone and hmc5883

Code Example

This is a controleverything example – they have code examples for various platforms. This is the C example from https://github.com/ControlEverythingCommunity/HMC5883

[codesyntax lang=”cpp”]

#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>

void main() 
{
	// Create I2C bus
	int file;
	char *bus = "/dev/i2c-2";
	if ((file = open(bus, O_RDWR)) < 0) 
	{
		printf("Failed to open the bus. \n");
		exit(1);
	}
	// Get I2C device, HMC5883 I2C address is 0x1E(30)
	ioctl(file, I2C_SLAVE, 0x1E);

	// Select Configuration register A(0x00)
	// Normal measurement configuration, data rate o/p = 0.75 Hz(0x60)
	char config[2] = {0};
	config[0] = 0x00;
	config[1] = 0x60;
	write(file, config, 2);
	// Select Mode register(0x02)
	// Continuous measurement mode(0x00)
	config[0] = 0x02;
	config[1] = 0x00;
	write(file, config, 2);
	sleep(1);

	// Read 6 bytes of data from register(0x03)
	// xMag msb, xMag lsb, zMag msb, zMag lsb, yMag msb, yMag lsb
	char reg[1] = {0x03};
	write(file, reg, 1);
	char data[6] ={0};
	if(read(file, data, 6) != 6)
	{
		printf("Erorr : Input/output Erorr \n");
	}
	else
	{
		// Convert the data
		int xMag = (data[0] * 256 + data[1]);
		if(xMag > 32767)
		{
			xMag -= 65536;
		}

		int zMag = (data[2] * 256 + data[3]);
		if(zMag > 32767) 
		{
			zMag -= 65536;
		}

		int yMag = (data[4] * 256 + data[5]);
		if(yMag > 32767) 
		{
			yMag -= 65536;
		}

		// Output data to screen
		printf("Magnetic field in X-Axis : %d \n", xMag);
		printf("Magnetic field in Y-Axis : %d \n", yMag);
		printf("Magnetic field in Z-Axis : %d \n", zMag);
	}
}

[/codesyntax]

Save this as HMC5883.c, I used the Cloud 9 IDE

First of all compile the c program.

$>gcc HMC5883.c -o HMC5883 -lm

Run the c program.

$>./HMC5883

Output

After running you should see something like this

Magnetic field in X-Axis : 251
Magnetic field in Y-Axis : 149
Magnetic field in Z-Axis : 167

LEAVE A REPLY

Please enter your comment!
Please enter your name here