Triple-axis Magnetometer HMC5883L- Need help calculating dec

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
molex1701
 
Posts: 54
Joined: Mon Feb 25, 2013 6:34 pm

Triple-axis Magnetometer HMC5883L- Need help calculating dec

Post by molex1701 »

Hello,
I received my compass board that I bought from your 5 year Ask an Engineer discount (Thanks very much!!!). Anyway I've been having trouble using it. It only returns values in 230-280 range or so. Looking at the example file it mentions going to this site http://magnetic-declination.com/ and computing declinationAngle. But I don't know how to compute that value based on my numbers. My information is:
Magnetic declination: -7° 18' WEST
Declination is NEGATIVE
Inclination: 69° 27'
Magnetic field strength: 54049.8 nT. Sorry but I'm very bad at math :( I'm hoping setting this will help get me the correct values. Good day.

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Triple-axis Magnetometer HMC5883L- Need help calculating

Post by adafruit_support_bill »

Magnetic declination: -7° 18' WEST
That means that magnetic North in your area is 7° 18' West of True North. So you simply need to add 7° 18' to your compass reading to find True North.
It only returns values in 230-280 range or so.
But before you worry about the declination, we need to make sure you are getting good readings on magnetic North. When turning the device through a full 360 degrees, are all your outputs in that range?

User avatar
molex1701
 
Posts: 54
Joined: Mon Feb 25, 2013 6:34 pm

Re: Triple-axis Magnetometer HMC5883L- Need help calculating

Post by molex1701 »

Hello,
But before you worry about the declination, we need to make sure you are getting good readings on magnetic North. When turning the device through a full 360 degrees, are all your outputs in that range?
All I ever get is in the high 200 range as I spin it around on my desk.

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Triple-axis Magnetometer HMC5883L- Need help calculating

Post by adafruit_support_bill »

All I ever get is in the high 200 range as I spin it around on my desk.
This is with the example sketch from the library?
Please post some photos showing your soldering and connections to the magnetometer.

User avatar
molex1701
 
Posts: 54
Joined: Mon Feb 25, 2013 6:34 pm

Re: Triple-axis Magnetometer HMC5883L- Need help calculating

Post by molex1701 »

Hello,
Didn't mean to seem like I ignored you but mother nature had other plans.

http://i.imgur.com/kJX0Oie.png

http://i.imgur.com/ZAfctY6.png

Let me know if photos show enough.

User avatar
molex1701
 
Posts: 54
Joined: Mon Feb 25, 2013 6:34 pm

Re: Triple-axis Magnetometer HMC5883L- Need help calculating

Post by molex1701 »


User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Triple-axis Magnetometer HMC5883L- Need help calculating

Post by adafruit_support_bill »

That all looks right. Can you post the exact code you are using, and a sample of the serial monitor output?

User avatar
molex1701
 
Posts: 54
Joined: Mon Feb 25, 2013 6:34 pm

Re: Triple-axis Magnetometer HMC5883L- Need help calculating

Post by molex1701 »

The code is just the included one from the library I downloaded from your site but here it is to double check it.

Code: Select all

