micro:bit Digital Compass: Arrow to North
Turn the board: the arrow on the LED display always points north. The compass inside the board measures the direction (compass.heading) and the closest of eight arrows is picked. No extra parts are needed.

Video
Circuit Diagram

Source Code
1# ─────────────────────────────────────────
2# BlueGrays · bluegrays.com
3# micro:bit Digital Compass: Arrow to North
4# ─────────────────────────────────────────
5# The board finds north with its built-in compass and the arrow on the LED
6# display always points north. Turn the board and the arrow turns too. On
7# the first run you tilt the board to fill the display: that sets up the
8# compass. No extra parts are needed.
9#
10# Pins:
11# LED -> Onboard 5x5 LED display
12# COMPASS -> Onboard compass
13
14from microbit import *
15
16# ============ SETTINGS ============
17LOOP_MS = 100 # Loop pause (ms)
18
19while True:
20 heading = compass.heading() # 0-359 degrees, 0 = north
21 # North is this many degrees clockwise from the top of the board.
22 turn = (360 - heading) % 360
23 # There are eight arrows, one for each 45 degree slice; pick the closest one.
24 arrow = ((turn + 22) // 45) % 8
25 display.show(Image.ALL_ARROWS[arrow])
26 sleep(LOOP_MS)
27FAQ
Why does a fill-the-screen game appear?
That is the compass calibration. The first time the code reads a heading, the board asks you to tilt it in every direction until all the dots on the display are lit. This is how it learns the magnetic field around it. Then a happy face shows and the program goes on. After you send new code it may ask again.
Why does the arrow sometimes point the wrong way?
The compass measures the Earth's magnetic field, and magnets, speakers, phones, computers and iron objects nearby confuse it. Move the board away from them and hold it flat. With eight arrows the direction shown can be off by up to 22.5 degrees; that is normal.
How do I put this code on the micro:bit?
The code is MicroPython. Open the micro:bit Python Editor (python.microbit.org) on a computer, delete the code that is there and paste this one. Connect the board with a USB cable and press Send to micro:bit. If that button does not work, use Save to download the .hex file and copy it to the drive called MICROBIT. You can rebuild the same project with blocks in MakeCode, but this code cannot be pasted there.
Does it work on the older micro:bit (V1) too?
The code is written for the micro:bit V2. Projects that only use the LED display, buttons A and B and the accelerometer also run on a V1. The speaker, the microphone and the touch logo exist only on the V2, so code that uses them gives an error on a V1. If your board has a speaker on the back and a gold logo on the front, it is a V2.
