HMS 5883 L mag Calibration?

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
kpurzycki
 
Posts: 108
Joined: Mon Jan 13, 2014 11:46 am

HMS 5883 L mag Calibration?

Post by kpurzycki »

I just received my HMC5883L mag. The manual and everything was great. When I hooked it up to my Arduino Un0 rev3 the 5883 does not display 0 degrees when the X axis is pointing in the direction of magnetic North. (determined using both an electronic and magnetic compass) The mag display is 345 degrees For magnetic North. ( about 15 degrees off). The sketch was copied from the manual and pasted directly. I also modified the sketch from the LSM303 manual. Both sketches give the some readings. What is more frustration is that subtracting 90 degrees from the heading (ex 345) and rotate the mag to the left 90 degrees the MAG displays 292 instead of 255 (345-90=255 ). Similarly when 180 Degrees is subtracted from heading (345) the mag reads 242 instead of 165 (345-180=165). My angle of delineation is the same as the manual 0.22 rad.
Can you suggest something else to try to get the Mag to read O degrees when pointed to mag north ?
Is the math in the Sketch correct? What is M_PI? I assume it is the value of Pi 3.14159 but the variable was never assigned.
I think the mag needs to be recalibrated. How do you calibrate the 5883.

My goal is to use a compass to steer robots etc. It looks like a lot of thought went into the library but is there a simple calibration fix?

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: HMS 5883 L mag Calibration?

Post by adafruit_support_mike »

You probably need to adjust for your local magnetic declination.

The example sketch has a couple of lines to do that:

Code: Select all

  // 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;
but the value of 0.22 used in the sketch is almost certainly wrong for where you are.

User avatar
kpurzycki
 
Posts: 108
Joined: Mon Jan 13, 2014 11:46 am

Re: HMS 5883 L mag Calibration?

Post by kpurzycki »

I think I did everything you stated. Below is my code and results obtained from the HMC5883 x pointing mag North
Due North.
The 5883 reads 310 instead of 0 degrees. There must be a silly mistake in the sketch or math. I would appreciate your review and hope you can help me get the 5883 to work properly by displaying 0 degrees when X is pointed due North .

Code: Select all

/*
 HMC5883 magnentometer/compass
  Designed specifically to work with the Adafruit HMC5883 Breakout
  http://www.adafruit.com/products/1746 
*/

#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 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);
  }
}

void loop(void) 
{
  /* Get a new sensor event */ 
  sensors_event_t event; 
  mag.getEvent(&event);
  float declinationAngle = 0.22; ////Parsippany -12 deg 50 min west = 0.2182  or -22.22 radians= -12.605 degrees
  float heading = atan2(event.magnetic.y, event.magnetic.x)-declinationAngle; //values are radians? 
  // 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;
    
  float headingDegrees = (heading * 180/M_PI); // Convert radians to degrees for readability. ?M_PI
  
  Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
  delay(1000);
}
Values returned for HMC5883 Pointing due mag North

I do Not know how to attach a JPG image the data looks as follows:
Heading (degrees): 309.97
Heading (degrees): 310.10
Heading (degrees): 309.81
Heading (degrees): 310.45
Heading (degrees): 310.21
Heading (degrees): 309.73
Heading (degrees): 310.25
Heading (degrees): 310.26
Last edited by adafruit_support_mike on Mon Sep 22, 2014 1:34 am, edited 1 time in total.
Reason: added CODE tags to preserve formatting

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: HMS 5883 L mag Calibration?

Post by adafruit_support_mike »

Hmm.. a 50 degree error is pretty significant.

Could you post a set of raw magnetometer X and Y readings please? Let's see if the sensor is doing something wacky.

BTW - the constant M_PI is declared in the "math.h" header file. It's common practice to put a file-specific prefix on library constants so they don't collide with values in other libraries.

User avatar
kpurzycki
 
Posts: 108
Joined: Mon Jan 13, 2014 11:46 am

Re: HMS 5883 L mag Calibration?

Post by kpurzycki »

hi
When I select insert an image I get ??
Is it possible to attach a document with the data??
How do I attach a JPG file with the data??

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: HMS 5883 L mag Calibration?

Post by adafruit_support_mike »

The best way to post the readings would be to copy the text in the Arduino IDE Serial Monitor and paste it into a post. Putting CODE tags around it will preserve the formatting so it's easier to see how things line up.

To attach an image to the post, you can use the 'File' button below the text area where you compose a message. It will be below the 'SUBMIT' button. Click the 'Choose File' button, select the image you want to add to your post, then click 'Add The File'.

The message entry page will reload, and you'll have a couple of new items below the text area: one will say "Place Inline", which you can use to put the image anywhere in the post. The other will say 'Remove File', which you can use if you decide against posting that specific item.

User avatar
kpurzycki
 
Posts: 108
Joined: Mon Jan 13, 2014 11:46 am

Re: HMS 5883 L mag Calibration?

Post by kpurzycki »

Hi
I attached the data you requested as a JPG file.

I cannot believe that Adafruit would sell a compass which was not reliable and proven to work. The use of declination angle correction only changes the bearing by -12 to -13 degrees. I cannot understand why if you add or subtract 90 degrees from whatever value you get for North and you adjust the compass to read -90 or + 90 you do not get a bearing which is + or -90 units from the original bearing.in most cases the result is of by 30-40 degrees.

It seems like the math does not work or the formula used to calculate the heading based on X, Y and Z mag values is nor correct??

I hope the attached data gives you a clue what's wrong.

