Micro:bit compass example in micropython

The micro:bit also has a compass (magnetometer)

The compass has to be calibrated before it can give accurate readings. Calibration involves moving a dot to make a circle on the matrix. Once this is complete you get a smiling face image displayed to show you that you have successfully completed the calibration.

Again you will need to click on the REPL button in the mu editor prior to flashing the micro:bit

Move the Micro:bit about and you should see different values in the console, 0 is equivalent to North

Code

from microbit import *
 
compass.calibrate()
while True:
    bearing = compass.heading()
    print(bearing)
    sleep(100)

As part of an experiment you could use the display object to display one of the corresponding images

 

  • Image.ARROW_N, Image.ARROW_NE, Image.ARROW_E, Image.ARROW_SE, Image.ARROW_S, Image.ARROW_SW, Image.ARROW_W, Image.ARROW_NW