Monday, March 12, 2012

Using the MicroMag3 with Arduino

After working with the accelerometer, I decided to experiment next with a magnetometer. A magnetometer measures magnetic field strength along an axis (see wikipedia page on magnetometer). High end systems can measure bends in laser light sent along a fiber optic cable. Most MEMS devices use electrical charge sent along a coil to measure both strength and direction of a magnetic field. Mutliple coils are used to measure change in fields along specific axis. A very succinct description is provided at Sensor workshop notes:

A magnetoiductive circuit consists of a coil around a ferromagnetic core that is incorporated into a circuit that forms a relaxation oscillator. Charge gradually builds up in the circuit, is rapidly discharged, and then starts to gradually build up again, and so on. The frequency of this oscillation varies with the strength of the magnetic field perpendicular to the coil. In the micromag3, the oscillation that is produced is a square wave, which can be easily read as a digital signal. The Micromag3 calculates magnetic field strength by comparing two measurements from the same circuit. First one end of the circuit is grounded, and the other oscillates. Then the other end is grounded, and the first oscillates. Subtracting one result from the other provides temperature stabilization and the direction of the magnetic field. 
After some searching I decided to use the MicroMag3. This was based on cost and amount of information (i.e., code) out there on the web. Here are several sites I found very useful in coding up a library to read the sensor data and understand the measurements coming back:

  1. http://itp.nyu.edu/physcomp/sensors/Reports/ThreeAxisMag2
  2. http://www.sparkfun.com/datasheets/Sensors/MicroMag3%20Data%20Sheet.pdf
  3. http://wiring.org.co/learning/basics/magneticfield.html
  4. http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1263858213
  5. http://cwwang.com/2008/02/20/micromag-3-axis/
  6. https://github.com/stevemarple/MicroMag
The last link was particularly useful although I am not sure about the SS pin used as the SPI library (see below) seems to use a different default pin. Here are the parts that I used in this project (along with some wires):
  1. http://www.sparkfun.com/products/244
  2. http://www.sparkfun.com/products/11021
  3. http://www.sparkfun.com/products/7914
To start out, I had to build the prototype board. My experience with the accelerometer tests (previous blog) was that I needed a single mount point for both the sensors and the Arduino. This is needed so that I can move everything around easily and make valid measurements. This involved a little soldering to mount the connections. SparkFun had an easy to follow set of assembly instructions. I only setup the headers for now (see figures below) as I can add the lights and reset button later. Once that was done, I wired up the MicroMag3. This took a little time to figure out as the slave select pin has a default setting if you use the SPI library and several different code bases set it to something else. Once I decided on using SPI, everything got a lot easier.


The pin layout is also shown below. I decided to use the SPI library to take care of the bit shifting for serial reading off the chip. Out of the box, the SPI library expects the following pin configuration:

const static uint8_t SS   = 10;
const static uint8_t MOSI = 11;
const static uint8_t MISO = 12;
const static uint8_t SCK  = 13;
I copied the library into my workspace as I am sure that later on, this pin configuration will need to be changed. But for now, the easiest thing to do was wire it up using the defaults.

The code is located at:
https://github.com/mark-r-stevens/Ardadv/tree/master/device/sensors/magnetometer
I also put the data sheet there for easy reference. I updated the UI I was using before to have a simple compass so that I could check the heading coming back from the magnetometer. A simple check against my iPhone and the values look similar. There are a few questions I have about the default orientation of the magnetometer (seems 90 degrees from what I expect). The latency also seems high, but there are a few settings in the code to read faster (not sure what effects that has on accuracy). Also, seems the measurements are a little noisy (not as bad as the accelerometer) which will be cleaned up when I add some filtering and state estimation. Finally, I will probably also need to have some calibration procedures to define what default return levels should be forced to zero.

Here is a movie of the result. Somewhat hard to see given the compression, I should probably figure out a better way to post these. That can probably wait until the movies get a little more interested (like with robots).
video

Up next is starting to build the drive motor. I went with a 4wd robot from dfrobot. This meant finding a drive motor that can support 4 DC motors, which lead me to the AdaFruit board. More to follow....


1 comment:

  1. Hello, do you have any ideas what are the units of the output values and how to maximize the resolution of the micromag3?

    ReplyDelete