Have you ever used this 5883 compass?
Attachments
XYZ Data.jpg
XYZ Data.jpg (183.71 KiB) Viewed 1589 times

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: HMS 5883 L mag Calibration?

Post by adafruit_support_mike »

I have one running on my desk right now. It works as it should. We sell hundreds of them, and this is the first time I've heard of one being that far off without external interference.

The heading calculation is nothing more than an inverse tangent function, and the raw values are clearly wrong for a compass pointing north. The Y component should be zero with the compass pointed toward magnetic north, and about 5.4 when it's pointed toward true north.

I'm leaning toward the opinion that you have a bad sensor, but let's check a couple of other things to make sure.

First, turn the board until the Y component is reasonably close to zero. Check to see if there are any magnetic sources in the X direction.

Second, take readings in two or three other places physically remote from where you're testing the device now. If the readings stay generally the same regardless of location, we'll call this one a dud and get you a replacement.

User avatar
kpurzycki
 
Posts: 108
Joined: Mon Jan 13, 2014 11:46 am

Re: HMS 5883 L mag Calibration?

Post by kpurzycki »

The following data is from 3 location about 1 mile apart.
Location 1
x. -13.36 thru -14.09
y -.09 thru 0.27
z -45-82 thru -46.02

Location 2
x - 8.73 thru -9.18
y 0.27 thru 0.55
z -46.22 thru -46.63

Location 3
x 5.09 thru 5.64
y 0.09 thru 0.36
z -73.57 thru -73.88

I also attached a photo showing the 5883 X and a compass pointing north the 5883 heading was 310 degrees
compass pointing north.jpg

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: HMS 5883 L mag Calibration?

Post by adafruit_support_mike »

Okay, let's call that a bad sensor.

Send a note to [email protected] with a link to this thread and the folks there will get you a new HMS5883 breakout.

User avatar
kpurzycki
 
Posts: 108
Joined: Mon Jan 13, 2014 11:46 am

Re: HMS 5883 L mag Calibration?

Post by kpurzycki »

Hi
How do I get to ??


I really appreciate your prompt response to my problem and the suggestions to try to determine what the people was with the 5883L.

When I type in in I get to the support_forum which asks me select a forum to visit. I must have made a mistake. What is the thread? Can you tell me instructions on how to get to Support_adafruit.com or better yet can you forward the following info order confirmation no.579785-9024223527.
When can I expect delivery? Will I be notified that it was delivered?

Thanks again for you curious and prompt response

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: HMS 5883 L mag Calibration?

Post by adafruit_support_mike »

Send an email message to the address [email protected].

Include a link to this thread in the email. The folks there will handle the replacement, and you should receive an email from UPS with a tracking code as soon as the package ships.

User avatar
kpurzycki
 
Posts: 108
Joined: Mon Jan 13, 2014 11:46 am

Re: HMS 5883 L mag Calibration?

Post by kpurzycki »

HI
Just a follow up. I received a new HMC5883L magnetometer. I soldered on a female header and attached it to an Arduino UNO. Ran magsensor sketch and when the X axis was pointed in the same direction as Mag North using a magnetic compass the bearing was approximately 0 degrees. See attached data for new sensor. To check I replaced the new sensor with the old sensor and ran magsensor sketch. When the x axis is pointed north the bearing was approx. 341 instead of 0 degrees. See attached data for old sensor.

Your diagnostics that the original sensor was bad was right on. I am glad I purchased this unit from Adifruit. I do not believe other suppliers of magnetometer would have been very helpful.

Thanks again for your prompt courteous assistance.
Attachments
5883 old North.jpg
5883 old North.jpg (170.05 KiB) Viewed 1528 times
5883 new North.jpg
5883 new North.jpg (167.89 KiB) Viewed 1528 times

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: HMS 5883 L mag Calibration?

Post by adafruit_support_mike »

Glad to hear the new one is working for you.

User avatar
kpurzycki
 
Posts: 108
Joined: Mon Jan 13, 2014 11:46 am

Re: HMS 5883 L mag Calibration?

Post by kpurzycki »

Hi I think I celebrated to early. I wanted to know the variance between the bearings in N, E, S, and W. (See photo of mag compass and both pointed due North. The same was done for each heading The results were not what I expected (see data for each direction. (using the supplied example sketch ( magsensor) & Arduino UNO)
5883 Direction, bearing. Compass Heading. Degrees off
North 354.56 to 355.11. 0 -6
East. 85.10 to 85.91. 90. -5
South. 219.94 to 220.82. 180 +40
West. 290.6 to 291.36. 270 +21

What values do you get for the when you compare the 5883 to a compass?
I did not change angle of delineation. Mine is -12deg. 15 min west or 0.2182 radiant. Since this was close to yours 0.22 I did not change it.
Am I expecting to much from the 5883 . Yes when you point it North it is within 5 degrees of mag north. However when you point it South it is off by 40 degrees. I repeated this several times each time uploading the program and then starting the Serial print. A similar error was found when pointed west. Can I be doing something wrong. Can you give me better instructions on how to get accurate bearings in all directions.
Attachments
5883 SOUTH.jpg
5883 SOUTH.jpg (181.11 KiB) Viewed 1516 times
5883 EAST.jpg
5883 EAST.jpg (179.62 KiB) Viewed 1516 times
5883 NORTH.jpg
5883 NORTH.jpg (173.29 KiB) Viewed 1516 times

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

Return to “Arduino Shields from Adafruit”