Kenneth M. Cruikshank
Getting Started with HMC5488L digital compass

Kenneth M. Cruikshank
Kinect Projects

This page documents some projects using the Arduino prototype board.

Introduction

If you found this page from a web search, you may first want to review the Arduino Introduction page and the Code Style page.

Here is the working code to read a Honeywell HMC5883L 3-Axis Digital Compass

Magnetic Sensor Chip

As with any sensor, the Datasheet is the ultimate reference.  I have had some students use code for this chip that they found online, however, the code is often incorrect. The output behaves as you would expect, but many of the actual values are incorrect (see NOAA's Magnetic Field Calculator, http://www.ngdc.noaa.gov/geomag-web/#igrfwmm, to get an estimate of your local magnetic field component values).

Caution: What is a different about this chip is that rather than storing the component values is registers, the two's-complement of the values are stored. This requires that the two's-complement first be translated back to a "regular" binary integer before being used. What can make it hard to spot on some of the magnetic field components from this sensor is that the two's complment results in the same value (but not for all components).

Magnetic Field Units

Magnetic field strength is often reported in Gauss (G or Ga) or in nanoTesla (nT). Gauss is the unit in the cgs (centimeter-gram-second) system, and Tesla (T) is the SI unit (in the mks or metre-kilogram-second system). The Earth’s magnetic field has a large range depending on latitude, but is in the order of 0.5 Ga or 5 x 10-5 T at mid-latitudes in western North America.  When working with the Earth’s magnetic field, we often use units of milli-Gauss (mGa), so the field is about 500 milliGauss (mGa), 500,000 (mGa) microGauss, or 50,000 (nT) nano-Tesla,. Often a nT is called a “gamma”, so rather than seeing nT (or mGa) you may see a unit of g.

To get an idea of the magnetic field strength (and the components of the field), use the NOAA Magnetic Field Calculator (http://www.ngdc.noaa.gov/geomag-web/#igrfwmm).

The sensor calculate the magnetic field in Gauss, but the code I have here displays the results in nanoTessla. The sensor has a range of +/- 6 Ga, and an accuracy of about 0.xx Ga, or about 200 nT.

General Outline of Code

  1. Initialize chip
  2. Read X, Y, and Z componets (in Two's-complement form)
  3. Convert Regester values to Raw Values
  4. Apply calibration
  5. Convert to Gauss
  6. Convert to Nanotessla
  7. Calculate total field strength

 

Geology Department
http://www.pdx.edu/geology

Copyright © 1994-2015 · K.M. Cruikshank ·
http://geomechanics.research.pdx.edu