element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Sensors
  • Technologies
  • More
Sensors
Sensor Forum Help with 5883L crazyness.
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Sensors to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 16 replies
  • Answers 1 answer
  • Subscribers 318 subscribers
  • Views 3506 views
  • Users 0 members are here
  • 5883l
Related

Help with 5883L crazyness.

screamingtiger
screamingtiger over 9 years ago

shabaz

jw0752

 

I have collected data and cannot get it to point to the correct heading.  Its off quite a bit and I cannot adjust as the amount it is off is non-linear.  For example, at 0 degrees it shows 100 degrees.  But at 45 degrees it shows 170 degrees.  Z axis is level and remain constant for the most part.

 


Here is the data I collected, does it makes sense?  I used a compass on my phone and compared to the readings I am getting.

 

 

 

0 degrees North
X 0
Y 155
Z -530

 

Calculated heading: 100

 

 

90 degrees east

 

X -165
Y 100 (shouldn't this read 0 since it is pointing in same direction X was just at?)
Z -530
Calculated Heading: 224

 


180 degress South
X: 0
Y: -290
Z: -540

 

Calculating Heading: 286

 

 

 

270 degrees West

 

X: 250
Y: -100
Z: -550
Calculated Heading: 252

 


One thing I am thinking is odd, if at 0 degrees north X axis reads 0, if I rotate 90 degrees to right, would Y axis now read 0?

 

I am not sure what the axis is measuring, I thought it was force and it I point x axis in one direction, then Y axis in same direction, they would show the same reading...

 

X and Y are 90 degrees in parallel plane.
I ordered another magnometer hoping a different brand will help.  Its still 5883L but different maker of breakout.

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 9 years ago in reply to screamingtiger +2
    Hi Joey, I took your code and added ALG_TEST stuff to it and modifying the declinationAngle variable: //#include "heading.h" #include <stdio.h> #include <iostream> //#include <wiringPi.h> //#include…
  • jw0752
    jw0752 over 9 years ago +1
    Hi Joey, This may be off base but if I treat your X and Y data as vectors and calculate Result = Sqrt( X^2 + Y^2) I get a Result that is at least in the ball park for the headings that you listed. Like…
  • jw0752
    jw0752 over 9 years ago +1
    Ok now you can see I really don't grasp it quickly as that is exactly what you were doing. No wonder my calculations matched yours. I will put my thinking cap back on and let you know if something better…
  • jw0752
    0 jw0752 over 9 years ago

    Hi Joey,

    This may be off base but if I treat your X and Y data as vectors and calculate Result = Sqrt( X^2 + Y^2) I get a Result that is at least in the ball park for the headings that you listed.  Like I said I have no experience with these devices but it would make sense that they would give output in a vector format.

    John

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • jw0752
    0 jw0752 over 9 years ago

    Ok now you can see I really don't grasp it quickly as that is exactly what you were doing. No wonder my calculations matched yours. I will put my thinking cap back on and let you know if something better pops out.

    John

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • jw0752
    0 jw0752 over 9 years ago

    Is there a correction factor necessary for the angle of the Earth's magnetic field in your area?

     

    The Data Sheet mentions a calibration factor that can be calculated using the self test.

     

     

    HMC5883L 18 www.honeywell.com SCALE FACTOR CALIBRATION

     

     

     

     

     

     

    Using the self test method described above, the user can scale sensors’ sensitivity to match each other. Since placing device in positive bias mode (or alternatively negative bias mode) applies a known artificial field on all three axes, the resulting ADC measurements in data output registers can be used to scale the sensors. For example, if the expected self test value for X-axis is 766 and the actual value is 750 then a scale factor of (766/750) should be multiplied to all future readings of X-axis. Doing so for all three axes will ensure their sensitivity are well matched,

     

     

     

    The built-in self test can also be used to periodically compensate the scaling errors due to temperature variations. A compensation factor can be found by comparing the self test outputs with the ones obtained at a known temperature. For example, if the self test output is 750 at room temperature and 700 at the current temperature then a compensation factor of (750/700) should be applied to all current magnetic readings. A temperature sensor is not required using this method.

     

     

     

    John

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • screamingtiger
    0 screamingtiger over 9 years ago in reply to jw0752

    Hi John, the magnetic declination in my area is HERE:

    http://www.magnetic-declination.com/

     

    2 degrees 46 minutes

     

    I am basically using the code form here:

    https://learn.adafruit.com/adafruit-hmc5883l-breakout-triple-axis-magnetometer-compass-sensor/wiring-and-test

     

    The only difference is I am not using Arduino, but I confirmed the X,Y and Z values do match.  I am going to check to trig function though to ensure they match.  I am using ATAN2.

     

     

    Basically its:

    heading = atan2(fy,fx)

     

    float declinationAngle = 0.22;  //I need to put 2.5 here but it wont make a difference.
            heading += declinationAngle;
            if(heading < 0)
            heading += 2*PI;
            if(heading > 2*PI)
            heading -= 2*PI;

     

    currentHeadingDegrees = heading * 180/M_PI;

     

    M_PI Is built in and defined as 3.14159265358979323846

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • screamingtiger
    0 screamingtiger over 9 years ago in reply to jw0752

    I'll check out the calibration, that may explain it.  I ordered another one of these since I already have 2 and one is completely junk.  So maybe 3rd time a charm?  Different brand though, same chip.

     

    The good news is that the chip is always the same, gives the same readings.  It just doesn't scale linear to the actual heading for an adjustment.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • jw0752
    0 jw0752 over 9 years ago

    I am tempted to erase my first two replies but I have learned from past experience that even bad advice can sometimes trigger an insight. If I take the dot product of the values of your test I get the following numbers:

    North:   552.20

    East      564.02

    South   612.94

    West    612.37

    If the magnetometer is measuring the strength of the field it is expected that we would have the same number with the dot product of the vectors regardless of the orientation of the magnetometer.  These numbers are close particularly the South and West. I assumed that your angles were precisely 90 degrees as the numbers were crunched using the easy Pythagorean Equation. This would seem to indicate that the magnetometer is working correctly and it is a mathematical translation problem. I am guessing that the N and E were not precisely at right angles and so my calculations would have been off for them.

    Sorry for the inundation of ideas but I use this as a problem solving method.

    John

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • jw0752
    0 jw0752 over 9 years ago

    Here is the last thought before I quit for the night. Does it make a difference for your equations which axis the chip is rotated around. From your readings it seems to me that the field is practically perpendicular to the Z sensor and that you rotated around the axis of the Z sensor. Does that make any sense?

    John

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 9 years ago

    Hi Joey,

     

    This is how I'd try to troubleshoot this - unfortunately I've not used this sensor before, but there is a typical process I would use for sensors:

    Basically, try to find known values (e.g. a worked example in the datasheet), and then plug those values into your code so that the data from the sensor

    is overwritten with your known values, so you can see if the rest of the coded algorithm behaves as expected. I do it something like this:

     

    #define ALG_TEST 0
    ... code...
    i2c_read(buf); // read into buffer the actual sensor data
    if (ALG_TEST)
      {
       buf[0]=0x11;
        buf[2]=0x22;
        buf[3]=0x33;
      }
    ... data processing ..
    ... remainder of your code ..

     

    Basically at any point during your code development you can set ALG_TEST to 1 to quickly confirm the algorithm is not broken, and set it back to 0 to go back to normal operation.

     

    The problem is finding some known values and the correct computed value. To do this, perhaps use this arduino compass web page which (about half-way down)

    has a screenshot of captured raw values, and the computed results. You could plug the raw values into your code, and check if the result matches.

    Another way I frequently use is to come up with some raw values and the expected result by checking if the datasheet has a worked example (in your case unfortunately

    the datasheet doesn't have this) or by inventing some sensor values, and writing some formula to generate the output, using (say) Excel.

    Then, at any point during testing, I can plug the raw sensor values into the spreadsheet and see what value I expect. This really helps to confirm the coded algorithm is

    working well. I do this a lot with virtually any IC. For example, here is my 555 spreadsheet:

    image

    I can plug in values into the green cells, and the red cells show the expected output for the 555. I'd suggest a similar thing

    with the IC you're using. It takes a while to create, but worth it in the long run.

    Also, with the two modules you have, do they both give similar values that match each other?

    How are they connected to the RPI - short(ish) wires? What resistors are you using for pull-ups for the I2C -

    are these already on the board, or external? Could you attach a photo of the set-up if possible?

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 9 years ago

    Also, if you can post the code then I can give it a visual inspection, in case anything seems odd.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • screamingtiger
    0 screamingtiger over 9 years ago in reply to shabaz

    Go to https://github.com/screamingtiger/QuadCOP

    And look at head.h and heading.cpp

     

    I think I have missed a stip.  there is some code that is setting up gains

    static float _hmc5883_Gauss_LSB_XY = 1100.0F;
    static float _hmc5883_Gauss_LSB_Z  = 980.0F;

    I'm not using these but I did think I confirmed I am getting same values as the arduino I was using.

    Anyways have a look at the code if you will.  Its pretty simple actually.

     

     

    The god news is that I can still have a test auto flight today.  Because my magnometer is consistent, I can use it to fly in a square pattern.  I think that will be a good test.  But would love to have this working.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube