Skip to content

Activity

Compass bearing

Beginner | MakeCode, Python | Buttons, Compass | Input/output, Magnetism, Sensors, Using a compass

Step 1: Make it

What is it?

Turn your micro:bit into a simple compass which shows its bearing from magnetic North in degrees.

micro:bit showing 0 degrees numerical reading and a compass pointing North

How it works

  • Your micro:bit has a built-in compass sensor called a magnetometer. You can use it to measure the Earth’s magnetic field and use it as a compass.
  • When you first use the micro:bit compass you have to calibrate it – a little game appears on the screen where you have to tilt the micro:bit to light up every LED, then you’re ready to go.
  • When you press the button A input, the micro:bit takes a reading from the compass sensor and shows the device’s numerical compass bearing on the LED display output. Point the micro:bit North and you should see a reading of 0 degrees.
compass rose showing angles for each point, North, South, South-East etc

What you need

  • micro:bit (or MakeCode simulator)
  • MakeCode or Python editor
  • battery pack (optional)
  • a planet with magnetic poles to stand on – for example, Earth!

Step 2: Code it

1from microbit import *
2compass.calibrate()
3
4while True:
5    if button_a.was_pressed():
6        display.scroll(str(compass.heading()))

Step 3: Improve it

  • Add another button to recalibrate the compass.
  • Have the micro:bit make a sound when it’s pointing in a particular direction – this could be useful to help navigation when you can’t look at a display or for people with impaired vision.
  • Make the micro:bit display letters or arrows to show if it’s pointing North, South, East or West.