MinIMU-9 v2 gives same mag reading

I’m having a problem with a MinIMU-9 v2 sometimes giving the same heading (+or-1) regardless of the direction it’s heading.
the heading it gives appears to be the direction it’s at when initialized, but doesn’t change when it’s rotated, excepting for a +or-1 jitter.
my code is very similar to the heading example given in the LSM303 library.

LSM303 compass;

void setup()   
    compass.m_min.x = -381; compass.m_min.y = -562; compass.m_min.z = -652;
    compass.m_max.x = +597; compass.m_max.y = +597; compass.m_max.z = 544;

short get_current_heading()
    return compass.heading((LSM303::vector){0,-1,0});

elsewhere I get the result of get_current_heading() then Serial.Print() the result so I can see what’s going on.
The problem is intermittent, IE I can turn the thing off then on again and it will sometimes work, sometimes not, which is frustrating since I’ll make a change, think it’s fixed, then it will come back. I’m not sure what might be causing this and not sure how to troubleshoot it.
any ideas?
I’m doing this on a Teensy3

EDIT: Ohh I’ve just noticed something - if I re-upload the code to the T3 it will work, if I restart it i’ll get the stuck heading that’s… odd.

I suspect there’s something that is sometimes being reset or initialized by the Teensy and sometimes not. Does it make a difference if you power on or reset the IMU by itself, the Teensy by itself, or both of them together?

If your full program isn’t much longer, could you post it so we can see what it does?

- Kevin

messed around a bit more, if I reset the teensy it will work, but if I just power everything on either by battery or USB I get this problem, so my previous statement was somewhat incorrect.

the IMU is being powered through the teensy 3.3v

tested with just the heading example and get the same result, but I’ve attached the full code I’m using anyway.

if I repower the IMU in the not working state nothing changes, I still get an incorrect serial output.
if I repower the IMU in the working state I stop getting serial output.
(that was with just the heading example)
T3bot.ino (10.6 KB)

on a hunch, I just added a 1second delay to the beginning of the void setup on the heading example which seems to have fixed the problem.
is it trying to initialise before the IMU has fully powered on?

I’m glad you were able to fix the problem. It’s possible the IMU or the Teensy weren’t fully powered on when your program started trying to communicate with the IMU before. Are you powering it through the VIN pin? If so, you might want to try connecting 3.3 V to the VDD pin instead (bypassing the regulator) to see if that helps.

- Kevin

I’m powering the IMU on the VDD pin.
I could try powering it from the power source instead of through the teensy 3.3 out, and see if that makes a difference, though I’d have to go through the IMU VIN that way.

also, while I’m thinking of it, you may want to update the LSM303 and LG3 arduino libraries, I initially had a bit of trouble and was getting some odd readings using these libraries with the T3, here’s an example of the serial output I was getting:
A X: 4028 Y: 4 Z: 3056 M X: 165 Y: 65329 Z: 64977
A X: 4020 Y: 4 Z: 3048 M X: 165 Y: 65315 Z: 64957
The problem ended up being the right shift operation in these libraries. Paul from PJRC(teensy) explains what’s going on way better than I ever could here: … sts/331757
scroll down a bit to “Issues Running 8 Bit AVR Code on 32 Bit ARM”

Pretty easy to fix once you know what’s going on, but had me scratching my head for a bit. I expect this may crop up a bit more with the Due out now.

I did post an issue on github in these libraries but not sure anyone is keeping an eye on them.

I saw the issues posted to GitHub; thanks for proposing those fixes. We’ll try to get them into the code soon.

- Kevin