/*
HMC5883L_Example.pde - Example sketch for integration with an HMC5883L triple axis magnetomerwe.
Copyright (C) 2011 Love Electronics (loveelectronics.co.uk)

This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or F-I@T8NESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

// Reference the I2C Library
#include <Wire.h>
// Reference the HMC5883L Compass Library
#include <HMC5883L.h>

// Store our compass as a variable.
HMC5883L compass;
// Record any errors that may occur in the compass.
int error = 0;

// Out setup routine, here we will configure the microcontroller and compass.
void setup()
{
  // Initialize the serial port.
  Serial.begin(9600);

  Serial.println("Starting the I2C interface.");
  Wire.begin(); // Start the I2C interface.

  Serial.println("Constructing new HMC5883L");
  compass = HMC5883L(); // Construct a new HMC5883 compass.
    
  Serial.println("Setting scale to +/- 1.3 Ga");
  error = compass.SetScale(1.3); // Set the scale of the compass.
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));
  
  Serial.println("Setting measurement mode to continous.");
  error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));
}

// Our main program loop.
void loop()
{
  // Retrive the raw values from the compass (not scaled).
  MagnetometerRaw raw = compass.ReadRawAxis();
  // Retrived the scaled values from the compass (scaled to the configured scale).
  MagnetometerScaled scaled = compass.ReadScaledAxis();
  
  // Values are accessed like so:
  int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)

  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2(scaled.YAxis, scaled.XAxis);
  
  // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  // Find yours here: http://www.magnetic-declination.com/
  // Mine is: 2� 37' W, which is 2.617 Degrees, or (which we need) 0.0456752665 radians, I will use 0.0457
  // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  float declinationAngle = 0.0457;
  heading += declinationAngle;
  
  // Correct for when signs are reversed.
  if(heading < 0)
    heading += 2*PI;
    
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)
    heading -= 2*PI;
   
  // Convert radians to degrees for readability.
  float headingDegrees = heading * 180/M_PI; 

  // Output the data via the serial port.
  Output(raw, scaled, heading, headingDegrees);

  // Normally we would delay the application by 66ms to allow the loop
  // to run at 15Hz (default bandwidth for the HMC5883L).
  // However since we have a long serial out (104ms at 9600) we will let
  // it run at its natural speed.
  // delay(66);
}

// Output the data down the serial port.
void Output(MagnetometerRaw raw, MagnetometerScaled scaled, float heading, float headingDegrees)
{
   Serial.print("Raw:\t");
   Serial.print(raw.XAxis);
   Serial.print("   ");   
   Serial.print(raw.YAxis);
   Serial.print("   ");   
   Serial.print(raw.ZAxis);
   Serial.print("   \tScaled:\t");
   
   Serial.print(scaled.XAxis);
   Serial.print("   ");   
   Serial.print(scaled.YAxis);
   Serial.print("   ");   
   Serial.print(scaled.ZAxis);

   Serial.print("   \tHeading:\t");
   Serial.print(heading);
   Serial.print(" Radians   \t");
   Serial.print(headingDegrees);
   Serial.println(" Degrees   \t");
}
I start the compass module with the X arrow pointing north. But with it number starting at 255 it seems to show that its south? I'm not really sure yet what the XY arrows show for North/South yet. I spin the compass to the right then all the way back to X pointing north again. As you can see the numbers stay in the high 200s. Sigh.
Example output:

Code: Select all

Constructing new HMC5883L
Setting scale to +/- 1.3 Ga
Setting measurement mode to continous.
Raw:	-132   -418   -218   	Scaled:	-121.44   -384.56   -200.56   	Heading:	4.45 Radians   	255.09 Degrees   	
Raw:	-132   -417   -220   	Scaled:	-121.44   -383.64   -202.40   	Heading:	4.45 Radians   	255.05 Degrees   	
Raw:	-133   -418   -222   	Scaled:	-122.36   -384.56   -204.24   	Heading:	4.45 Radians   	254.97 Degrees   	
Raw:	-132   -418   -219   	Scaled:	-121.44   -384.56   -201.48   	Heading:	4.45 Radians   	255.09 Degrees   	
Raw:	-134   -417   -220   	Scaled:	-123.28   -383.64   -202.40   	Heading:	4.45 Radians   	254.80 Degrees   	
Raw:	-134   -419   -217   	Scaled:	-123.28   -385.48   -199.64   	Heading:	4.45 Radians   	254.88 Degrees   	
Raw:	-135   -420   -220   	Scaled:	-124.20   -386.40   -202.40   	Heading:	4.45 Radians   	254.80 Degrees   	
Raw:	-130   -416   -218   	Scaled:	-119.60   -382.72   -200.56   	Heading:	4.46 Radians   	255.26 Degrees   	
Raw:	-131   -416   -219   	Scaled:	-120.52   -382.72   -201.48   	Heading:	4.45 Radians   	255.14 Degrees   	
Raw:	-132   -419   -222   	Scaled:	-121.44   -385.48   -204.24   	Heading:	4.45 Radians   	255.13 Degrees   	
Raw:	-135   -418   -220   	Scaled:	-124.20   -384.56   -202.40   	Heading:	4.45 Radians   	254.72 Degrees   	
Raw:	-130   -420   -218   	Scaled:	-119.60   -386.40   -200.56   	Heading:	4.46 Radians   	255.42 Degrees   	
Raw:	-131   -419   -219   	Scaled:	-120.52   -385.48   -201.48   	Heading:	4.46 Radians   	255.26 Degrees   	
Raw:	-134   -419   -219   	Scaled:	-123.28   -385.48   -201.48   	Heading:	4.45 Radians   	254.88 Degrees   	
Raw:	-134   -418   -220   	Scaled:	-123.28   -384.56   -202.40   	Heading:	4.45 Radians   	254.84 Degrees   	
Raw:	-130   -420   -220   	Scaled:	-119.60   -386.40   -202.40   	Heading:	4.46 Radians   	255.42 Degrees   	
Raw:	-131   -413   -221   	Scaled:	-120.52   -379.96   -203.32   	Heading:	4.45 Radians   	255.02 Degrees   	
Raw:	-125   -414   -222   	Scaled:	-115.00   -380.88   -204.24   	Heading:	4.46 Radians   	255.82 Degrees   	
Raw:	-133   -412   -221   	Scaled:	-122.36   -379.04   -203.32   	Heading:	4.45 Radians   	254.73 Degrees   	
Raw:	-136   -403   -220   	Scaled:	-125.12   -370.76   -202.40   	Heading:	4.43 Radians   	253.97 Degrees   	
Raw:	-131   -403   -220   	Scaled:	-120.52   -370.76   -202.40   	Heading:	4.44 Radians   	254.61 Degrees   	
Raw:	-123   -402   -223   	Scaled:	-113.16   -369.84   -205.16   	Heading:	4.46 Radians   	255.61 Degrees   	
Raw:	-108   -397   -225   	Scaled:	-99.36   -365.24   -207.00   	Heading:	4.49 Radians   	257.40 Degrees   	
Raw:	-106   -398   -223   	Scaled:	-97.52   -366.16   -205.16   	Heading:	4.50 Radians   	257.70 Degrees   	
Raw:	-99   -379   -225   	Scaled:	-91.08   -348.68   -207.00   	Heading:	4.50 Radians   	257.98 Degrees   	
Raw:	-75   -368   -224   	Scaled:	-69.00   -338.56   -206.08   	Heading:	4.56 Radians   	261.10 Degrees   	
Raw:	-77   -372   -222   	Scaled:	-70.84   -342.24   -204.24   	Heading:	4.55 Radians   	260.92 Degrees   	
Raw:	-88   -345   -225   	Scaled:	-80.96   -317.40   -207.00   	Heading:	4.51 Radians   	258.31 Degrees   	
Raw:	-92   -319   -226   	Scaled:	-84.64   -293.48   -207.92   	Heading:	4.48 Radians   	256.53 Degrees   	
Raw:	-92   -309   -226   	Scaled:	-84.64   -284.28   -207.92   	Heading:	4.47 Radians   	256.04 Degrees   	
Raw:	-94   -294   -222   	Scaled:	-86.48   -270.48   -204.24   	Heading:	4.45 Radians   	254.89 Degrees   	
Raw:	-121   -258   -220   	Scaled:	-111.32   -237.36   -202.40   	Heading:	4.32 Radians   	247.49 Degrees   	
Raw:	-119   -255   -218   	Scaled:	-109.48   -234.60   -200.56   	Heading:	4.32 Radians   	247.60 Degrees   	
Raw:	-119   -245   -214   	Scaled:	-109.48   -225.40   -196.88   	Heading:	4.31 Radians   	246.71 Degrees   	
Raw:	-131   -237   -215   	Scaled:	-120.52   -218.04   -197.80   	Heading:	4.25 Radians   	243.69 Degrees   	
Raw:	-135   -236   -214   	Scaled:	-124.20   -217.12   -196.88   	Heading:	4.24 Radians   	242.85 Degrees   	
Raw:	-135   -238   -213   	Scaled:	-124.20   -218.96   -195.96   	Heading:	4.24 Radians   	243.06 Degrees   	
Raw:	-141   -229   -210   	Scaled:	-129.72   -210.68   -193.20   	Heading:	4.21 Radians   	241.00 Degrees   	
Raw:	-133   -227   -207   	Scaled:	-122.36   -208.84   -190.44   	Heading:	4.23 Radians   	242.25 Degrees   	
Raw:	-132   -229   -210   	Scaled:	-121.44   -210.68   -193.20   	Heading:	4.24 Radians   	242.66 Degrees   	
Raw:	-141   -221   -206   	Scaled:	-129.72   -203.32   -189.52   	Heading:	4.19 Radians   	240.08 Degrees   	
Raw:	-144   -225   -204   	Scaled:	-132.48   -207.00   -187.68   	Heading:	4.19 Radians   	240.00 Degrees   	
Raw:	-132   -227   -209   	Scaled:	-121.44   -208.84   -192.28   	Heading:	4.23 Radians   	242.44 Degrees   	
Raw:	-127   -227   -209   	Scaled:	-116.84   -208.84   -192.28   	Heading:	4.25 Radians   	243.39 Degrees   	
Raw:	-124   -228   -209   	Scaled:	-114.08   -209.76   -192.28   	Heading:	4.26 Radians   	244.08 Degrees   	
Raw:	-118   -231   -208   	Scaled:	-108.56   -212.52   -191.36   	Heading:	4.29 Radians   	245.56 Degrees   	
Raw:	-117   -227   -208   	Scaled:	-107.64   -207.92   -191.36   	Heading:	4.28 Radians   	245.25 Degrees   	
Raw:	-124   -228   -209   	Scaled:	-114.08   -209.76   -192.28   	Heading:	4.26 Radians   	244.08 Degrees   	
Raw:	-132   -231   -208   	Scaled:	-121.44   -212.52   -191.36   	Heading:	4.24 Radians   	242.87 Degrees   	
Raw:	-129   -231   -211   	Scaled:	-118.68   -212.52   -194.12   	Heading:	4.25 Radians   	243.44 Degrees   	
Raw:	-127   -235   -210   	Scaled:	-116.84   -216.20   -193.20   	Heading:	4.26 Radians   	244.23 Degrees   	
Raw:	-135   -247   -211   	Scaled:	-124.20   -227.24   -194.12   	Heading:	4.26 Radians   	243.96 Degrees   	
Raw:	-128   -240   -212   	Scaled:	-117.76   -220.80   -195.04   	Heading:	4.27 Radians   	244.55 Degrees   	
Raw:	-124   -237   -212   	Scaled:	-125.12   -225.40   -194.12   	Heading:	4.25 Radians   	243.58 Degrees   	
Raw:	-159   -258   -212   	Scaled:	-146.28   -237.36   -195.04   	Heading:	4.21 Radians   	240.97 Degrees   	
Raw:	-156   -266   -213   	Scaled:	-143.52   -244.72   -195.96   	Heading:	4.23 Radians   	242.23 Degrees   	
Raw:	-156   -275   -213   	Scaled:	-143.52   -253.00   -195.96   	Heading:	4.24 Radians   	243.05 Degrees   	
Raw:	-158   -279   -213   	Scaled:	-145.36   -256.68   -195.96   	Heading:	4.24 Radians   	243.10 Degrees   	
Raw:	-153   -284   -212   	Scaled:	-140.76   -261.28   -195.04   	Heading:	4.26 Radians   	244.31 Degrees   	
Raw:	-155   -283   -214   	Scaled:	-142.60   -260.36   -196.88   	Heading:	4.26 Radians   	243.91 Degrees   	
Raw:	-167   -292   -214   	Scaled:	-161.92   -268.64   -196.88   	Heading:	4.22 Radians   	241.54 Degrees   	
Raw:	-178   -278   -212   	Scaled:	-163.76   -255.76   -195.04   	Heading:	4.19 Radians   	239.99 Degrees   	
Raw:	-188   -279   -212   	Scaled:	-172.96   -256.68   -195.04   	Heading:	4.17 Radians   	238.64 Degrees   	
Raw:	-195   -281   -209   	Scaled:	-179.40   -258.52   -192.28   	Heading:	4.15 Radians   	237.86 Degrees   	
Raw:	-199   -287   -212   	Scaled:	-183.08   -264.04   -195.04   	Heading:	4.15 Radians   	237.88 Degrees   	
Raw:	-197   -301   -213   	Scaled:	-181.24   -276.92   -195.96   	Heading:	4.18 Radians   	239.41 Degrees   	
Raw:	-161   -316   -226   	Scaled:	-148.12   -290.72   -207.92   	Heading:	4.29 Radians   	245.62 Degrees   	
Raw:	-164   -317   -229   	Scaled:	-150.88   -291.64   -210.68   	Heading:	4.28 Radians   	245.26 Degrees   	
Raw:	-174   -295   -223   	Scaled:	-160.08   -271.40   -205.16   	Heading:	4.23 Radians   	242.09 Degrees   	
Raw:	-211   -261   -207   	Scaled:	-194.12   -240.12   -190.44   	Heading:	4.08 Radians   	233.67 Degrees   	
Raw:	-242   -259   -192   	Scaled:	-222.64   -238.28   -176.64   	Heading:	4.01 Radians   	229.56 Degrees   	
Raw:	-224   -286   -203   	Scaled:	-206.08   -263.12   -186.76   	Heading:	4.09 Radians   	234.55 Degrees   	
Raw:	-223   -289   -205   	Scaled:	-205.16   -265.88   -188.60   	Heading:	4.10 Radians   	234.96 Degrees   	
Raw:	-200   -293   -211   	Scaled:	-184.00   -269.56   -194.12   	Heading:	4.16 Radians   	238.30 Degrees   	
Raw:	-192   -298   -212   	Scaled:	-176.64   -274.16   -195.04   	Heading:	4.19 Radians   	239.82 Degrees   	
Raw:	-196   -289   -214   	Scaled:	-180.32   -265.88   -196.88   	Heading:	4.16 Radians   	238.47 Degrees   	
Raw:	-186   -281   -215   	Scaled:	-171.12   -258.52   -197.80   	Heading:	4.17 Radians   	239.12 Degrees   	
Raw:	-178   -273   -211   	Scaled:	-163.76   -251.16   -194.12   	Heading:	4.18 Radians   	239.51 Degrees   	
Raw:	-178   -277   -216   	Scaled:	-163.76   -254.84   -198.72   	Heading:	4.19 Radians   	239.89 Degrees   	
Raw:	-180   -290   -213   	Scaled:	-165.60   -266.80   -195.96   	Heading:	4.20 Radians   	240.79 Degrees   	
Raw:	-174   -328   -216   	Scaled:	-160.08   -301.76   -198.72   	Heading:	4.27 Radians   	244.67 Degrees   	
Raw:	-161   -350   -223   	Scaled:	-148.12   -322.00   -205.16   	Heading:	4.33 Radians   	247.92 Degrees   	
Raw:	-155   -356   -221   	Scaled:	-142.60   -327.52   -203.32   	Heading:	4.35 Radians   	249.09 Degrees   	
Raw:	-159   -355   -219   	Scaled:	-146.28   -326.60   -201.48   	Heading:	4.34 Radians   	248.49 Degrees   	
Raw:	-144   -370   -218   	Scaled:	-132.48   -340.40   -200.56   	Heading:	4.39 Radians   	251.35 Degrees   	
Raw:	-133   -373   -222   	Scaled:	-122.36   -343.16   -204.24   	Heading:	4.42 Radians   	252.99 Degrees   	
Raw:	-127   -372   -216   	Scaled:	-116.84   -342.24   -198.72   	Heading:	4.43 Radians   	253.77 Degrees   	
Raw:	-126   -378   -222   	Scaled:	-115.92   -347.76   -204.24   	Heading:	4.44 Radians   	254.18 Degrees   	
Raw:	-126   -382   -219   	Scaled:	-115.92   -351.44   -201.48   	Heading:	4.44 Radians   	254.36 Degrees   	
Raw:	-126   -385   -215   	Scaled:	-115.92   -354.20   -197.80   	Heading:	4.44 Radians   	254.50 Degrees   	
Raw:	-126   -380   -219   	Scaled:	-115.92   -349.60   -201.48   	Heading:	4.44 Radians   	254.27 Degrees   	
Raw:	-120   -408   -214   	Scaled:	-110.40   -375.36   -196.88   	Heading:	4.47 Radians   	256.23 Degrees   	
Raw:	-122   -406   -220   	Scaled:	-112.24   -373.52   -202.40   	Heading:	4.47 Radians   	255.89 Degrees   	
Raw:	-118   -427   -208   	Scaled:	-108.56   -392.84   -191.36   	Heading:	4.49 Radians   	257.17 Degrees   	
Raw:	-139   -403   -217   	Scaled:	-127.88   -370.76   -199.64   	Heading:	4.43 Radians   	253.59 Degrees   	
Raw:	-136   -406   -214   	Scaled:	-125.12   -373.52   -196.88   	Heading:	4.43 Radians   	254.10 Degrees   	
Raw:	-138   -416   -210   	Scaled:	-126.96   -382.72   -193.20   	Heading:	4.44 Radians   	254.27 Degrees   	
Raw:	-146   -421   -206   	Scaled:	-134.32   -387.32   -189.52   	Heading:	4.42 Radians   	253.49 Degrees   	
Raw:	-147   -428   -201   	Scaled:	-135.24   -393.76   -184.92   	Heading:	4.43 Radians   	253.66 Degrees   	
Raw:	-150   -426   -204   	Scaled:	-138.00   -391.92   -187.68   	Heading:	4.42 Radians   	253.22 Degrees   	
Raw:	-153   -427   -203   	Scaled:	-140.76   -392.84   -186.76   	Heading:	4.41 Radians   	252.91 Degrees   	
Raw:	-157   -425   -199   	Scaled:	-144.44   -391.00   -183.08   	Heading:	4.40 Radians   	252.34 Degrees   	
Raw:	-152   -430   -203   	Scaled:	-139.84   -395.60   -186.76   	Heading:	4.42 Radians   	253.15 Degrees   	
Raw:	-152   -429   -200   	Scaled:	-139.84   -394.68   -184.00   	Heading:	4.42 Radians   	253.11 Degrees   	
Raw:	-151   -436   -200   	Scaled:	-138.92   -401.12   -184.00   	Heading:	4.42 Radians   	253.52 Degrees   	
Raw:	-151   -427   -200   	Scaled:	-138.92   -392.84   -184.00   	Heading:	4.42 Radians   	253.14 Degrees   	
Raw:	-151   -412   -198   	Scaled:	-138.92   -379.04   -182.16   	Heading:	4.41 Radians   	252.49 Degrees   	
Raw:	-157   -391   -202   	Scaled:	-144.44   -359.72   -185.84   	Heading:	4.38 Radians   	250.74 Degrees   	
Raw:	-152   -366   -209   	Scaled:	-139.84   -336.72   -192.28   	Heading:	4.36 Radians   	250.07 Degrees   	
Raw:	-130   -356   -210   	Scaled:	-119.60   -327.52   -193.20   	Heading:	4.41 Radians   	252.56 Degrees   	
Raw:	-116   -349   -209   	Scaled:	-106.72   -321.08   -192.28   	Heading:	4.44 Radians   	254.23 Degrees   	
Raw:	-108   -344   -212   	Scaled:	-99.36   -316.48   -195.04   	Heading:	4.45 Radians   	255.19 Degrees   	
Raw:	-100   -346   -203   	Scaled:	-92.00   -318.32   -186.76   	Heading:	4.48 Radians   	256.50 Degrees   	
Raw:	-99   -359   -201   	Scaled:	-91.08   -330.28   -184.92   	Heading:	4.49 Radians   	257.20 Degrees   	
Raw:	-82   -332   -201   	Scaled:	-75.44   -305.44   -184.92   	Heading:	4.52 Radians   	258.74 Degrees   	
Raw:	-72   -325   -200   	Scaled:	-66.24   -299.00   -184.00   	Heading:	4.54 Radians   	260.13 Degrees   	
Raw:	-57   -323   -202   	Scaled:	-52.44   -297.16   -185.84   	Heading:	4.58 Radians   	262.61 Degrees   	
Raw:	-69   -326   -202   	Scaled:	-63.48   -299.92   -185.84   	Heading:	4.55 Radians   	260.67 Degrees   	
Raw:	-77   -322   -199   	Scaled:	-70.84   -296.24   -183.08   	Heading:	4.52 Radians   	259.17 Degrees   	
Raw:	-92   -308   -199   	Scaled:	-84.64   -283.36   -183.08   	Heading:	4.47 Radians   	255.99 Degrees   	
Raw:	-86   -301   -198   	Scaled:	-79.12   -276.92   -182.16   	Heading:	4.48 Radians   	256.67 Degrees   	
Raw:	-71   -307   -202   	Scaled:	-65.32   -282.44   -185.84   	Heading:	4.53 Radians   	259.60 Degrees   	
Raw:	-67   -315   -202   	Scaled:	-61.64   -289.80   -185.84   	Heading:	4.55 Radians   	260.61 Deg

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Triple-axis Magnetometer HMC5883L- Need help calculating

Post by adafruit_support_bill »

That's not one our libraries. Please try the one linked in the tutorial: https://learn.adafruit.com/adafruit-hmc ... g-and-test

You will need both our unified sensor driver and the HMC5883 driver.
https://github.com/adafruit/Adafruit_Sensor
https://github.com/adafruit/Adafruit_HMC5883_Unified

User avatar
molex1701
 
Posts: 54
Joined: Mon Feb 25, 2013 6:34 pm

Re: Triple-axis Magnetometer HMC5883L- Need help calculating

Post by molex1701 »

Oh sorry I've gotten that confuse with my many attempts of getting this to work.

Code: Select all

/***************************************************************************
  This is a library example for the HMC5883 magnentometer/compass

  Designed specifically to work with the Adafruit HMC5883 Breakout
  http://www.adafruit.com/products/1746
 
  *** You will also need to install the Adafruit_Sensor library! ***

  These displays use I2C to communicate, 2 pins are required to interface.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!

  Written by Kevin Townsend for Adafruit Industries with some heading example from
  Love Electronics (loveelectronics.co.uk)
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the version 3 GNU General Public License as
 published by the Free Software Foundation.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or F$I$T$N$E$SS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

 ***************************************************************************/

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>

