-1

I do not know that this is a good place for asking question but I have a 3 axis magnetometer IIS2MDC. I used this with Arduino. I downloaded the library IIS2MDC.h in Arduino IDE. I used example code of IIS2MDC.h library for reading magnetometer data. I have 3 axis raw data.

My question is that how to convert magnetometer data into degrees? Is there any reference or solution for this?

2
  • Welcome to SE/Arduino! The device and its library should have documentation, and I would expect example code to show such conversions. Please edit your question to provide links to that, and add why it does not help you. -- OT: A library consists in C++ projects commonly of two parts, the header file and the library file. The former is "just" for convenience, the important file is the second one. Apparently the IDE does everything automagically correct, but please be aware of such details. Commented Dec 11, 2023 at 13:41
  • @EDExpert, do edit your question to make it better. Just so you know: In the many stackexchange web sites, it is better to edit your question and for others to edit their answer to resolve the unexpected. In other words: Ask your question as if in front of an audience any of whom may have the same problem that needs resolving. Commented Dec 11, 2023 at 14:30

1 Answer 1

0

It is likely the magnetometer needs to be calibrated before it can be used. Before calibration is applied to the magnetometer's output values, comparing any 2 of the 3 orthogonal sensors will be difficult.

Search and learn the terms hard iron and soft iron distortion / calibration with respect to magnetometers. Hard iron distortion is an offset error. Find the maximum (point a particular magnetometer toward magnetic north) and the minimum (point the same magnetometer away from magnetic north) values of one of the magnetometers. Calculate an offset value such that when applied the maximum and minimum magnitudes are the same. Soft iron distortion is a magnitude error. Find the maximum and minimum values for all 3 sensors. From this find the total range of all 3 sensors. Arbitrarily pick one of the sensors ranges and calculate magnitude adjustment values for the remaining sensor ranges such that after applying them all 3 sensors have the same range.

At this point let us narrow the scope of the electronic compass to simplify the project. Let us constrain the compass to only be used on a table which is parallel to the surface of the earth. At which point we only need to use 2 of the sensors (the two that lie in the plane of the table).

To calculate the angle the magnetometer is pointing with respect to magnetic north we take the arctangent of the ratio of the 2 magnetic sensor which lie in the plane of the table. However, this math function only gives us angles for half the desired angles. Fortunately, the C programming math library contains a special function atan2() which can return a full circle of angles (from here):

Synopsis. The atan2() function divides the first argument by the second and returns the arc tangent of the result, or arctan( y / x ) . The return value is given in radians, and is in the range -π ≤ atan2( y , x ) ≤ π.

2
  • I try hard iron and soft iron distortion / calibration but i am unable to get correct values. I convert heading values of magnetometer into degrees using atan2(y/x) * 180/ PI equation but i got few random values i did not get 0 to 360 degree values. So how can i work my IIS2MDC chip as compass IC and get accurate degrees according to direction? Commented Dec 14, 2023 at 8:54
  • Take it one step at a time. Before finding the arc tan, make sure the way your sensor is held that you are using the 2 sensors that are always parallel (not tilted) with respect to the earth's surface. Then, after adding or subtracting the calibrated offset and multiplying with the calibrated magnitude that the 2 values raise when that sensor is pointed to north and lowers when pointed away by the same or nearly the same amount. That the negative of the maximum for a particular sensor is the same or nearly the same as the minimum. After that works, then take the arc tan. Commented Dec 15, 2023 at 2:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.