SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By srigoa91
I am working on a project which requires the use of HMC5843. The readings are fine but i want to convert the gauss readings (x,y,z readings) to angle in degrees. How do i do it? I have uploaded a sample of the readings i obtained
You do not have the required permissions to view the files attached to this post.
By waltr
Start with Table 12 on page 12 of the data sheet.
Read the Configuration register B to determine the Gain bits for looking up in Table 12.
The Gain column of Table 12 is in Counts per milli-Gauss.

So if the Gain setting is 001 (default) the Gain is 1300 counts/mGauss.
With axis readings of: x=-298, y= 143, z=-245.
Reading divided by Gain = Magnetic strength
-298/1300 = -0.229
143/1300 = 0.11
-245/1300 = -0.188

These really don't make sense since the Earth's field strength is between 0.3 to 0.6 Gauss at the surface.
It looks as if the units are in error and the Gain column is counts/Gauss not milli-Gauss.
The vector sum of the three above is 0.316 Gauss which a reasonable value. But assumes that the Gain is at the Default value.

Note that these values are subject to the error tolerance and non-linearity per the specs on page 2.

Edit: Just re-read your question. The answer above is to calculate the actual magnetic strength from the readings whereas you wish to calculate the magnetic field vector heading.

This just requires some trig ( so with a sample reading of: x=-298, y= 143, z=-245.

These are the magnetic strength vectors for each axis in three dimensions. On a 2D Cartesian graph ( ... ate_system) plot a point for x and y and draw a line from the point to the origin. The angle of this line to the x-axis is related by the Tangent, tan angle = y/x. So the angle = arctan (y/x). arctan( 143/-298) = -25° (as calculated by Excel's ATAN function).
Now is the hard part. What does -25° mean and is this really correct. When you plotted the point (-298, 143) the point is in the upper left quadrant (quad II). The ATAN function I used returns an angle between -180° and +180° (-PI/2 to PI/2 Radians). An angle of zero degrees is the positive x-axis (x = pos, y = 0). Also if the values were x = 289, y = -143 (lower left quad) the ATAN function would return the same angle of -25°. This means that you must figure out which quadrant the vector is in and then adjust the angle by 0, 180 or -180°.

Can you take it from here?
By esklar81

What "angle" did you want to calculate? At this is a three-dimensional signal, there are three principal axes from which you could be looking. For the purpose of discussion, I'll assume you don't particularly care about angle from the vertical or its complement, the angle from the horizontal plane.

Calculating angles from x,y data requires a bit of trigonometry. As the tangent of an angle (θ (theta), for example) is defined by:
tan(θ) = y/x,
θ can be determined by taking the arctangent of each side of the equation (assuming you're interested in only the first angle that has that tangent):
arctan(tan(θ)) = arctan (y/x)
θ = arctan (y/x)
For the example above (x = -298, y = 143):
θ = arctan (143/(-298)= arctan (-0.48) = -25.63 = θ

You may have noticed a problem with this. :? That angle is in the fourth quadrant (positive x, negative y), but our data are (negative x, positive y), which is in the second quadrant. There's nothing the arctangent function can do to help you with this, it takes a single argument and (-a/b) = (a/-b) = -(a/b). Similarly, the arctangent function won't tell you if your are in the first or third quadrant, because (a/b) = (-a/-b). Therefore, you'll need to check to see if the arctangent function put you in the right quadrant and, if not, correct it by adding 180 degrees. So:
θc = θ + 180 = -25.63 +180 = 154.37
As a check, tan(154.37) = -0.48

If you're looking to use the information for navigation, I call to your attention that the standard practice is to state "azimuth", more commonly called "bearing" , in degrees clockwise from North. Therefore, you'll need to do a bit more arithmetic.

Because bearing is always a non-negative number less than 360:
In the first quadrant: bearing = 90 - θi
In the second quadrant: bearing = 450 - θii
In the third quadrant: bearing = 450 - θiii
In the fourth quadrant: bearing = 450 - θiv

To simplify computing, it is generally true that bearing = (450 - θ) modulo 360

In the example: bearing = (450 - θc) modulo 360 = (450 - 154.37) modulo 360 = 295.63 modulo 360 = 295.63
Similarly, for θ = 23: bearing = (450 - θ) modulo 360 = (450 - 23) modulo 360 = 427 modulo 360 = 67

If you are interested in the angle between the magnetic field vector and the horizontal plane, that can be computed from the x,y,z data. I'll leave figuring out how as an "exercise for the reader", but you're welcome to post what you determine here for review.

Good Luck,
By mayday
Hi. I follow your instruction to calculate the bearing from flux density with respect to north direction.
This is how I collect the HMC5843 data:
HMC5843 compass is lying flat on a table with z-axis perpendicular to the ground, then it rotates 360 degree. The flux readings are noted at every 5 degree intervals.

In this way, it is expected that the z-axis readings should remain consistent.
I use θ = arctan(y/x) to get the angle in degree, then calculate θc accordingly.
You mentioned that bearing = (450 - θc) modulo 360. But from what I experiment, it is more accurate to use (450 - θ) modulo 360. In fact, using (450 - θc) modulo 360 will give incorrect angle. May I know the reason?

Second question is that the above method is only applicable for cases when the compass is not tilted, ie the compass should be rotated on a horizontal plane. If want to consider tilted case, how can we manipulate x,y,z readings in order to get the tilt compensated angle?

Thank you in advance!

long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]