# Rotating Magnets

A project log for 3D Magnetic Field Scanner

Capture interactive models of magnetic fields with your 3D printer

Ted Yapo 01/13/2018 at 18:042 Comments

Someone on GitHub asked about using the MLX90393 to determine the angle of a permanent magnet.  I had to admit I had never tried to use this part for its presumably indented purpose.  So, after breakfast, I lashed some junk together to try it out.

It's a makeshift goniometer made from a milling machine rotary table.  I had at one point driven the table with a stepper motor, which is still connected, but today I just moved it manually.  A small neodymium magnet is held in the clamp jammed in the table slots.  A MLX90393 eval board is held fixed about 60mm away from the magnet.

I recorded data for angles from 0-400 degrees, and made the following plot:

The x- and y-components of the field show a familiar sine/cosine relationship, while there is just a little variation in z.  This is because the quick setup is not very well aligned.  The resulting angle (determined from 180*atan2(y, x)/pi seems OK,  and is probably usable for many purposes as is.

The true angle and detected angle have an offset of around 160 degrees because I didn't try to align "0" on the rotary table with the sensor axes at all.

One of the problems is that at this distance from the magnet, the field is close to the same magnitude as the geomagnetic field.  Here's a snippet of the actual data (angle, Bx, By, Bz, T):

```0 -165.30 -57.15 132.37 19.96
10 -156.60 -80.70 133.83 20.09
20 -143.85 -103.35 134.79 20.09
30 -127.35 -122.85 136.97 20.13
40 -107.55 -139.20 138.42 20.09
50 -84.75 -151.05 139.63 19.87
60 -61.35 -159.75 141.33 19.91
70 -36.30 -163.95 143.26 20.18
80 -11.25 -162.90 144.47 20.11
90 14.55 -157.80 145.68 20.20
100 37.95 -148.05 146.65 20.33
110 59.70 -133.50 147.14 19.98
120 79.05 -116.55 147.62 20.22
```

For more accuracy, you could calibrate the system, but this would change if you re-oriented the setup in space, since the geomagnetic field would change relative to the sensor.  Instead, it might be easier to move the magnet closer, increasing the field and diminishing the influence of the big magnet we live on.

There's probably something fancy you can do to incorporate the z-axis data, too, but I can't think of it at the moment.  I bet there are some compass calibration algorithms that could clean this up a bit.

## Discussions

Jesse Schoch wrote 01/14/2018 at 08:10 point

quick video of what I came up here: https://youtu.be/lGYVTiu5m34

Are you sure? yes | no

Ted Yapo wrote 01/16/2018 at 02:52 point

Nice demo!

Now I want to make an RGB LED compass.  The r, g, and b channels would just be driven directly from the x-, y-, and z- field strengths.  When you point it north (including the magnetic dip at your location), it would turn red, for instance.  I guess you need to do something with negative values, but it could still be cool.

Are you sure? yes | no