LSM303 detects strong vertical magnetic field

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
tchen42
 
Posts: 3
Joined: Sun Nov 17, 2013 4:25 pm

LSM303 detects strong vertical magnetic field

Post by tchen42 »

I have a LSM303 breakout board that I'm using with an Arduino for a compass-based project.
The accelerometer seems to be reporting fine and I can calculate pitch/roll.

When held flat, the magnetometer also reports reasonable raw x and y readings. However, there is also a very large negative z-axis reading (possible sample output when held at <5 degrees pitch/roll could be 20, 5, -50).
This reading is constantly pointing down towards the Earth, so if I rotate the board 90 degrees the x or y axis will increase sharply and if I rotate the board 180 degrees, there will be a large positive z-axis reading.

I have tested the board outside in the middle of campus, in the lab where I work, and in my dorm hall. The same reading is in all of these places. In addition, I have soldered the board both directly to wires and also to a protoboard. Is it possible that my sensor is defective, or am I missing something? If it turns out that my sensor is working right, how can I tilt-compensate a compass heading given that the board detects a strong, non-constant, vertical magnetic field all across campus?

The current tilt equations I am using are:

Code: Select all

  float xTilt = 90 - (atan2(zAccel,xAccel) * 180)/ Pi;
  float yTilt = 90 - (atan2(zAccel,yAccel) * 180)/ Pi;
The current heading equations I am using are:

Code: Select all

  float xMagTC = (xMag * cos(xTilt) + zMag * sin(xTilt)) - 50 * cos(xTilt);
  float yMagTC = (xMag * sin(yTilt) * sin(xTilt) + yMag * cos(yTilt) - zMag * sin(yTilt) * cos(xTilt)) - 50 * cos(yTilt);
  float headingYTC = ((atan2(yMagTC,xMagTC) * 180) / Pi) - 14.49; //adjust for magnetic declination
  if (headingYTC < 0)
  {
    headingTC = 360 + headingYTC;
  }
where the -50 * cos(tilt) is attempting to compensate for the vertical reading.

Thanks for your help.

waltr
 
Posts: 306
Joined: Wed Jun 12, 2013 5:01 pm

Re: LSM303 detects strong vertical magnetic field

Post by waltr »

You typically need to do a calibration of the magnetometer and apply offset corrections at minimum.
To do calibrations rotate on one axis and plot or record the other two axis. Find the Min and Max values then calculate the mid value. This is the offset error that needs to be applied to use the magnetometer as a compass.

Here is a snippet of my code to do corrections on two axes (assumes that the magnetometer is flat and z doesn't change):

Code: Select all

        mag = lsm.read_mag()
        print mag
        x,y,z = mag   #unpack tuple
        x = x -55   #adjust for offsets
        y = y + 219
        heading = math.atan2(y, x) *180/3.14
        if heading < 0:
          heading = 360 + heading
        print heading
There are a few papers out on the web about magnetometer corrections. Do a search and read for more details.

Note: there is an error in the adafruit_LSM303.py file. The Magnetometer internal registers are in the order X, Z, Y. Not x, y ,z. (Check the LSM303 data sheet to see this). So I changed the file in the def read(self): to:

Code: Select all

 # Read the magnetometer
list = self.mag.readList(self.LSM303_REGISTER_MAG_OUT_X_H_M, 6)
res.append((self.mag16(list, 0),
self.mag16(list, 4),
self.mag16(list, 2),
0.0 )) # ToDo: Calculate orientation

tchen42
 
Posts: 3
Joined: Sun Nov 17, 2013 4:25 pm

Re: LSM303 detects strong vertical magnetic field

Post by tchen42 »

I don't think there's any python in the library I'm using. https://github.com/adafruit/Adafruit_LSM303DLHC

I'll try doing some calibration

Bob_CA
 
Posts: 3
Joined: Sat Nov 16, 2013 6:07 pm

Re: LSM303 detects strong vertical magnetic field

Post by Bob_CA »

The earth's magnetic field does typically have a z component. If you want some confirmation, take the board to the geology or geophysics dept on your campus and ask one of the professors there about the local geomagnetic field.

You might also try the online calculator here: http://www.ngdc.noaa.gov/geomag/magfield.shtml to find out about what to expect at your location. Be aware building framework or other ferrous metal or electrical equipment can cause local variations.

Bob

tchen42
 
Posts: 3
Joined: Sun Nov 17, 2013 4:25 pm

Re: LSM303 detects strong vertical magnetic field

Post by tchen42 »

Bob_CA wrote:The earth's magnetic field does typically have a z component. If you want some confirmation, take the board to the geology or geophysics dept on your campus and ask one of the professors there about the local geomagnetic field.

You might also try the online calculator here: http://www.ngdc.noaa.gov/geomag/magfield.shtml to find out about what to expect at your location. Be aware building framework or other ferrous metal or electrical equipment can cause local variations.

Bob
Thanks! These values seem to be in line with what I've been receiving. I was unaware that the z-component of the Earth's magnetic field was so strong (most of the compass tutorials I've read haven't said anything about it). With the right values I should be able to tilt-compensate and separate the three fields.

waltr
 
Posts: 306
Joined: Wed Jun 12, 2013 5:01 pm

Re: LSM303 detects strong vertical magnetic field

Post by waltr »

I don't think there's any python in the library I'm using.
You're right. I didn't look at your code close enough to recognize that it is C.
Also, missed the real question and answer. Bob_CA hit it however.

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

Return to “Other Products from Adafruit”