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 |
|
|
4.1.2. Circuit
Connect micro:bit and PC via micro USB cable.
Hardware connection
4.1.3. Block code
Open MakeCode first. Import the .hex file. The path is as below:
File type |
Path |
File name |
HEX file |
../Projects/BlockCode/04.1_DisplayMagnetometerData |
DisplayMagnetometerData.hex |
After importing successfully, the code is shown as below:
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:
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:
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.
The angular offset read from the magnetometer chip is stored in the variable azimuth.
Then the value of the variable azimuth is printed on the serial port interface.
4.1.3.1. Reference
Block |
Function |
|
Set the number of data pins and LEDs, as well as the type of LED. |
|
Set LED color |
|
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:
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.
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:
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:
The angular offset is the angle between the direction of the micro:bit and the geographic north pole, as shown in the following figure.
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 |
|
|
4.2.2. Circuit
Connect micro:bit and PC via micro USB cable.
Hardware connection
4.2.3. Block code
Open MakeCode first. Import the .hex file. The path is as below:
File type |
Path |
File name |
HEX file |
../Projects/BlockCode/04.2_ElectronicCompass |
ElectronicCompass.hex |
After importing successfully, the code is shown as below:
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:
The angular offset read from the magnetometer chip is stored in the variable azimuth
Determine the value of the variable azimuth to change the direction of the arrow.
4.2.3.1. Reference
Block |
Function |
|
Run code depending on whether a Boolean condition is true or false. |
|
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:
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:
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)





