cancel
Showing results for 
Search instead for 
Did you mean: 

Magnetometer (model LIS2MDL) getting strange readings for not so high magnetic fields. What is going on?

ginestopo
Associate II

0693W00000UFWlOQAX.jpgAs you can see in the picture, the higher part of the sinusoid signal is getting wrecked in the highest part, but it appears in the lower part (so the information is still there).

This happens when the magnetic field is strong enough (We are putting a coil next to it generating the magnetic field)

The problem is that I (and any of the people I work with) do not think this is a big enough magnetic field for it to overflow, so that is why we are wondering if there is another problem we did not think about.

As the manufacturer suggests, the data aquired by the magnetometer should be processed as following:

0693W00000UFWlTQAX.png0693W00000UFWlsQAH.png 

and what I am doing to process it in the code is exactly that:

uint8_t y_low = 0;
uint8_t y_high = 0;
 
//y_L
HAL_I2C_Mem_Read(&hi2c1,slave_address << 1, OUTY_L_REG, I2C_MEMADD_SIZE_8BIT, &y_low, size, HAL_MAX_DELAY);
 
//y_H
HAL_I2C_Mem_Read(&hi2c1,slave_address << 1, OUTY_H_REG, I2C_MEMADD_SIZE_8BIT, &y_high, size, HAL_MAX_DELAY);
 
		
y_value_plot = (int16_t)join_into_signed_16b(y_low, y_high, true);   //<----- CAMBIAR A INT16_T

where the function join_into_signed_16b does the following:

uint16_t join_into_signed_16b(uint8_t low_part, uint8_t high_part, bool convert_to_mg){
 
	uint16_t signed_16b = (uint16_t)((high_part<<8)|low_part);
 
//	if(convert_to_mg){
//		signed_16b = (int16_t)((float)signed_16b * 1.5);
//	}
 
	return signed_16b;
}

The resultant wave is "clipping" because the upper part of the Y register is not changing and remains all 8bit at 1.

0693W00000UFWmWQAX.png 

Also as you can see, the concatenation is performed as expected.

Thank you very much in advance.

Ginés.

7 REPLIES 7
JSchn.5
Associate III

Looks like a mid-high bit is getting corrupted from a noise or timing issue caused by the field itself I should think.

Suggest you scope SDA and SCL preferably at a bad bit. This is where an MSO scores.

Also check whether the reads were successful.

Finally I note you're applying a varying magnetic field and that's going to induce all sorts electromagnetics and all that. I would plat or screen the sensor leads.

AScha.3
Chief II

looks like signal is over max-limit, and >> uint16_t signed_16b is : making unsigned int signed or what ??

maybe, there is a gain setting on chip ?

If you feel a post has answered your question, please click "Accept as Solution".

Thanks for replying. I do not think it is a matter of corruption, as I think HAL_I2C_Mem_Read ensures the communication was successful.

Moreover, it seems that the high 8 bits for the x and z axis change in a very slow manner. I think it is a matter of gain as @AScha.3​ mentions. Nevertheless I cannot find a register/function to change such behavior in this sensor.

Have a nice day.

Thank for replying. The gain setting makes a lot of sense, nevertheless I was not able to change such configuration in the LIS2MDL. The upper 8 bits data register won't change for some reason.

Have a nice day.

AScha.3
Chief II

Ooo : maybe you invert MSB with your confused INT - UINT castings...

so keep to signed, when value is signed !

->

uint16_t join_into_signed_16b...() -> int16_t join_into_signed_16b...()

+

  1. -> if(convert_to_mg){
  2. // signed_16b = (int16_t)((float)signed_16b * 1.5);

->

  1. if(convert_to_mg){
  2. // signed_16b = (float)signed_16b * 1.5;

if you invert MSB by (wrong ) conversion , signal looks like your signal!

If you feel a post has answered your question, please click "Accept as Solution".

Thanks a lot! I will give it a try later and let you know. Have a nice day.

Sadly the problem persists. The upper register wong change for some reason.