/* Assign a unique ID to this sensor at the same time */
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);

void displaySensorDetails(void)
{
  sensor_t sensor;
  mag.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" uT");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" uT");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" uT");  
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

void setup(void) 
{
  Serial.begin(9600);
  Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
  
  /* Initialise the sensor */
  if(!mag.begin())
  {
    /* There was a problem detecting the HMC5883 ... check your connections */
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
    while(1);
  }
  
  /* Display some basic information on this sensor */
  displaySensorDetails();
}

void loop(void) 
{
  /* Get a new sensor event */ 
  sensors_event_t event; 
  mag.getEvent(&event);
 
  /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
  Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print("  ");
  Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print("  ");Serial.println("uT");

  // Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2(event.magnetic.y, event.magnetic.x);
  
  // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  // Find yours here: http://www.magnetic-declination.com/
  // Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
  // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  float declinationAngle = 0.22;
  heading += declinationAngle;
  
  // Correct for when signs are reversed.
  if(heading < 0)
    heading += 2*PI;
    
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)
    heading -= 2*PI;
   
  // Convert radians to degrees for readability.
  float headingDegrees = heading * 180/M_PI; 
  
  Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
  
  delay(500);
}
Again started with X arrow pointing north and rotated to the right.

Code: Select all

`Sensor:       HMC5883
Driver Ver:   1
Unique ID:    12345
Max Value:    800.00 uT
Min Value:    -800.00 uT
Resolution:   0.20 uT
------------------------------------

X: -7.18  Y: -30.91  Z: -21.63  uT
Heading (degrees): 269.52
X: -4.82  Y: -26.27  Z: -21.33  uT
Heading (degrees): 272.21
X: -7.36  Y: -22.36  Z: -19.59  uT
Heading (degrees): 264.38
X: -13.18  Y: -28.09  Z: -20.10  uT
Heading (degrees): 257.47
X: -14.09  Y: -27.09  Z: -19.29  uT
Heading (degrees): 255.12
X: -16.55  Y: -28.55  Z: -20.20  uT
Heading (degrees): 252.51
X: -14.55  Y: -25.91  Z: -21.22  uT
Heading (degrees): 253.30
X: -15.82  Y: -24.91  Z: -20.82  uT
Heading (degrees): 250.19
X: -18.64  Y: -26.45  Z: -19.80  uT
Heading (degrees): 247.44
X: -17.73  Y: -28.64  Z: -20.71  uT
Heading (degrees): 250.85
X: -16.91  Y: -32.45  Z: -21.43  uT
Heading (degrees): 255.09
X: -17.36  Y: -30.27  Z: -21.73  uT
Heading (degrees): 252.77
X: -20.55  Y: -32.91  Z: -20.31  uT
Heading (degrees): 250.63
X: -17.36  Y: -34.36  Z: -21.02  uT
Heading (degrees): 255.80
X: -14.55  Y: -35.55  Z: -20.71  uT
Heading (degrees): 260.35
X: -11.82  Y: -35.91  Z: -21.43  uT
Heading (degrees): 264.39
X: -10.09  Y: -33.73  Z: -22.55  uT
Heading (degrees): 265.95
X: -9.55  Y: -30.27  Z: -23.37  uT
Heading (degrees): 265.10
X: -8.27  Y: -26.36  Z: -22.65  uT
Heading (degrees): 265.18
X: -5.55  Y: -27.18  Z: -22.45  uT
Heading (degrees): 271.07
X: -6.55  Y: -29.45  Z: -23.16  uT
Heading (degrees): 270.08
X: -7.09  Y: -30.18  Z: -22.65  uT
Heading (degrees): 269.38
X: -7.09  Y: -30.55  Z: -22.86  uT
Heading (degrees): 269.54

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Triple-axis Magnetometer HMC5883L- Need help calculating

Post by adafruit_support_bill »

Those are some mighty strange readings. Let's try replacing the sensor. If you contact [email protected] with a link to this thread we can send out a replacement.

User avatar
molex1701
 
Posts: 54
Joined: Mon Feb 25, 2013 6:34 pm

Re: Triple-axis Magnetometer HMC5883L- Need help calculating

Post by molex1701 »

Ok, Thanks for your time. Will get request in.

User avatar
molex1701
 
Posts: 54
Joined: Mon Feb 25, 2013 6:34 pm

Re: Triple-axis Magnetometer HMC5883L- Need help calculating

Post by molex1701 »

Hello,
Received my replacement compass but had same trouble. Did You notice on the picture that my breadboard is on a metal plate with power connectors? I bring up because I noticed in the other persons thread that he removed compass from a similar board. I didn't have a separate board to try so I solder wires direct to the board and plugged right into Arduino.. Now I seem to get readings that make sense. Had you guys tested it with the breadboard with a metal plate because the compass doesn't seem to like that?

Well now that I have readings how do I convert offset?
Magnetic declination: -7° 18' WEST

That means that magnetic North in your area is 7° 18' West of True North. So you simply need to add 7° 18' to your compass reading to find True North.
I don' get what to do with the 18' does that come part of the 7 degrees? As I said I'm poor with math and not sure what that 18' means. Thanks.

I've also test the original compass I received this way and it works too. Would you like me to return one since now I have two that work?

Which heading is suppose to be north, Zero degrees?

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Triple-axis Magnetometer HMC5883L- Need help calculating

Post by adafruit_support_bill »

Had you guys tested it with the breadboard with a metal plate because the compass doesn't seem to like that?
Is the plate steel? The one I have here is aluminum and doesn't seem to affect readings like that.

Zero degrees is North. Angles are reported in degrees, minutes and seconds, where minutes are 1/60 of a degree and seconds are 1/60 of a minute. So if your declination is -7° 18' WEST, that is -(7 + 18/60) or -7.3 degrees. So just add 7.3 to the reading.

Don't worry about the extra magnetometer. You can keep it for another project.

User avatar
molex1701
 
Posts: 54
Joined: Mon Feb 25, 2013 6:34 pm

Re: Triple-axis Magnetometer HMC5883L- Need help calculating

Post by molex1701 »

I can't tell if steel or aluminum. I got it apart of the Makershed electronics kit 1.

Welp for first project I was going to make is a compass for my car. I don't like those cheep analog ones and thought one with leds would show up better when I drive at night anyhow.
Right now I'll probably just use 8 leds and I want to use a shift register to output to them for practice. I'm still learning different parts.

I'll want to get one of those pixie rings later.

I'll try to come up with another project for other one. Maybe put it on a robot.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”