QMC5883 three axis geomagnetic sensor (pointer)

A, this routine to achieve the function

QMC5883 three-axis geomagnetic sensor module is used to measure the geomagnetic intensity and Azimuth Angle of XYZ three-axis, and print the output in the serial port debugging terminal of computer by print() function.

2. Basic concepts

1. Geomagnetic sensor

2. The magnetic fieldGeomagnetic field refers to the natural magnetic phenomena in the earth’s interior. The earth can be thought of as a magnetic dipole, with one pole near the geographic North Pole and the other near the geographic South Pole. The geomagnetic field strength is about 0.5-0.6 Gauss. The measurement range of geomagnetic field configured in the QMC5883 sensor is 2G or 8G, which is called 2Gauss or 8Gauss here.

Description of sensor parameter configuration

QMC5883 sensor we have set its default configuration as ODR=100Hz(maximum output of 100 sets of measurement results per second), OSR=512(the strongest anti-interference, but slightly higher power consumption). The LIB_QMC5883Config() function configures the measurement range of the sensor in the XYZ axis from -2g to 2G or -8g to 8G. No matter which of the two configurations is selected, the output values of the three directions of the sensor X, Y and Z axis are in the full range of -32768 to 32767(that is, when you get the measurement value of the sensor XYZ axis, you need to perform a simple coefficient conversion depending on whether you configure 2G or 8G full range). Higher accuracy and sensitivity when configured for 2G range, wider range but less accuracy when configured for 8G range.

Wiring diagram

Vi. Complete code

-- Configure USB to work in virtual serial mode, so that calling print() will print LIB_UsbConfig("CDC") -- Set the QMC5883 triaxial magnetic field intensity sensor to occupy SCL0 and SDA0 pins, and set the measuring range of triaxial magnetic field as2 -G ~ +2G
LIB_QMC5883Config("IIC0"."RNG_2G")-- Start the big cyclewhile(GC(1) = =true)
do-- Check whether the number of sensors is displayed every 0.2 secondsLIB_DelayMs(200)
    flag, X, Y, Z, azimuth = LIB_QMC5883GetResult()
    if flag == 1Print (X, Y, Z) then print(X, Y, Z)string.format("X:%d,Y:%d,Z:%d,Azimuth:%.0f\r\n", X, Y, Z, azimuth))
    end
end
Copy the code

Seven, the code running results

Read more at shineblink.comThe website links