The HMC5883 is a digital compass designed for low-field magnetic sensing. This device has a wide magnetic field range of +/-8 Oe, and an output rate of 160 Hz. The HMC5883 sensor includes automatic degaussing strap drivers, offset cancellation, and a 12-bit ADC that enables 1° to 2° compass heading accuracy. All I²C Mini Modules are designed to operate at 5VDC.
In this tutorial we are going to explain the detailed working of HMC5883 with particle photon. Particle photon is a board which facilitates sending and receiving data from website, that is supporting the most basic feature of Internet Of Things (IoT).
Step 1: Hardware RequiredThe materials that we need for accomplishing our goal includes the following hardware components:
1. HMC5883
3. I2C Cable
4. I2C Shield for Particle Photon
The hardware hookup section basically explains the wiring connections required between the sensor and the particle photon. Ensuring correct connections is the basic necessity while working on any system for the desired output. So, the requisite connections are as follows:
- The HMC5883 will work over I2C. Here is the example wiring diagram, demonstrating how to wire up each interface of the sensor.
- Out-of-the-box, the board is configured for an I2C interface, as such we recommend using this hookup if you’re otherwise agnostic. All you need is four wires!
Only four connections are required Vcc, Gnd, SCL and SDA pins and these are connected with the help of I2C cable.
These connections are demonstrated in the pictures above.
Step 3: Code to measure Magnetic Field IntensityLets start with the particle code now.
While using the sensor module with the Arduino, we include application.h
and spark_wiring_i2c.h
library. "application.h
" and spark_wiring_i2c.h
library contains the functions which facilitate the I2C communication between the sensor and the particle.
The entire Particle code is given below for the convenience of the user:
#include
#include
// HMC5883 I2C address is 0x1E(30)
#define Addr 0x1E
int xMag = 0, yMag = 0, zMag = 0;
void setup()
{
// Set variable
Particle.variable("i2cdevice", "HMC5883");
Particle.variable("xMag", xMag);
Particle.variable("yMag", yMag);
Particle.variable("zMag", zMag);
// Initialise I2C communication as MASTER
Wire.begin();
// Initialise Serial Communication, set baud rate = 9600
Serial.begin(9600);
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select configure register A
Wire.write(0x00);
// Set normal measurement configuration, data output rate = 0.75Hz
Wire.write(0x60);
// Stop I2C Transmission
Wire.endTransmission();
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select Mode register
Wire.write(0x02);
// Set continuous measurement
Wire.write(0x00);
// Stop I2C Transmission
Wire.endTransmission();
delay(300);
}
void loop()
{
unsigned int data[6];
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write(0x03);
// Stop I2C Transmission
Wire.endTransmission();
// Request 6 bytes of data
Wire.requestFrom(Addr, 6);
// Read 6 bytes of data
// xMag msb, xMag lsb, zMag msb, zMag lsb, yMag msb, yMag lsb
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();
}
delay(300);
// Convert the data
xMag = ((data[0] * 256) + data[1]);
if(xMag > 32767)
{
xMag -= 65536;
}
zMag = ((data[2] * 256) + data[3]);
if(zMag > 32767)
{
zMag -= 65536;
}
yMag = ((data[4] * 256) + data[5]);
if(yMag > 32767)
{
yMag -= 65536;
}
// Output data to dashboard
Particle.publish("Magnetic Field in X-Axis : ", String(xMag));
delay(1000);
Particle.publish("Magnetic Field in Y-Axis : ", String(yMag));
delay(1000);
Particle.publish("Magnetic Field in Z-Axis : ", String(zMag));
delay(1000);
}
Particle.variable()
function creates the variables to store the output of the sensor and Particle.publish()
function displays the output on the dashboard of the site.
The sensor output is shown in the picture above for your reference.
Step 4: ApplicationsHMC5883 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. It's one to two degree high level accuracy and precision enables Pedestrian Navigation and LBS Applications.
Comments