SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By e5kjetil
#42351
I just bought a MicroMag 3-axis SPI module at SparkFun..

I hooked it up to a dsPIC30 with DRDY pin to an external hardware interrupt , SPI runs at just below the 1MHz MicroMag limit.. Every three measurements I print the numbers to a COM terminal to observe them...

Numbers flush down the screen in my terminal window. They are formatted by the two bytes the MicroMag returns after DRDY triggers high by the formula: Mag[axis] = (int)(0x100 * Hbyte) | (int)Lbyte, showing up as integers from 0 to 65535..

This is where things become unclear for me.. The data sheet states 2's compelement, but when I convert to signed and take the sqaure of the components, that number varies much as I turn the sensor around so obviously I need to do something else..

I do not need to convert the units to gauss or tesla. All I need is the square of all components to be constant, so I can calibrate a length for that magnetic vector. I need it to be a scalar of the magnetic vector.

The data sheet is a bit confusing on the calculations (my device is 5V powered. I cannot figure out what resistor the data sheet denotes Rb at recommended 75 ohm with 5V, numbers in datasheet are calculated based on 3V... and so on). Also, I was a bit disappointed about speed performance. Albeit I only skimmed the datasheet before purchasing the sensor, but it showed excellent data on update rate and sensitivity, too bad you can't get both but have to choose..
By jasonharper
#42362
One of the customer reviews of this device suggests that it isn't reliable at 5V - after all, that's only a quarter volt away from destruction.

I would assume that the Rb resistor is described in more detail in the datasheet for the actual sensor IC (PNI-11096) used in this module. As a quick check, look at the module for either one or three resistors with the datasheet Rb value of 43 ohms.

You realize that "converting to signed" doesn't actually involve doing anything, right? It's just how you interpret the number. You're doing something wrong (inappropriate printf format, perhaps) if int variables are displaying as 0..65535, their range is actually -32768..32767.
By silic0re
#42368
I actually had a bit of trouble with my measurements going all over the place, initially. I think it ended up being a problem with the SPI module not being setup correctly, and only picking up 7 out of the 8 bits. (But of course, yours may be setup correctly!)

Here is now I convert the reading:

mag_measurement = (int)(256 * highbyte) + (int)lowbyte;

and I've posted some dsPIC code for interfacing to the MicroMag3 in this thread a little while back:

viewtopic.php?t=9252
By e5kjetil
#42376
OK, ok..

Well, I'm not going to buy myself a new sensor, so options are either to build a 3V SPI+GPIO conversion bus, or I can run my MCU at 3.3V (MicroMag can't handle 5V logic at 3.3V supply, dsPIC30 can IIRC). My other SPI DAC device are 5V but can surely cope with 3.3V I/O, and A/D channels have an instrumentation amplifier based DAC autobias system so it can be adjusted to any voltage..

It seems to me I will have to buy a bunch of 3.3V LDO regulators then and run the MCU @ 3.3V.

Thanks.
silic0re wrote:I actually had a bit of trouble with my measurements going all over the place, initially. I think it ended up being a problem with the SPI module not being setup correctly, and only picking up 7 out of the 8 bits. (But of course, yours may be setup correctly!)

Here is now I convert the reading:

mag_measurement = (int)(256 * highbyte) + (int)lowbyte;

and I've posted some dsPIC code for interfacing to the MicroMag3 in this thread a little while back:

viewtopic.php?t=9252
I had that same problem hehe I hate it when SPI is as cumbersome as with this chip. I found your code searching the net and it helped me adjust my CKE/SMP/POL bits to fix it.

The previous SPI chip I implemented was a TLV5610 8-ch 12-bit DAC, it required FS sync 16-bit SPI, and I got it working in 10 minutes, had never done frame sync SPI before. That's how it's supposed to work if chip designers starts using their brain when working.. To be honest with you, I get a really bad impression of this sensor. I wish I had bought Honeywell sensors instead. Besides everything else, the big drawback with this chip is that if you want really percise measurements, it samples way too slow for any Kalman filter.

