Sunday, August 3, 2014

Magnetometer(Digital Compass) HMC5883L


Here is our magnetometer/digital compass. Datasheet, sample code, as well as a link to the Bildr (a helpful website for our purpose) is located in this sparkfun link: https://www.sparkfun.com/products/10530

The main purpose of this sensor is to detect magnetic fields in order to recognize the North direction. When calibrated correctly, it outputs how many degrees away from the North the compass' x-side is pointed to. This sensor is compatible with the I2C bus, and so it has in total 4 connections with the Arduino Uno:


 


After connection, the compass is now ready to talk! It is important to note that this sensor requires the I2C library Wire.h, as well as its own library available in this Bildr link: http://bildr.org/2012/02/hmc5883l_arduino/. With the help of this Bildr link, we were able to come up with the code for operation including some calibration information regarding magnetic declination:

#include <Wire.h>
#include <HMC5883L.h>

HMC5883L compass;

void setup(){
  Serial.begin(9600);
  Wire.begin();
  compass = HMC5883L(); //new instance of HMC5883L library
  setupHMC5883L(); //setup the HMC5883L
}

// Our main program loop.
void loop(){
  float heading = getHeading();
  Serial.println(heading);
  delay(100); //only here to slow down the serial print
}

void setupHMC5883L(){
  //Setup the HMC5883L, and check for errors
  int error;  
  error = compass.SetScale(1.3); //Set the scale of the compass.
  if(error != 0) Serial.println(compass.GetErrorText(error)); //check if there is an error, and print if so

  error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
  if(error != 0) Serial.println(compass.GetErrorText(error)); //check if there is an error, and print if so
}

float getHeading(){
  //Get the reading from the HMC5883L and calculate the heading
  MagnetometerScaled scaled = compass.ReadScaledAxis(); //scaled values from compass.
  float heading = atan2(scaled.YAxis, scaled.XAxis);
  
 //declination angle is the angle between the magnetic north and the true north on a horizontal plane. In Walnut, CA, declination angle is 12° 13' EAST, which converts to 0.213221057 in radians. Since it said EAST, we have to compensate by subtracting the angle from our magnetic heading to get the true heading
  float declinationAngle = 0.213221057;
  heading -= declinationAngle;
  
  // Correction for when signs are reversed.
  if(heading < 0) heading += 2*PI;
  if(heading > 2*PI) heading -= 2*PI;

  return heading * RAD_TO_DEG; //radians to degrees
}

Despite having a very detailed code for operation, this specific digital compass does not come with an accelerometer. Therefore, it is NOT tilt compensated! It produces erroneous heading depending on how the sensor board is tilted. 

We subjected this board through various tests: dry ice bath reaching -40 degrees Celsius, extremely low pressure conditions using a devised bell jar reaching 0.005 atm more or less, and a normal cold test done in a refrigerator at -17 degrees Celsius. As it did not collapse amidst extreme conditions, it was cleared for the flight.

For future high altitude balloon launches, it is wise to be more familiar with Arduino as it helps in understanding the code. Understanding the code could lead to better calibration and integration to the main code for the main Arduino.

Additional info about this sensor is available in Trello: https://trello.com/c/is2z01uo/4-hmc5883l-triple-axis-compass-magnetometer-sensor

No comments:

Post a Comment