Digital Compass
What you'll build¶
A compass needle on the LED matrix that always points north. Press A to scroll the heading in degrees,
or B to scroll the direction (N, NE, E...).
Code¶
Calibrating
The first time it runs, the micro:bit asks you to tilt it to fill the screen: roll it around until
every LED is lit. Calibrate again with compass.calibrate() if the readings drift or you move near metal.
How it works¶
compass.heading()returns 0–359°, the direction the top of the board (the edge opposite the pins) faces. 0° is north, 90° is east.Image.ALL_CLOCKSis a list of 12 clock-hand images, one every 30°.((15 - heading) // 30) % 12picks the hand pointing back towards north: turn to face east (90°) and the needle points left, to 9 o'clock.(heading + 22) // 45splits the circle into eight 45° slices, centred on N, NE, E and so on.
Keep the board flat: the heading is only accurate when the micro:bit is level.
Try this next¶
- Show an arrow that points to a chosen direction, like your front door.
- Use
compass.get_field_strength()to make a metal detector. - Pair it with the Spirit Level so you know when the board is flat enough.