MSP-EXP432P401R LaunchPad and MAG3110 3D magnetic sensor example

In this example we show how to connect a MAG3110 3D magnetic sensor to a MSP-EXP432P401R LaunchPad, the example will use the Energia IDE.

I used a MAG3110 module in this example, which you can see below

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

More info – MAG3110 site

 

Connection

Here is a picture of the launchpad so you can see what pins we are referring to below in the table

launchpad pins1
launchpad pins1
Module Connection MSP432 Connection
SDA J1-10 SDA
SCL J1-9 SCL
Gnd J3-22 Gnd
Vcc J1-1 3.3v

Code

 

#include<Wire.h>
 
// MAG3110 I2C address is 0x0E(14)
#define Addr 0x0E
 
void setup() 
{
  // Initialise I2C communication as MASTER 
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);
 
  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select control register-1
  Wire.write(0x10);
  // Set active mode enabled
  Wire.write(0x01);
  // Stop I2C Transmission
  Wire.endTransmission();
  delay(300);
}
 
void loop() 
{
  unsigned int data[6];
 
  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select data register
  Wire.write(0x01);
  // Stop I2C Transmission
  Wire.endTransmission();
 
  // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);
 
  // Read 6 bytes of data
  // xMag lsb, xMag msb, yMag lsb, y Mag msb, zMag lsb, zMag msb
  if(Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
  }
 
  // Convert the data
  int xMag = ((data[1] * 256) + data[0]);
  int yMag = ((data[3] * 256) + data[2]);
  int zMag = ((data[5] * 256) + data[4]);
 
  // Output data to serial monitor
  Serial.print("Magnetic field in X Axis : ");
  Serial.println(xMag);
  Serial.print("Magnetic field in Y Axis : ");
  Serial.println(yMag);
  Serial.print("Magnetic field in Z Axis : ");
  Serial.println(zMag);
  delay(1000);
}

 

Output

Open the serial monitor and you should see something like this

Magnetic field in X Axis : 31230
Magnetic field in Y Axis : 32261
Magnetic field in Z Axis : 17665
Magnetic field in X Axis : 30974
Magnetic field in Y Axis : 32517
Magnetic field in Z Axis : 13057
Magnetic field in X Axis : 31742
Magnetic field in Y Axis : 31237
Magnetic field in Z Axis : 18177
Magnetic field in X Axis : 31486
Magnetic field in Y Axis : 32005
Magnetic field in Z Axis : 18689

 

Links

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