That's the 1st impression anywayz.. Maybe some of my critizism will calm down if it starts working properly at 3V..
By Shifted
#42379
I wish I had bought Honeywell sensors instead
I have one sitting in the shop, it takes an act of god to get one, and an act of congress to get the datasheet/interface information. We were going to use it to determine static orientation of a ship (yaw, pitch, roll, and heave) in real time, but determined that it was just cheaper to buy a Kongsberg IMU for one unit, and a SeaTex IMU for the other. The 3 axis Honeywell accelerometer we have retails for about $18k, the SeaTex IMU we have retails at $25k, and the Kongsberg IMU we have retails at $45k.
By silic0re
#42393
e5kjetil wrote:OK, ok..

I had that same problem hehe I hate it when SPI is as cumbersome as with this chip. I found your code searching the net and it helped me adjust my CKE/SMP/POL bits to fix it.

The previous SPI chip I implemented was a TLV5610 8-ch 12-bit DAC, it required FS sync 16-bit SPI, and I got it working in 10 minutes, had never done frame sync SPI before. That's how it's supposed to work if chip designers starts using their brain when working.. To be honest with you, I get a really bad impression of this sensor. I wish I had bought Honeywell sensors instead. Besides everything else, the big drawback with this chip is that if you want really percise measurements, it samples way too slow for any Kalman filter.

That's the 1st impression anywayz.. Maybe some of my critizism will calm down if it starts working properly at 3V..
If you're just using the MicroMag3 to get a compass heading, you should be able to use it at a far far reduced sensitivity to the max sensitivity I used in the code. The code I posted is meant for general purpose magnetic field measurement, rather than finding the heading to magnetic north. This should increase your refresh rate, to help make the sensor more useful to your Kalman filter.

In general, aside from the issues of getting the sensor working (and PNI support not answering any e-mails), it tends to perform rather well. I have noticed some drift when it's been exposed to fields in close proximity, but I suppose the drift isn't all that large when you consider the total field strength required to facilitate that drift...

I haven't used any of the Honeywell sensors, but they'd certainly be interesting to try. I generally prefer sensors that have everything integrated, as having to construct a low-noise amplifier (which I don't seem to be terribly profient at yet) can be a bit tricky, and I usually end up with a really noisy signal. That's why the MicroMag3 is handy -- even with it's few issues, at the end when you get it working it's a relatively SPI-compliant device that's really easy to poll and likely fairly close to it's specifications. :)
By e5kjetil
#42427
silic0re wrote: If you're just using the MicroMag3 to get a compass heading, you should be able to use it at a far far reduced sensitivity to the max sensitivity I used in the code. The code I posted is meant for general purpose magnetic field measurement, rather than finding the heading to magnetic north. This should increase your refresh rate, to help make the sensor more useful to your Kalman filter.
Nice. Since my Kalman filters has grown more and more complex overtime, I found the need for an Real-time framework on the PC, and my current USB interface can manage about a 100Hz update rate at the setup I'm using. So a steady 100Hz will be all I need.

EDIT: with sensitivity=3 (/256) I get 80-ish readings per second, on sensitivity=2 (/128) I get 140-ish.. Anyone have the brains to calculate what gauss resolution I will get with /128 divisor?
silic0re wrote: In general, aside from the issues of getting the sensor working (and PNI support not answering any e-mails), it tends to perform rather well. I have noticed some drift when it's been exposed to fields in close proximity, but I suppose the drift isn't all that large when you consider the total field strength required to facilitate that drift...
I'm looking forward to try it at 3V this weekend. I ended up opting for a 3.3V conversion bus, as I really need 5V analog supply on my dsPIC and i cannot use that with 3V IO.
silic0re wrote: I haven't used any of the Honeywell sensors, but they'd certainly be interesting to try. I generally prefer sensors that have everything integrated, as having to construct a low-noise amplifier (which I don't seem to be terribly profient at yet) can be a bit tricky, and I usually end up with a really noisy signal. That's why the MicroMag3 is handy -- even with it's few issues, at the end when you get it working it's a relatively SPI-compliant device that's really easy to poll and likely fairly close to it's specifications. :)
In my analog stages I have moved from discrete opamp creations to integrated instrumentation amps with laser trimmed resistors (INA128, dual INA2128 - works down to unity gain). With a low noise multichannel DAC biasing the instrumentation amp inputs, and percision ADCVcc/2 supply biasing the outputs, the MCU can painlessly calibrate the zero reading to ADCVcc/2 (at least with x,y-axis accelerometers, and gyroscopes.. harder with magnetic sensors and Z-axis accel since they are not at zero point during calibration)

