4. Chapter Magnetometer

In this chapter, we will learn the micro:bit built-in magnetometer chip.

4.1. Project Display Magnetometer Data

This project will print the data obtained from the magnetometer chip on the serial console.

4.1.1. Component list

Microbit x1

USB cable x1

Chapter03_00

Chapter03_03

4.1.2. Circuit

Connect micro:bit and PC via micro USB cable.

Hardware connection

../../../_images/Chapter10_00.png

4.1.3. Block code

Open MakeCode first. Import the .hex file. The path is as below:

( How to import project )

File type

Path

File name

HEX file

../Projects/BlockCode/04.1_DisplayMagnetometerData

DisplayMagnetometerData.hex

After importing successfully, the code is shown as below:

../../../_images/Chapter11_00.png

Check the connection of the circuit and verify it correct, and then download the code into the micro:bit. After completing downloading, the magnetometer needs to be calibrated (calibration must be performed using the magnetometer program). Calibrating the magnetometer will cause the program to pause until the calibration is completed. Start the calibration process, a prompt will scroll on the LED matrix, which indicates that you need to rotate the micro:bit until all LEDs on the LED screen are illuminated, and then a smile is displayed which means the calibration is completed, as shown below:

../../../_images/Chapter11_01.png

Then open the serial console ( Open the Serial Port), place the micro:bit horizontally on the desktop, and rotate the micro:bit (clockwise or counterclockwise) to see the angular offset read from the magnetometer chip. As shown below:

../../../_images/Chapter11_02.png

3 indicates the number of times of consecutive readings of the same value.

The angular offset is the angle between the directions of the micro:bit and the geographic North Pole, as shown in the following figure.

../../../_images/Chapter11_03.png

The angular offset read from the magnetometer chip is stored in the variable azimuth.

../../../_images/Chapter11_04.png

Then the value of the variable azimuth is printed on the serial port interface.

../../../_images/Chapter11_05.png

4.1.3.1. Reference

Block

Function

Chapter11_06

Set the number of data pins and LEDs, as well as the type of LED.

Chapter11_07

Set LED color

Chapter11_08

Turn on LED

4.1.4. Python code

Open the .py file with Mu. Code, the path is as below:

File type

Path

File name

Python file

../Projects/PythonCode/04.1_DisplayMagnetometerData

DisplayMagnetometerData.py

After loading successfully, the code is shown as below:

../../../_images/Chapter11_09.png

After checking the connection of the circuit and verify it correct, download the code into micro:bit.

After downloading the program, click REPL, as shown below.

../../../_images/Chapter11_10.png

Then press the reset button of the Micro:bit, you need to calibrate the magnetometer (the calibration must be performed when downloading using the magnetometer program).

Calibrating the magnetometer will cause the program to pause until the calibration is complete. Start the calibration process, a prompt will scroll on the LED matrix, which indicates that you need to rotate the micro:bit until all LEDs on the LED screen are illuminated, and then a smile is displayed which means the calibration is completed, as shown below:

../../../_images/Chapter11_11.png

Place the micro:bit horizontally on the desktop, and rotate the micro:bit (clockwise or counterclockwise) to see the angular offset read from the magnetometer chip. As shown below:

../../../_images/Chapter11_12.png

The angular offset is the angle between the direction of the micro:bit and the geographic north pole, as shown in the following figure.

../../../_images/Chapter11_13.png

The following is the program code:

1from microbit import *
2compass.calibrate()
3while True:
4    azimuth = compass.heading()
5    uart.write(str(azimuth)+"\r\n")
6    sleep(1000)

Magnetometer calibration.

1compass.calibrate()

The angular offset read from the magnetometer chip is stored in the variable azimuth and then printed out every 1s through a serial port.

1azimuth = compass.heading()
2uart.write(str(azimuth)+"\r\n")
3sleep(1000)

4.1.4.1. Reference

compass.calibrate()

Starts the calibration process. An instructive message will scroll on the LED matrix, which indicates that you need to rotate the micro:bit until all LEDs are illuminated.

compass.heading()

Gives the compass heading, calculated from the above readings, as an integer in the range from 0 to 360, representing the angle in degrees, clockwise, with north as 0.

4.2. Project Electronic Compass

In this project, we will use micro:bit to make an electronic compass, displaying an arrow on the micro:bit, and the arrow always points to the geographic north pole.

4.2.1. Component list

Microbit x1

USB cable x1

Chapter03_00

Chapter03_03

4.2.2. Circuit

Connect micro:bit and PC via micro USB cable.

Hardware connection

../../../_images/Chapter11_14.png

4.2.3. Block code

Open MakeCode first. Import the .hex file. The path is as below:

( How to import project )

File type

Path

File name

HEX file

../Projects/BlockCode/04.2_ElectronicCompass

ElectronicCompass.hex

