Beaglebone and MAG3110 magnetometer example

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

Freescale’s MAG3110 is a small, low-power, digital 3-axis magnetometer. The device can be used in conjunction with a 3-axis accelerometer to realize an orientation independent electronic compass that can provide accurate heading information. It features a standard I2C serial interface output and smart embedded functions.

The MAG3110 is capable of measuring magnetic fields with an output data rate (ODR) up to 80 Hz; these output data rates correspond to sample intervals from 12.5 ms to several seconds. The MAG3110 is available in a plastic DFN package and it is guaranteed to operate over the extended temperature range of -40°C to +85°C.

Here is a typical module for this sensor

Parts Required


Name Link
Beaglebone BeagleBone Black TI AM3358 Cortex-A8 development BB-Black Rev.C
MAG3110 MAG3110 3-Axis Digital Earth Magnetic Field Geomagnetic Sensor Module
Connecting wire Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire


[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");
	// Get I2C device, MAG3110 I2C address is 0x0E(14)
	ioctl(file, I2C_SLAVE, 0x0E);

	// Select control register1(0x10)
	// Send Start Command , Active mode
	char config[2] = {0};
	config[0] = 0x10;
	config[1] = 0x01;
	write(file, config, 2);

	// Read 6 bytes of data
	// msb first
	// Read xMag msb data from register(0x01)
	char reg[1] = {0x01};
	write(file, reg, 1);
	char data[1] = {0};
	if(read(file, data, 1) != 1)
		printf("Error : Input/Output error \n");
	char data_0 = data[0];

	// Read xMag lsb data from register(0x02)
	reg[0] = 0x02;
	write(file, reg, 1);
	read(file, data, 1);
	char data_1 = data[0];

	// Read yMag msb data from register(0x03)
	reg[0] = 0x03;
	write(file, reg, 1);
	read(file, data, 1);
	char data_2 = data[0];

	// Read yMag lsb data from register(0x04)
	reg[0] = 0x04;
	write(file, reg, 1);
	read(file, data, 1);
	char data_3 = data[0];

	// Read zMag msb data from register(0x05)
	reg[0] = 0x05;
	write(file, reg, 1);
	read(file, data, 1);
	char data_4 = data[0];

	// Read zMag lsb data from register(0x06)
	reg[0] = 0x06;
	write(file, reg, 1);
	read(file, data, 1);
	char data_5 = data[0];

	// Convert the data
	int xMag = (data_0 * 256 + data_1);
	if(xMag > 32767)
		xMag -= 65536;

	int yMag = (data_2 * 256 + data_3);
	if(yMag > 32767)
		yMag -= 65536;

	int zMag = (data_4 * 256 + data_5);
	if(zMag > 32767)
	zMag -= 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);


Code Example

This is a controleverything example – they have code examples for various platforms. This is the C example from

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

First of all compile the c program.

$>gcc MAG3110.c -o MAG3110

Run the c program.



After running you should see something like this

Magnetic field in X-Axis : -746
Magnetic field in Y-Axis : 1233
Magnetic field in Z-Axis : 277


Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.