FREE SHIPPING OVER \$89; 10% OFF FOR YOUR FIRST ORDER (CODE:NEW10); \$20 OFF OVER \$179 (CODE:BL20); \$30 OFF OVER \$259(CODE:BL30)

# Microbit Python Tutorials 9: Direction follower

Posted by SuFiona on

Course9-Direction follower

Learning goals:

This lesson learns the use of compasses to achieve the orientation of the micro:bit, and the arrows above the micro:bit dot matrix point to the north.

Code

from microbit import *

compass.calibrate()

while True:

needle = ((15 - compass.heading()) // 30) % 12

display.show(Image.ALL_CLOCKS[needle])

In the program, the compass.calibrate() function is first called to perform compass calibration. The calibration process is as shown in Figure 9-1. The small red dot in the center is drawn on the micro:bit dot matrix. After the description, a smiley, will appear on the dot matrix, which indicating calibration is completed.

Figure 9-1

1.You should open the Mu software, and enter the code in the edit window, , as shown in Figure 9-2.

Note! All English and symbols should be entered in English, and the last line must be a space.

Figure 9-2

2.As shown in Figure 9-3, you need to click the Check button to check if our code has an error. If a line appears with a cursor or an underscore, the program indicating this line is wrong.

Figure 9-3

3.You need to connect the micro data cable to micro:bit and the computer, then click the Flash button to download the program to micro:bit as shown in Figure 9-4.

Figure 9-4

4. Experimental phenomena as shown in Figure 9-5 to Figure 9-10, no matter how you turn micro:bit, the pointers on the dot matrix point to the north.

Figure 9-5                                    Figure 9-6                                   Figure 9-7

Figure 9-8                                     Figure 9-9                                Figure 9-10

The code of the experiment:  9.Direction follower.zip