In my projects, this seems to work very well, and I actually prefer it to most digital sensors as I can use dsPIC ADC with oversampling and digital IIR filtering.

But agreed.. when simplicity counts, SPI sensors are hard to beat..
By e5kjetil
#42457
jasonharper wrote:One of the customer reviews of this device suggests that it isn't reliable at 5V - after all, that's only a quarter volt away from destruction.
Is that customer review available online on any specific url?
By e5kjetil
#42458
silic0re wrote:I've posted some dsPIC code for interfacing to the MicroMag3 in this thread a little while back:

viewtopic.php?t=9252
e5kjetil wrote:I found your code searching the net and it helped me adjust my CKE/SMP/POL bits to fix it.
Actually, I cannot make it work with that CKP/CKE/SMP combination... Strange. This is my SPIxCON:
Code: Select all
SPI1CONbits.FRMEN = 0;	// Framed SPI support
			// 1 = enabled, 0 = disabled.
SPI1CONbits.SPIFSD = 0;	// Frame Sync Pulse Direction Control on SSx pin bit
			// 1 = Frame sync pulse input (slave)
			// 0 = Frame sync pulse output (master)
SPI1CONbits.DISSDO=0;	// Disable SDOx pin bit
			// 0 = SDOx pin is controlled by the module,
			// 1 = SDOx pin is general puropse IO controlled by register
SPI1CONbits.MODE16=0;	// 1 = 16 bits mode, 0 = 8 bits mode
SPI1CONbits.SMP=1;	// SPI Data Input Sample Phase bit
			// Master mode:
			// 1 = Input data sampled at end of data output time
			// 0 = Input data sampled at middle of data output time
			// Slave mode:
			// SMP must be cleared when SPI is used in Slave mode.
SPI1CONbits.CKE=0;	// SPI Clock Edge Select bit
			// 1 = Serial output data changes on transition from
			// active clock state to Idle clock state (see bit 6, CKP)
			// 0 = Serial output data changes on transition from 
			// Idle clock state to active clock state (see bit 6, CKP)
// Note: The CKE bit is not used in the Framed SPI modes. The user should program this
// bit to ‘0’ for the Framed SPI modes (FRMEN = 1).
SPI1CONbits.SSEN=0;	// Slave Select Enable (Slave mode) bit
			// 1 = SS pin used for Slave mode
			// 0 = SS pin not used by module. GPIO.
SPI1CONbits.CKP=1;	// Clock Polarity Select bit
// 1 = Idle state for clock is a high level; active state is a low level
// 0 = Idle state for clock is a low level; active state is a high level
SPI1CONbits.MSTEN=1;	// Master Mode Enable bit: 1 = Master mode, 0 = Slave mode
SPI1CONbits.SPRE=0b000;	// Secondary Prescale (Master Mode) bits
SPI1CONbits.PPRE=0b10;	// Primary Prescale (Master Mode) bits
// 0b000 and 0b10 results in 938KHz at 30MHz Fck.
By jasonharper
#42459
e5kjetil wrote:Is that customer review available online on any specific url?
I went to the MicroMag 3-axis product page, and clicked on the Reviews tab near the bottom. I don't see any way to link directly to the review.
By silic0re
#42470
e5kjetil wrote: Actually, I cannot make it work with that CKP/CKE/SMP combination... Strange. This is my SPIxCON:
Hmmm... The code was for a dsPIC30FJ256MC710, and I remember a few peculiarities with getting the SPI config bits to work along the way. I'd have a look at the datasheet to see if the SPI config looks like it conforms to the MicroMag3 specifications -- but then, it works okay for me, so it should. Have you got it to work with other configuration settings?

