LSM303AGR can measure magnetic fields up to ±50 gauss. The Earth's magnetic field strength is around 0.6 gauss.
In this way, the LSM303AGR can theoretically be used to measure the magnetic field of a magnet, such as using it to make a game joystick.
Because it will be affected by the external magnetic field, the influence of the earth's magnetic field is usually around 1%.
External influences should be solved by adding another sensor for compensation
In the actual test, I used csharp to write a simple host computer program to display the X, Y, and Z curves.
The problem I encountered is that when the magnetic field of the magnet reaches a certain level, the sign bit of the data will change when the output data is greater than 32767 or less than -32768.
For example, from -32760 to +32750, in order to prevent the MCU from sending data and the host computer from decoding data errors, I changed the Z axis to a continuously accumulated data in the program.
During the test, the data of the Z axis is normal, but the X and Y axes still have problems like the one in the figure below, where the data jumps from the top to the bottom, or from the bottom to the top.
Below is the original data and the chart generated by EXCEL
EXCEL File
测试数据.xlsx
(74.07 KB, downloads: 0)
The test tool uses ST's BlueCoin development board
The code uses DataLog under STSW-BCNKT01_V2.1.0 and makes a simple modification to the format of data output to USB.