Micro Magnetometer - MMC5983MA - example code issue

Hello, I’ve been learning to work with the SparkFun Micro Magnetometer - MMC5983MA (Qwiic) but am having trouble getting what would seem to be a full range of values returned from the example sketch “Example2-I2C_Digital_compass” https://github.com/sparkfun/SparkFun_MM … ompass.ino.

Using that example sketch, I’m never able to generate heading values outside of a very specific range (about 117.0 to 153.0), no matter how I orient the magnetometer, how I rotate it, what power source I use. I have two of the same micro magnetometer sensors and both produce the same results.

I’m trying with various boards including Sparkfun RedBoard. I understand of course that proximity to magnetic / metal objects / electronics will be a factor, and also that calibration may be necessary for accurate readings. However, after multiple experiments in multiple locations, I only see heading values in that very specific limited range (117-153), no matter what direction the magnetometer is facing.

FWIW, I noticed that the values shown in the screenshot for the MMC5983 hookup guide fall precisely in that same range. https://learn.sparkfun.com/tutorials/qw … okup-guide

Being a newbie, it is likely that I’m misunderstanding something about the sensor or the example sketch. Any help or advice would be very much appreciated.

I’m having the same issue but for a different range of values (about 270-70?) so perhaps something is amiss in the code? File an issue here https://github.com/sparkfun/SparkFun_MM … is%3Aissue and an engineer will be able to take a look at it

I’m also getting very weird results. I have two devices to compare, one the micro one and the other the larger qwiic device with two connections, both using the mmc5983 and both behaving about the same. When I turn the device through 360 degrees, for about 180 degrees i get readings between 0 and about 3000 for 180 degrees, and between about 63000 and 65000 for the other 180 degrees. This happens for both X and Y axes. There is a kind of null at about the point where it jumps from about 0 to about 65000. The largest of these numbers is so close to 65535 that I am sure that some sort of number overflow problem is involved, perhaps a sign bit is being interpreted as a most significant bit?

This is just using the example 1 code and only looking at the raw results for X and Y. if I try to rotate it in the Z direction breadboard wires are going to fall out.

John

Ok, I’ve just changed the places where it gets the raw readings from unsigned int to signed int and the results look much more reasonable, being in the range 0 to 5000 or so, with a pair of nulls about 180 degrees apart. This is something like what I would expect. There is a slight bias, eg the maximum is higher in one direction than the other by about 2000. This probably corresponds to residual magnetism in the device. I think to get rid of that we are going to have do a set operation, take a set of readings, then do a reset and take another set, and average the readings out. Unless there is some other magic way of degaussing the device.

This bit:

void loop()

{

unsigned int currentX = 0;

unsigned int currentY = 0;

unsigned int currentZ = 0;

becomes:

void loop()

{

signed int currentX = 0;

signed int currentY = 0;

signed int currentZ = 0;

The compass example still doesn’t work right, even with a change to signed int there, I suspect there is some other arithmetic error but I haven’t looked at it much yet.

John

Ok, after some more messing around I have come to the conclusion that the currentX,Y, and Z variables should actually be unsigned long. This is because what the MMC5983 produces is apparently 18 bit data, and the library source when I try to make sense of it is using a UINT32, which will be truncated if we put it into a 16 bit INT. Anyway I have made that change and things are looking more reasonable, the values for the local natural field are coming out at something like the sort of value one would expect, eg in the range of about .25 Gauss.

The one remaining snag is that the polar diagram of the device seems not to be symmetrical. By which I mean that if I find the maximum X in one orientation, then turn the device through 180 degrees, I would expect to read the same value with the opposite sign. After all, the local magnetic field should only have one magnitude and direction.

This is what one would see if the device or components on the board near it had some residual magnetism.

John

I should also note that the other device also has a similar asymmetry, but not so pronounced. So presumably slightly less residual magnetism.

John