The electronic compass is an important navigation tool that can provide the heading and attitude of moving objects in real time. With the advancement of semiconductor technology and the development of mobile phone operating systems, smartphones that integrate more and more sensors have become more powerful, and many mobile phones have implemented the function of electronic compasses. Applications based on electronic compasses (such as Skymap for Android) have also become popular on various software platforms.
To realize the electronic compass function, a three-axis magnetic sensor for detecting magnetic fields and a three-axis accelerometer are required. With the maturity of micro-mechanical technology, STMicroelectronics has launched the LSM303DLH, a two-in-one sensor module that integrates a three-axis magnetometer and a three-axis accelerometer in one package, allowing users to design a low-cost, high-performance electronic compass in a short time. This article uses LSM303DLH as an example to discuss the working principle, technical parameters and implementation method of the electronic compass.
1. Background knowledge of the geomagnetic field and heading angle
As shown in Figure 1, the Earth's magnetic field points from the magnetic south pole to the magnetic north pole like a bar magnet. At the magnetic pole, the magnetic field is perpendicular to the local horizontal plane, and at the equator, the magnetic field is parallel to the local horizontal plane, so the magnetic field in the northern hemisphere is tilted toward the ground. The unit used to measure the intensity of magnetic induction is Tesla or Gauss (1 Tesla = 10000 Gauss). Depending on the geographical location, the strength of the geomagnetic field is usually 0.4-0.6 Gauss. It should be noted that the magnetic north pole and the geographical north pole do not coincide, and there is usually an angle of about 11 degrees between them.
Figure 1. Distribution of the Earth's magnetic field
The geomagnetic field is a vector. For a fixed location, this vector can be decomposed into two components parallel to the local horizontal plane and one component perpendicular to the local horizontal plane. If the electronic compass is kept parallel to the local horizontal plane, the three axes of the magnetometer in the compass correspond to these three components, as shown in Figure 2.
Figure 2 Schematic diagram of geomagnetic field vector decomposition
In fact, for the two components in the horizontal direction, their vector sum always points to the magnetic north. The heading angle (Azimuth) in the compass is the angle between the current direction and the magnetic north. Since the compass remains horizontal, only the detection data of the two horizontal axes (usually the X-axis and the Y-axis) of the magnetometer can be used to calculate the heading angle using Formula 1. When the compass rotates horizontally, the heading angle changes between 0º-360º.
2. ST integrated magnetometer and accelerometer sensor module LSM303DLH
2.1 Working principle of magnetometer
In the LSM303DLH, the magnetometer uses anisotropic magnetoresistance (Anisotropic Magneto-Resistance) material to detect the magnitude of magnetic induction intensity in space. This alloy material with a crystal structure is very sensitive to the external magnetic field, and changes in the strength of the magnetic field will cause changes in the resistance value of the AMR itself.
During the manufacturing process, a strong magnetic field is applied to the AMR to magnetize it in a certain direction, establishing a main magnetic domain. The axis perpendicular to the main magnetic domain is called the sensitive axis of the AMR, as shown in Figure 3. In order to make the measurement result change in a linear manner, the metal wires on the AMR material are arranged at a 45° angle, and the current flows through these wires, as shown in Figure 4. The main magnetic domain established on the AMR material by the initial strong magnetic field has a 45° angle with the direction of the current.
When there is an external magnetic field Ha, the direction of the main magnetic domain on the AMR will change and will no longer be the initial direction, so the angle θ between the magnetic field direction and the current will also change, as shown in Figure 5. For AMR materials, the change in angle θ will cause the change in the resistance of the AMR itself, and the relationship is linear, as shown in Figure 6.
ST uses a Wheatstone bridge to detect the change in AMR resistance, as shown in Figure 7. R1/R2/R3/R4 are AMR resistors with the same initial state, but R1/R2 and R3/R4 have opposite magnetization characteristics. When an external magnetic field is detected, the resistance of R1/R2 increases by ∆R while that of R3/R4 decreases by ∆R. In this way, when there is no external magnetic field, the output of the bridge is zero; when there is an external magnetic field, the output of the bridge is a small voltage ∆V.
Figure 7 Wheatstone bridge
When R1=R2=R3=R4=R, when the resistance changes to ∆R under the action of the external magnetic field, the bridge output ∆V is proportional to ∆R. This is the working principle of the magnetometer.
2.2 Set/Reset Circuit
Due to the influence of the external environment, the direction of the main magnetic domain on the AMR in the LSM303DLH will not remain unchanged permanently. The LSM303DLH has a built-in set/reset circuit that periodically generates current pulses through the internal metal coil to restore the initial main magnetic domain, as shown in Figure 8. It should be noted that the effect of the set pulse and the reset pulse is the same, but the direction is different.
Figure 8 LSM303DLH set / reset circuit
The set/reset circuit brings many advantages to the LSM303DLH:
1) Even if there is interference from a strong external magnetic field, the LSM303DLH can resume normal operation after the interference disappears without the need for the user to calibrate again.
2) Even if it works for a long time, it can maintain the initial magnetization direction to achieve accurate measurement, and the measurement accuracy will not be affected by chip temperature changes or increased internal noise.
3) Eliminate the bridge deviation caused by temperature drift.
2.3 Performance parameters of LSM303DLH
LSM303DLH integrates a three-axis magnetometer and a three-axis accelerometer with a digital interface. The measurement range of the magnetometer is from 1.3 Gauss to 8.1 Gauss, divided into 7 levels, and users can choose freely. And it can maintain consistent measurement results and the same sensitivity in a magnetic field environment within 20 Gauss. Its resolution can reach 8 mGauss and a 12-bit ADC is used internally to ensure accurate measurement of magnetic field strength. Compared with magnetometers using the Hall effect principle, LSM303DLH has low power consumption, high accuracy, good linearity, and does not require temperature compensation.
LSM303DLH has an automatic detection function. When the control register A is set, the self-test circuit inside the chip will generate an excitation signal of approximately the magnitude of the geomagnetic field and output it. Users can use the output data to determine whether the chip is working properly.
As a highly integrated sensor module, LSM303DLH integrates a high-performance accelerometer in addition to the magnetometer. The accelerometer also uses a 12-bit ADC, which can achieve a measurement accuracy of 1mg. The accelerometer can run in low-power mode and has a sleep/wake-up function, which can greatly reduce power consumption. At the same time, the accelerometer also integrates 6-axis direction detection and two programmable interrupt interfaces.
3. Introduction to ST electronic compass solution
A traditional electronic compass system requires at least a three-axis magnetometer to measure magnetic field data and a three-axis accelerometer to measure compass inclination. The gravity distribution and magnetic field data in three-dimensional space are transmitted to the processor through the signal processing and data acquisition part. The processor calculates the azimuth from the magnetic field data and performs tilt compensation through the gravity data. The azimuth output after processing is not affected by the spatial attitude of the electronic compass, as shown in Figure 9.
Figure 9 Schematic diagram of electronic compass structure
LSM303DLH integrates the above accelerometer, magnetometer, A/D converter and signal processing circuit, and still communicates with the processor through the I2C bus. In this way, only one chip can realize 6-axis data detection and output, which reduces the design difficulty of customers, reduces the occupied area of PCB board, and reduces the device cost.
The typical application of LSM303DLH is shown in Figure 10. It requires few peripheral devices and the connection is also simple. The magnetometer and accelerometer each have an I2C bus to communicate with the processor. If the customer's I/O interface level is 1.8V, Vdd_dig_M, Vdd_IO_A and Vdd_I2C_Bus can all be powered by 1.8V, and Vdd can be powered by 2.5V or above; if the customer's interface level is 2.6V, except for Vdd_dig_M requiring 1.8V, all others can use 2.6V. As mentioned above, LSM303DLH requires a set/reset circuit to maintain the main magnetic domain of AMR. C1 and C2 are external matching capacitors for the set/reset circuit. Since there are certain requirements for the set pulse and reset pulse, it is recommended that users do not modify the size of C1 and C2 at will.
For portable devices, the power consumption of the device is very important and directly affects its standby time. LSM303DLH can control the power supply mode of the magnetometer and accelerometer separately to put them into sleep or low power mode. And users can adjust the data update frequency of the magnetometer and accelerometer to adjust the power consumption level. When the magnetometer data update frequency is 7.5Hz and the accelerometer data update frequency is 50Hz, the typical current consumption is 0.83mA. In standby mode, the current consumption is less than 3uA.
Figure 10 LSM303DLH typical application circuit diagram
4. Ferrous magnetic field interference and calibration
Electronic compasses calculate the direction of the magnetic north pole mainly by sensing the presence of the earth's magnetic field. However, since the earth's magnetic field is generally only weak at 0.5 gauss, an ordinary mobile phone speaker will still have a magnetic field of about 4 gauss when it is 2 cm away, and a mobile phone motor will have a magnetic field of about 6 gauss when it is 2 cm away, this feature makes the measurement of the earth's magnetic field on the surface of electronic equipment easily interfered by the electronic equipment itself.
Magnetic field interference refers to the deviation of the earth's magnetic field at the location where the magnetic sensor is placed due to the presence of magnetic materials or materials that can affect the local magnetic field strength. As shown in Figure 11, in the XYZ coordinate system of the magnetic sensor, the green circle represents the projection trajectory of the earth's magnetic field vector in the XY plane during the circular rotation around the z-axis. In the absence of any external magnetic field interference, this trajectory will be a standard circle centered at O(0,0). When there is external magnetic field interference, the measured magnetic field intensity vector α will be the vector sum of the earth's magnetic field β and the interfering magnetic field γ at that point. It is expressed as:
Figure 11 XY coordinates of magnetic sensor and projection trajectory of magnetic lines of force
Generally speaking, the interference magnetic field γ can be regarded as a constant vector at this point. There are many factors that can cause magnetic field interference, such as motors and speakers placed on the circuit board, and materials containing metals such as iron, nickel and cobalt, such as shielding covers, screws, resistors, LCD back panels and shells, etc. Similarly, according to Ampere's law, a wire with current passing through it will also generate a magnetic field, as shown in Figure 12.
Figure 12 Effect of current on magnetic field
In order to calibrate the magnetic field interference from the circuit board, the main task is to calculate γ.
4.1 Plane calibration method
For the calibration of the XY axis, the device equipped with the magnetic sensor rotates in the XY plane, as shown in Figure 11, which is equivalent to rotating the earth's magnetic field vector around the normal line passing through point O(γx,γy) perpendicular to the XY plane. The red circle is the trajectory of the magnetic field vector projected in the XY plane during the rotation. This can find the position of the center of the circle as ((Xmax + Xmin)/2, (Ymax + Ymin)/2). Similarly, rotating the device in the XZ plane can obtain the trajectory circle of the earth's magnetic field in the XZ plane, which can be used to calculate the magnetic field interference vector γ(γx, γy, γz) in three-dimensional space.
4.2 Stereo 8-character calibration method
Generally speaking, when a device with sensors rotates in all directions in the air, the spatial geometric structure composed of the measured values is actually a sphere, and all sampling points fall on the surface of the sphere, as shown in Figure 13. This is similar to the circle obtained by projection in a two-dimensional plane.
Figure 13 After the Earth's magnetic field rotates in space, a sphere is obtained in the sensor space coordinates
In this case, the center O(γx, γy, γz), that is, the magnitude and direction of the fixed magnetic field interference vector, can be calculated through sufficient sample points. The formula is as follows:
The figure-8 calibration method requires the user to use the device to be calibrated to make an 8-shaped shake in the air. In principle, the normal direction of the device should point to all 8 quadrants of the space as much as possible, as shown in Figure 14.
4.2 Ten-sided calibration method
Similarly, the purpose of calibration can also be achieved through the following 10-sided calibration method.
As shown in FIG. 16 , after the 10-face calibration method, part of the trajectory of the sphere surface described above can also be sampled, thereby deducing the position of the sphere center, that is, the size and direction of the fixed magnetic field interference vector.
Figure 16 Spatial trajectory after 10-surface calibration
5. Tilt compensation and yaw angle calculation
After calibration, the electronic compass can be used normally on the horizontal plane. However, most of the time, the mobile phone is not kept horizontal, and there is usually an angle between it and the horizontal plane. This angle will affect the accuracy of the heading angle, and the acceleration sensor needs to be used for tilt compensation.
The navigation system has long defined the posture of an object in the air, as shown in Figure 17. This definition is also used in Android. Pitch (Φ) is defined as the angle between the x-axis and the horizontal plane, with the positive direction shown in the figure; Roll (θ) is defined as the angle between the y-axis and the horizontal plane, with the positive direction shown in the figure. The heading angle error caused by the pitch angle is shown in Figure 18. It can be seen that a 10-degree tilt angle in the x-axis direction can cause a maximum heading angle error of 7-8 degrees.
Figure 17 Pitch angle and roll angle definition Figure 18 Heading angle error caused by pitch angle
The tilt posture of the mobile phone in the air is shown in Figure 19. The components of gravity acceleration on the three axes are detected by the 3-axis acceleration sensor, and Pitch and Roll can be calculated by Formula 2.
Figure 19 The tilting posture of the mobile phone in the air
6. Implementation of compass on Android platform
Many popular Android phones are equipped with a compass function. To achieve this function, you only need to equip it with the two-in-one sensor module LSM303DLH provided by ST. ST provides a complete solution. The software implementation in Android can be represented by the following block diagram:
These include:
• BSP Reference
• Linux Kernel Driver (LSM303DLH_ACC + LSM303DLH_MAG)
• HAL Library (Sensors_lsm303dlh + Liblsm303DLH) for sensors.default.so
After calculation by the library, the upper-level application can easily use the yaw angle information defined by Android and provided by the Library to write the application.
Previous article:DS1629 Intelligent Temperature Sensor Based on I2C Bus
Next article:A Preliminary Study on Energy Management of Wireless Sensor Network Nodes Based on ARM7
- Popular Resources
- Popular amplifiers
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- CGD and Qorvo to jointly revolutionize motor control solutions
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Nidec Intelligent Motion is the first to launch an electric clutch ECU for two-wheeled vehicles
- Bosch and Tsinghua University renew cooperation agreement on artificial intelligence research to jointly promote the development of artificial intelligence in the industrial field
- GigaDevice unveils new MCU products, deeply unlocking industrial application scenarios with diversified products and solutions
- Advantech: Investing in Edge AI Innovation to Drive an Intelligent Future
- CGD and QORVO will revolutionize motor control solutions
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Mir MYC-YT507 development board review: performance test one coremark running score
- PCB design, which tool software is the best?
- IAR settings let the code run from reset instead of main
- The 19th year holiday schedule is here.
- What basics are needed to learn programming?
- [National Technology N32G457 Review] Implementation of Environmental Status Detection Device
- [BearPi-HM Nano, play Hongmeng "Touch and Go"] Part 2: Setting up the environment is so simple
- Failed to set default vias in Altium Designer?
- ESP8266 most complete application documentation
- LED light flickering problem, help