Someone already wrote the code for you, so you just need a nice cover and your Micro:bit can be used as a compass.
https://www.instructables.com/id/Microbit-Compass/