Arduino Nano - MAG3110 Magnetometer Electronic Compass Sensor Tutorial
by Dcube Tech Ventures in Circuits > Electronics
22 Views, 0 Favorites, 0 Comments
Arduino Nano - MAG3110 Magnetometer Electronic Compass Sensor Tutorial
MAG3110 is a small, low power, digital 3-axis magnetometer. Its sensitivity is very high as well as it is embodied with an efficient noise reduction mechanism. It requires very low power for its efficient operation and works in single-shot measurement mode. Here is the demonstration with Arduino Nano.
What You Need..!!
Connections:
Take an I2C shield for Arduino Nano and gently push it over the pins of Nano.
Then connect the one end of I2C cable to MAG3110 sensor and the other end to the I2C shield.
Connections are shown in the picture above.
Code:
The arduino code for MAG3110 can be downloaded from our github repository- DCUBE Store.
Here is the link for the same :
https://github.com/DcubeTechVentures/MAG3110/blob/master/Arduino/MAG3110.ino
We include library Wire.h to facilitate the I2c communication of the sensor with the Arduino board.
You can also copy the code from here, it is given as follows:
// 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 .
#include
// 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);
}
Applications:
MAG3110 is a small, low power, digital 3-axis magnetometer. It can be employed in electronic compass and location based services which includes Smartphones, personal navigation devices, robotics, UAVs, speed sensing, current sensing and wrist watches with embedded electronic compasses (e-compass) function.