After importing successfully, the code is shown as below:

../../../_images/Chapter11_15.png

Check the connection of the circuit and verify it correct, and then download the code into the micro:bit. Calibrate the electronic compass. After the calibration is successful, place the micro:bit horizontally and turn the micro:bit to see that the arrow points to the geography Arctic.

The arrow will point to eight directions: northwest, west, southwest, south, southeast, east, northeast, north, each direction is 45 degrees apart. Assuming that the direction of the micro:bit is rotated 45 degrees from the north to the northeast of the geography, the arrow shown should be reversed, that is, it rotates

-45 degrees, pointing to the northwest of the micro:bit, which is the geographic north pole. Therefore, we can adjust the direction of the arrow according to its angular offset from the geographic North Pole.

When the variable azimuth is less than 22.5 or greater than 337.5, the arrow points to the due north of the micro:bit.

When the variable azimuth is greater than 22.5 or less than 67.5, the arrow points to the northwest of the micro:bit.

And so on in the same fashion, in every 45 degrees, the arrow points to a particular direction indicating the geographic north, as shown in the following illustration:

../../../_images/Chapter11_16.png

The angular offset read from the magnetometer chip is stored in the variable azimuth

../../../_images/Chapter11_17.png

Determine the value of the variable azimuth to change the direction of the arrow.

../../../_images/Chapter11_18.png

4.2.3.1. Reference

Block

Function

Chapter11_18

Run code depending on whether a Boolean condition is true or false.

Chapter11_19

Shows the selected arrow on the LED screen

4.2.4. Python code

Open the .py file with Mu. Code, the path is as below:

File type

Path

File name

Python file

../Projects/PythonCode/04.2_ElectronicCompass

ElectronicCompass.py

After loading successfully, the code is shown as below:

../../../_images/Chapter11_21.png

Check the connection of the circuit and verify it correct,, download the code into the micro:bit and calibrate the electronic compass. After the calibration is successful, place the micro:bit horizontally and rotate the micro:bit to see that the arrow points to the geography Arctic.

The arrow will point to eight directions:: northwest, west, southwest, south, southeast, east, northeast, north, each direction is 45 degrees apart. Assuming that the direction of the micro:bit is rotated 45 degrees from the north to the northeast of the geography, the arrow shown should be reversed, that is, rotated -45 degrees, pointing to the northwest of the micro:bit, which is the geographic north pole. Therefore, the direction of the arrow is adjusted according to the angular offset from the geographic North Pole.

When the variable azimuth is less than 22.5 or greater than 337.5, the arrow points to the true north of the micro:bit.

When the variable azimuth is greater than 22.5 and less than 67.5, the arrow points to the northwest of the micro:bit.

And so on in the same fashion, in every 45 degrees, the arrow points to a particular direction indicating the geographic north, as shown in the following figure:

../../../_images/Chapter11_22.png

The following is the program code:

 1from microbit import *
 2compass.calibrate()
 3while True:
 4    azimuth = compass.heading()
 5    if azimuth<22.5 and azimuth<67.5:
 6        display.show(Image.ARROW_NW)
 7    elif azimuth<67.5 and azimuth<112.5:
 8        display.show(Image.ARROW_W)
 9    elif azimuth<112.5 and azimuth<157.5:
10        display.show(Image.ARROW_SW)
11    elif azimuth<157.5 and azimuth<202.5:
12        display.show(Image.ARROW_S)
13    elif azimuth<202.5 and azimuth<247.5:
14        display.show(Image.ARROW_SE)
15    elif azimuth<247.5 and azimuth<292.5:
16        display.show(Image.ARROW_E)
17    elif azimuth<292.5 and azimuth<337.5:
18        display.show(Image.ARROW_NE)
19    elif azimuth<22.5 or azimuth>337.5:
20        display.show(Image.ARROW_N)

Calibrate the electronic compass first and store the data on the variable azimuth.

compass.calibrate()
azimuth = compass.heading()

Determine the value of the variable azimuth and change the direction of the arrow.

 1if azimuth<22.5 and azimuth<67.5:
 2    display.show(Image.ARROW_NW)
 3elif azimuth<67.5 and azimuth<112.5:
 4    display.show(Image.ARROW_W)
 5elif azimuth<112.5 and azimuth<157.5:
 6    display.show(Image.ARROW_SW)
 7elif azimuth<157.5 and azimuth<202.5:
 8    display.show(Image.ARROW_S)
 9elif azimuth<202.5 and azimuth<247.5:
10    display.show(Image.ARROW_SE)
11elif azimuth<247.5 and azimuth<292.5:
12    display.show(Image.ARROW_E)
13elif azimuth<292.5 and azimuth<337.5:
14    display.show(Image.ARROW_NE)
15elif azimuth<22.5 or azimuth>337.5:
16    display.show(Image.ARROW_N)