SparkFun IoT Brushless Motor Driver - Interrupt example file

Hi I just got the SparkFun IoT Brushless Motor Driver board and going through the example files. On the second one that goes through interrupts, getting confusing outputs in the serial, the threshold is set to 5 (the comment says that is mT, but not sure it is) however when running I only get all negative values under around -3mT.

Anything above that doesn’t trigger the interrupt. is that correct, seems strange, I tried flipping the mag direction with sensor.setMagDir(true); and then I get all the values above -3mT instead.

Any help in understanding would be great, thanks

Which example are you using? There are some here and also on the simpleFOC github

hi, it was actually the example files for the TMAG5273 Arduino library - Example2_Interrupts - SparkFun TMAG5273 Arduino Library

thanks

Ok, it took me a bit to figure out what what going on here. The good news is it’s mostly just a case of misleading nomenclature and needing to use the ambient offset, I believe

First: setXThreshold(5) is not 5 mT — it’s a raw register code

The // mT comment in the example is misleading. Per the datasheet and the library’s own doc comments, X_THR_CONFIG is an 8-bit, 2’s complement X axis threshold code for limit check, with a range of +/-128, and the threshold value in mT is calculated as (40(1+X_Y_RANGE)/128)*X_THR_CONFIG. Github descriptons With the default X_Y_RANGE (0 → ±40 mT range) and a code of 5, that works out to: (40 × 1 / 128) × 5 ≈ 1.56 mT

So setXThreshold(5) is actually setting a threshold around 1.5 mT, not 5 mT — the example comment just states the units it’s trying to represent (mT) without doing the conversion for you. If you want a specific real-world mT threshold, you need to invert that formula yourself: code = round(desired_mT * 128 / (40*(1+range))).

Second: setMagDir() doesn’t set magnetic pole direction — it flips which side of the threshold triggers

That function writes the bit documented as: the direction of threshold check, ignored when THR_HYST > 001b — 0x0 sets interrupt for field above the threshold, 0x1 sets interrupt for field below the threshold (SENSOR_CONFIG_2, bit 5). Link to reference. So:

  • setMagDir(false) (default) → interrupt fires when the field goes below the threshold → that’s exactly why you only saw negative values under ~-3 mT triggering it.
  • setMagDir(true) → interrupt fires when the field goes above the threshold → and now everything above -3 mT triggers, which is also exactly what you saw.

It has nothing to do with “which pole of the magnet”, it’s purely above-vs-below comparison direction. The name setMagDir makes it sound like polarity/orientation, but functionally it’s a threshold-comparison-direction toggle situation

If you want the interrupt to behave the way most people expect (“fire when field magnitude exceeds X mT in either direction from a calibrated zero”), you’ll want to first zero out the ambient offset (there’s an offset register/config for this), then compute your threshold code from the mT formula above, and treat setMagDir purely as “above vs. below,” picking whichever matches your use case.

thanks Russell, I got only as far as figuring out that the threshold value was not mT like the comment, but then got a bit lost, so this is super helpful I will have a go, the findings about the ambient offset correction is helpful. thanks again. K