No related product information!

Lesson 12 Make a Compass

2264

  In this lesson, we will carry out an interesting experiment to make a compass with the Micro:bit.

12.1 Components to be prepared

image.png

12.2 LED display

The front LED display of Micro:bit consists of 25 5x5 lattice LEDs.

25 LEDs arranged in a 5x5 grid make up the display for showing pictures, words and numbers.

image.png


12.3 Compass

(1)A digital compass is an input sensor that detects magnetic fields. Your BBC micro:bit has an inbuilt compass that can detect the direction in which it is facing. Find magnetic North or measure the strength of magnetic fields using the micro:bit's compass.

image.png 

(2)A digital compass is an input sensor that detects magnetic fields. Your BBC micro:bit has an inbuilt compass that can detect the direction in which it is facing.

Watch the video to find out more, then choose a project to start using your micro:bit as a compass.

https://microbit.org/get-started/user-guide/features-in-depth/#compass


12.4 Circuit

Connect micro:bit and PC with a Micro USB cable.

image.png

12.5 How it works and calibrate the compass

1.Your micro:bit has a compass sensor called a magnetometer that measures magnetic fields. It can sense the Earth's magnetic field and so you can use it as a compass.

2.When you first use the micro:bit compass you have to calibrate it. A little game appears on the screen where you have to tilt the micro:bit to light up every LED, then you’re ready to go.

If this is the first time you have used an Micro:bit magnetic field sensor, the pattern of the LED matrix will be the“TILT TO FILL SCREEN”.
      For the way to calibrate a Micro:bit electronic compass, please click the following link to view the video.

https://support.microbit.org/support/solutions/articles/19000008874-calibrating-the-micro-bit-compass

[Note]

Also, because the magnetometer is sensitive to magnetic fields, you should avoid attaching the micro:bit to any metal objects or rest it on a metal table, as this will affect both the calibration and the accuracy of the reading in normal use.

3.The program uses an infinite (forever) loop to keep taking compass readings and it stores them in a variable called ‘bearing’. It then uses selection: an if… else statement to show N for North on the LED display if the bearing is greater (>) than 337.5 degrees or less than (<)22.5. This means that it will show you where North is as long as your micro:bit is pointing in roughly the right direction.

image.png


12.6 MakeCode programming

  We will use an online MakeCode Editor to complete the experiment in this lesson, as shown below.

12.6.1 Start programming

(1) Log in to the website

1. You need to enter the URL in the address bar of Google Browser:

https://makecode.microbit.org/

2. After the website is successfully opened, the interface as shown below will appear:

image.png


(2) Import a project

1. In the HOME interface, click the "Import" button to import the external ".hex" file:

image.png


In the pop-up dialog box, select the "Import File", as shown in the following figure:

image.png


Click the "Choose File":

image.png


Find the code file for this lesson:

BBC_Microbit_Sensor\Code\Lesson_12\BlockCode

Select the file in ".hex" format and click the Open:


image.png


2. Notice whether the file has been loaded into the following window, and then click the "Go ahead!" button, as shown in the following figure:


image.png


3. Open the file successfully, as shown in the following figure:

image.png


12.6.2 Run the program

1. After the program is written, connect micro:bit and PC with a Micro USB cable.

2. After micro:bit is connected to the computer, you need to first "Pair device". Click the image.png button on the right of image.png in the lower left corner, and then click the image.png option, as shown in the following figure:

image.png


Then click image.png in the lower right corner:

image.png


Then the following dialog box will pop up, select image.png, and then click image.png

image.png


After the device is successfully paired, the image.png button changes to image.png

image.png


3. Start to download the program to Micro:bit, and click the image.png button. Generally, the program will be downloaded directly to the Micro:bit. After the download is completed, your Micro:bit will restart and run the program just downloaded.Observe the LED screen of the Micro:bit. If this is the first time you have used a Micro:bit magnetic field sensor, the pattern of the LED matrix will be the“TILT TO FILL SCREEN”.At this time, you need to debug the Micro:bit, and for specific methods, see the "12.4" section of this lesson.

