Skip to content

Compass

Magnetometer

NXP/Freescale MAG3110 three-axis magnetometer sensor

Overview

The MAG3110 is a small, low-power digital 3D magnetic sensor with a wide dynamic range to allow operation in PCBs with high extraneous magnetic fields. The MAG3110:

  • Measures the components of the local magnetic field, the sum of the geomagnetic field and the magnetic field created by components on the circuit board

  • Can be used in conjunction with a 3-axis accelerometer so that orientation-independent accurate compass heading information may be achieved

  • Features a standard I²C serial interface and is capable of measuring local magnetic fields up to 10 Gauss with output data rates up to 80 Hz

source: manifacturer website

Read the Datasheet

How it works

The sensor provides an heading expressed in degrees in respect to the magnetic north pole. It can also be used with a magnet, in this case we can measure the intensity of the magnetic field.

Calibration

On the first use of the compass, the calibration procedure will automatically start. The user must draw a circle with the device until it is fully calibrated.

An enclosure made from metal, or using in proximity of metal objects, might affect the accuracy of the reading and calibration. During calibration, compass heading returns -1003.

In blocks the following can be invoked to start the calibration:

calibrate compass

Finding the north

CompassHeading returns a number between 0 and 359. We can consider “NORTH” any number between 0 and 45.

If the compass is not ready, the compass heading will be -1003. In this case you need to call the compass calibration as described above.

compass heading (°)

Read heading

from microbit import *
while True:
    display.scroll('Heading %s' % compass.heading())

Show a compass

from microbit import *

compass.calibrate()

while True:
    needle = ((15 - compass.heading()) // 30) % 12
    display.show(Image.ALL_CLOCKS[needle])