Monday 22 January 2018

Magnetometer Sensor – How to Use it?

Magnetometer Sensor

Introduction:

Magnetometer sensor is an equipment which is capable of detecting strength and direction of magnetic field of a specific place. It is an instrument which consist of a sensor that measures magnetic flux. Since the magnetic flux density is proportional to the magnetic field strength so the output directly gives the intensity or strength of the magnetic lines. If in case any object disturbs the magnetic field lines will be detected by magnetometer.
There are two types of magnetometer: Vector Magnetometer and Scalar Magnetometer
Vector Magnetometers are used for the high sensitive applications. A fluxgate or Vector sensor drive has an alternating drive current that runs a permeable core material.
Scalar Magnetometer uses the Nuclear Magnetic Resonance (NMR) to measure the resonance frequency of the protons in a magnetic field.

Usage:

Magnetometers have wide area of usage. A few of them are: Archaeology, Directional drilling, Smart Phones, Coal exploration, Military, Mineral exploration, Magnetic surveys, Spacecrafts etc.

Application:

Project for Reference:

Code:

In order to make this code run properly, we first need to download the library.
For downloading library use this link – Library
#include <Wire.h>
#include <HMC5883L.h>

HMC5883L compass;

void setup()
{
  Serial.begin(9600);

  // Initialize Initialize HMC5883L
  Serial.println("Initialize HMC5883L");
  while (!compass.begin())
  {
    Serial.println("Could not find a valid HMC5883L sensor, check wiring!");
    delay(500);
  }

  // Set measurement range
  compass.setRange(HMC5883L_RANGE_1_3GA);

  // Set measurement mode
  compass.setMeasurementMode(HMC5883L_CONTINOUS);

  // Set data rate
  compass.setDataRate(HMC5883L_DATARATE_30HZ);

  // Set number of samples averaged
  compass.setSamples(HMC5883L_SAMPLES_8);

  // Set calibration offset. See HMC5883L_calibration.ino
  compass.setOffset(0, 0);
}

void loop()
{
  Vector norm = compass.readNormalize();

  // Calculate heading
  float heading = atan2(norm.YAxis, norm.XAxis);

  // Set declination angle on your location and fix heading
  // You can find your declination on: http://magnetic-declination.com/
  // (+) Positive or (-) for negative
  // For Bytom / Poland declination angle is 4'26E (positive)
  // Formula: (deg + (min / 60.0)) / (180 / M_PI);
  float declinationAngle = (4.0 + (26.0 / 60.0)) / (180 / M_PI);
  heading += declinationAngle;

  // Correct for heading < 0deg and heading > 360deg
  if (heading < 0)
  {
    heading += 2 * PI;
  }

  if (heading > 2 * PI)
  {
    heading -= 2 * PI;
  }

  // Convert to degrees
  float headingDegrees = heading * 180/M_PI; 

  // Output
  Serial.print(" Heading = ");
  Serial.print(heading);
  Serial.print(" Degress = ");
  Serial.print(headingDegrees);
  Serial.println();

  delay(100);
}

No comments:

Post a Comment

SMART BLIND STICK The smart blind stick is the project made to help the blind people who is suffering when walking.It just uses to de...