When you do get it to print out some (raw) numbers, I found it really helpful to move a small magnet very slowly along each of the axes to make sure that both your 2's complement conversion (in your code) is correct, as well as to make sure that all 8-bits of each byte are being read correctly. I had some strange issues with only having even values and jumps of 256 that helped diagnose such an error (don't forget to check the zero crossing to make sure it can go between positive and negative okay too).
EDIT: with sensitivity=3 (/256) I get 80-ish readings per second, on sensitivity=2 (/128) I get 140-ish.. Anyone have the brains to calculate what gauss resolution I will get with /128 divisor?
If the max resolution (0.015 uT) is at /4096, then /128 should be about 32 times less resolution, or about a 0.48 uT resolution. (That should be about 0.0048 Gauss).

The Earth's magnetic field tends to be around 40-50 uT (I think). So you're well within your detection resolution there.
By e5kjetil
#42551
I read up on the https://www.pnicorp.com/downloadResourc ... ber+06.pdf ASIC datasheet, and identified the Rb resistors on my MicroMag PCB. There are 6 of them. I can replace those resistors with recommended 5V values (75ohm vs 43ohm).. Would that perhaps be the better choice? I got to the 'tronics shop just after closing on friday, so I didn't get to buy 3.3V LDO regs yet..

silic0re wrote:Have you got it to work with other configuration settings?
CKE seems to be don't-care in my code, it works either set or cleared.
silic0re wrote: If the max resolution (0.015 uT) is at /4096, then /128 should be about 32 times less resolution, or about a 0.48 uT resolution. (That should be about 0.0048 Gauss).

The Earth's magnetic field tends to be around 40-50 uT (I think). So you're well within your detection resolution there.
Thanx, man 8)
By e5kjetil
#42615
I am still using the module at 5V, but I replaced the Rb resistors (R1,R2,R3,R4,R6,R7) on the module with 75ohm 1% tolerance ones. The problem with squares value persists. Funny enough; when the module sits flat on the table bottom down and is rotated around the axis perpendicular to the table (Z), the square of all components remain constant. I flip it's nose down spinning it around the same axis (relative to the table), squares is still constant all the way around the circle, but the number differs from when it was laying flat bottom down...

With /128 divisor:
Flat, bottom down: sqrt(x^2+y^2+z^2)=130
Nose down: sqrt(x^2+y^2+z^2)=100
On it's side: sqrt(x^2+y^2+z^2)=60

-ish..


My 2's complement routine is simple; if the sign bit is set, I take

val = -(Measurement ^ 0xFFFF)-1 // clear sign bit and invert remaining bits, set sign, subtract one

if sign bit is not set, I use it as a positive integer simply stating that

val = Measurement

Transition from positive to negative numbers across zero seems to work fine.
By jasonharper
#42618
Your 2's complement routine isn't nearly as simple as it should be: if a 2's complement number has the sign bit set, then it's ALREADY a negative number with its proper value. Your expression "val = -(Measurement ^ 0xFFFF)-1" does not actually do anything.

It's time to start displaying the individual axis readings, so that you can figure out where the problem is coming from.
By e5kjetil
#42656
jasonharper wrote:Your 2's complement routine isn't nearly as simple as it should be: if a 2's complement number has the sign bit set, then it's ALREADY a negative number with its proper value. Your expression "val = -(Measurement ^ 0xFFFF)-1" does not actually do anything.
Ah, I see that now..
jasonharper wrote:It's time to start displaying the individual axis readings, so that you can figure out where the problem is coming from.
I am displaying the individual axis readings without figuring anything out. This sensor shouldn't need a zero point calibrated, right?