When you calibrate the Micro:bit compass, you need to light up every LED, as shown in the following figure:

image.png


After calibration, a smiling face pattern will appear on the LED screen, as shown in the following figure:

image.png


4.You need to rotate the Micro:bit horizontally, download a“compass”app on your phone and observe the change of the arrow direction on the LED screen combining with the app.

image.png 


[Note]:

If Micro:bit doesn't respond after clicking the image.png, you need to click the image.png button on the right of the image.png, and then click the image.png , and observe the situation of the Micro:bit again, as shown in the following figure:

image.png


 If you have problems, please send us an email: support@adeept.com


12.6.3 Learn the code program

In the program, we use the following instruction blocks, which are explained as follows:


Block

Function

image.png 

This is an instruction block to calibrate the compass.

image.png 

This is an instruction block to obtain the direction of the compass. 

image.png 

This is an arrow pattern instruction block to show the direction on the LED screen.

image.png 


This is an instruction block for the conditional judgment statement of the“if true/false then”.


image.png 

This is a comparison operator instruction block.

image.png 

This is an instruction block for the “and” comparison operator



12.7 Python programming

12.7.1 Run the program

1.Connect micro:bit and PC with a Micro USB cable.

2. Open the Mu Editor installed on the computer, and click the button [Load] in the upper left corner to open the source code program of this lesson:

image.png 


Find the code file for this lesson:

BBC_Microbit_Sensor\Code\Lesson_12\PythonCode

Select the file in ".py" format and click the Open:

image.png 


3. Click the [Flash] button to download the program to Micro:bit, as shown in the following figure:

image.png 

 

 

    4.After the program is downloaded, you need to rotate the Micro:bit horizontally, download a“compass”app to your phone and observe the change of the arrow direction on the LED screen combining with the app.

image.png

 

【Note】:

After you click the [Flash] button, if there is no change on the LED screen of Micro:bit, you need to restart the Micro:bit, and then click the [Flash] button again.

    If you have problems, please send us an email: support@adeept.com


12.7.2 Learn the code program

The source codes are as follows:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

from microbit import *

compass.calibrate()

needle = 0

while True:

    needle = compass.heading()

    if needle >= 338 or needle <= 23:

        display.show(Image.ARROW_N)

    elif needle > 23 and needle < 68:

        display.show(Image.ARROW_NE)

    elif needle >= 68 and needle <= 113:

        display.show(Image.ARROW_E)

    elif needle > 113 and needle 158:

        display.show(Image.ARROW_SE)

    elif needle >= 158 and needle <= 203:

        display.show(Image.ARROW_S)

    elif needle > 203 and needle < 248:

        display.show(Image.ARROW_SW)

    elif needle >= 248 and needle <= 293:

        display.show(Image.ARROW_W)

    elif needle 293 and needle < 338:

        display.show(Image.ARROW_NW)



(1)Starts the calibration process. An instructive message will be scrolled to the user after which they will need to rotate the device in order to draw a circle on the LED display.


2

compass.calibrate()

 

(2)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.


5

needle = compass.heading()

 

  (3) You can determine which direction the compass is pointing in by determining the angle that the needle points to.

If you don't like to use arrows to indicate directions, you can use this statement to output the corresponding direction: 

display.show(“N”)  # ‘N’ means due north

image.png 

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

if needle >= 338 or needle <= 23:

    display.show(Image.ARROW_N)

elif needle > 23 and needle < 68:

    display.show(Image.ARROW_NE)

elif needle >= 68 and needle <= 113:

    display.show(Image.ARROW_E)

elif needle > 113 and needle 158:

    display.show(Image.ARROW_SE)

elif needle >= 158 and needle <= 203:

    display.show(Image.ARROW_S)

elif needle > 203 and needle < 248:

    display.show(Image.ARROW_SW)

elif needle >= 248 and needle <= 293:

    display.show(Image.ARROW_W)

elif needle 293 and needle < 338:

    display.show(Image.ARROW_NW)