Magnetometer (LIS3MDL) temperature reading changes with orientation
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
#define SPI_READ_BURST_START OUT_X_L
uint8_t buf[8]; // xyz, temp
...
spiReadBurstLIS3MDL(SPI_READ_BURST_START, buf, sizeof(buf));
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;
...
spiReadBurstLIS3MDL(SPI_READ_BURST_START, buf, sizeof(buf));
temp = 0xFF & spiReadLIS3MDL(TEMP_OUT_L);
temp |= spiReadLIS3MDL(TEMP_OUT_H) << 8;
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.