# Magnetometer (LIS3MDL) temperature reading changes with orientation

I have a LIS3MDL outputting what seem to be reasonable magnetic field strength values (see second image). However, I am confused why the temperature reading seems to be dependent on the orientation of the magnetometer when I performed the "figure 8" motions. As far as I understand, the temperature value I am reading is used as an input to a compensation algorithm in the magnetometer, so it should be independent of the field itself.

Concerning acquisition of the value, I am reading out the 16-bit, 2's complement value and then dividing by 2^8 and offsetting to 25 degC. I believe this is justified by the application note AN4602 (http://www.st.com/resource/en/application_note/dm00136626.pdf):

Serial.print((((int16_t)((buf[8] << 8) | buf[7])) / 256.0) + 25.0); // temp


• Is the board equipped with decoupling capacitors? Is the voltage filtered and stable? What about computing cross-correlation between two signals to make sure the temperature is correlated with angle? – Marko Buršič Mar 17 '17 at 0:10
• I seriously doubt the temperature is going up and down 7 degrees celcius every second... so it's either reading it wrong or more likely you are somehow. Are you sure you are actually reading the temperature? – Trevor_G Mar 17 '17 at 1:16
• You could also leave it sitting and warm it up with a heat gun or hair drier and see if it changes as much you expect. If it doesn't then you are not reading the temperature. Though bare in mind the magnetic values will change a bit too. – Trevor_G Mar 17 '17 at 1:44
• @abc - I suggest going back to basics, cut down your code to the minimum, analyse raw data bytes from the sensor (X, Y, Z and temperature) not the computed values, and look for patterns. As Trevor mentioned, I wonder if your code is actually using the correct bytes from the sensor, to compute the "temperature" value. Looking at calculated values can hide problems visible in the underlying raw data. Also, please perform the "gently heat the stationary sensor" test suggested by Trevor and see what happens to the raw data from the sensor. Do only the expected raw bytes change? – SamGibson Mar 17 '17 at 4:19
• If that is the MATLAB plot, then use XCORR (signal processing toolbox) : temperature VS. X-axis, temperature VS. Y-axis, temperature VS. Z-axis. It will show and confirm//reject your hypothesis. I gave you an upvote for preparation. – Marko Buršič Mar 17 '17 at 9:31

I recaptured data and plotted the temperature individually against each magnetometer axis. From the plots below, its apparent that the temperature data was actually the x-axis data.

I believe this is due to the SPI address auto-increment not working as expected. It seems to 'wrap around' only within the mag x/y/z registers, and does not go to the adjacent temperature registers. My original code was:

#define OUT_X_L 0x28
#define OUT_X_H 0x29
#define OUT_Y_L 0x2A
#define OUT_Y_H 0x2B
#define OUT_Z_L 0x2C
#define OUT_Z_H 0x2D
#define TEMP_OUT_L 0x2E
#define TEMP_OUT_H 0x2F

uint8_t buf[8]; // xyz, temp

...

Serial.print(micros());
Serial.print("\t");
Serial.print((int16_t)((buf[1] << 8) | buf[0])); // x
Serial.print("\t");
Serial.print((int16_t)((buf[3] << 8) | buf[2])); // y
Serial.print("\t");
Serial.print((int16_t)((buf[5] << 8) | buf[4])); // z
Serial.print("\t");
Serial.print((((int16_t)((buf[7] << 8) | buf[6])) / 256.0) + 25.0); // temp
Serial.print("\n");


But after changing to the following, the temperature output is as expected:

...

uint8_t buf[6]; // xyz
int16_t temp;

...

Serial.print(micros());
Serial.print("\t");
Serial.print((int16_t)((buf[1] << 8) | buf[0])); // x
Serial.print("\t");
Serial.print((int16_t)((buf[3] << 8) | buf[2])); // y
Serial.print("\t");
Serial.print((int16_t)((buf[5] << 8) | buf[4])); // z
Serial.print("\t");
Serial.print((temp / 256.0) + 25.0); // temp
Serial.print("\n");


Thanks for all the input. It was a silly issue with serial comms after all, nothing to do with the sensor.

• :) Glad you found it. As I suspected. When the data seems too weird to be true.. it usually is. – Trevor_G Mar 17 '17 at 20:59
• Happy end story.:) – Marko Buršič Mar 18 '17 at 0:06

You and your face are a heat source. Approximately 1/4 watt/cm^2. Your hands are cooler, but you use your hands to hold the PCB.

Simply by being near, by closely examining the PCB, you are injecting heat and causing temperature gradients.

PCB foil, the default 1ounce/foot^2 CU foil, has thermal resistance of 70 degree Centigrade per watt.

Is that a problem?

• Again, I want to reiterate that the only thing appearing to cause this is a change in orientation. My grip on the board and distance from heat sources is constant. – abc Mar 17 '17 at 3:40