Raspberry Pi and MAG3110 Magnetometer java example

In this example we will connect an MAG3110 Magnetometer to a Raspberry Pi and use Java to display the temperature readings, there are other languages such as C and python which work as well but I wanted to try something a little different

Here is a recap of the sensor

The MAG3110 is a small, low-power digital 3D magnetic sensor with a wide dynamic range to allow operation in PCBs with high extraneous magnetic fields. The MAG3110:

  • Measures the components of the local magnetic field, the sum of the geomagnetic field and the magnetic field created by components on the circuit board
  • Can be used in conjunction with a 3-axis accelerometer so that orientation-independent accurate compass heading information may be achieved
  • Features a standard I²C serial interface and is capable of measuring local magnetic fields up to 10 Gauss with output data rates up to 80 Hz

You can read more – https://www.nxp.com/products/sensors/magnetic-sensors/magnetometers/high-accuracy-3d-magnetometer:MAG3110

 

Parts List

Label Part Type
Raspberry Pi1 Raspberry Pi 2
U1 MAG3110

Layout

 

pi and mag3110
pi and mag3110

 

Code

This time we explore the world of Java on the Raspberry Pi

First you need to install PI4j – http://pi4j.com/install.html . I’ll sum it up as its easy to install from a terminal

The simplest method to install Pi4J on your RaspberryPi is to execute the following command directly on your RaspberryPi.
curl -s get.pi4j.com | sudo bash

Now for the java code – this is courtesy of a controleverything example

 

// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// MAG3110
// This code is designed to work with the MAG3110_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Compass?sku=MAG3110_I2CS#tabs-0-product_tabset-2
 
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import java.io.IOException;
 
public class MAG3110
{
	public static void main(String args[]) throws Exception
	{
		// Create I2C bus
		I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);
		// Get I2C device, MAG3110 I2C address is 0x0E(14)
		I2CDevice device = bus.getDevice(0x0E);
 
		// Send Start Command , Active mode
		device.write(0x10,(byte)0x01);
		Thread.sleep(200);
 
		// Read 6 bytes of data from address 0x01(1) , msb first
		byte[] data = new byte[6];
		device.read(0x01,data,0,6);
 
		// Convert data
		int xMag = (((data[0] & 0xFF) * 256 ) + (data[1] & 0xFF));
		if(xMag > 32767)
		{
			xMag -= 65536;
		}
 
		int yMag = (((data[2] & 0xFF) * 256 ) + (data[3] & 0xFF));
		if(yMag > 32767)
		{
			yMag -= 65536;
		}
 
		int zMag = (((data[4] & 0xFF) * 256 ) + (data[5] & 0xFF));
		if(zMag > 32767)
		{
			zMag -= 65536;
		}
 
		// Output data to screen
		System.out.printf("Magnetic field in X Axis :  %d %n" ,  xMag);
		System.out.printf("Magnetic field in Y Axis :  %d %n" ,  yMag);
		System.out.printf("Magnetic field in Z Axis :  %d %n" ,  zMag);
	}
}

Now you have to compile and run the program like this

$> sudo pi4j MAG3110.java

Testing

You should see the following

mag3110 output
mag3110 output

Links

GY-3110 MAG3110 Triple 3 Axis Magnetometer Breakout Electronic Compass Sensor Module For Arduino