Micro:bit Compass & Direction Finder (Python)

Learning Objectives

By the end of this lesson, students will be able to:

1. Concept Overview

The Micro:bit has a built-in magnetometer (compass sensor) that can measure the Earth’s magnetic field and give you a heading from 0° to 359°, where:

But the compass is not accurate until you calibrate it using:

compass.calibrate()

During calibration, students will tilt the Micro:bit and draw a pattern on the LED display. After calibration, the compass will return more accurate heading values.

2. Spec Sheet (Student-Friendly)

Program Name: P1_lastname_compassDirection.py
Device: Micro:bit v2
Language: Python (MicroPython)

Program Requirements

  1. Calibration:
    • When the program starts, it must call compass.calibrate() once.
  2. Compass Reading:
    • Repeatedly read the compass heading with compass.heading().
    • Heading is an integer from 0 to 359.
  3. Direction Ranges:
    • North (N): 315°–359° and 0°–44°
    • East (E): 45°–134°
    • South (S): 135°–224°
    • West (W): 225°–314°
  4. Output:
    • Display N, E, S, or W on the LED matrix depending on the heading.
    • Update the direction repeatedly as the Micro:bit is rotated.
  5. Looping:
    • The program must run in an infinite loop until the Micro:bit is reset.
  6. Timing:
    • Add a small delay (e.g., 200 ms) between readings to avoid flickering and to save power.

3. Pseudocode

START

IMPORT microbit library and compass

CALL compass.calibrate()  // user tilts Micro:bit to complete calibration

REPEAT FOREVER:
    GET current_heading from compass.heading()  // result is 0 to 359

    IF heading < 45 OR heading >= 315:
        direction = "N"
    ELSE IF heading < 135:
        direction = "E"
    ELSE IF heading < 225:
        direction = "S"
    ELSE:
        direction = "W"

    DISPLAY direction letter on the LED screen
    WAIT 0.2 seconds

END REPEAT
END

4. Final Python Code (Micro:bit v2)

Students can copy this into the Micro:bit Python editor (or Mu) and download it to their boards.

from microbit import *
import compass

# Step 1: Calibrate the compass once at the start
compass.calibrate()

while True:
    # Step 2: Read the current heading (0 - 359 degrees)
    heading = compass.heading()
	************ more is needed ***********







    # Step 4: Small delay so the display does not flicker too fast
    sleep(200)

5. Step-By-Step Student Instructions

  1. Open the Editor
    • Go to the Micro:bit Python editor or Mu editor.
    • Create a new file and name it P1_lastname_compassDirection.py.
  2. Type the Code
    • Type the Python program.
    • Check for capitalization and spelling (especially compass.calibrate() and compass.heading()).
  3. Flash to Micro:bit
    • Download the .hex file to the Micro:bit.
    • After flashing, the Micro:bit will ask you to calibrate the compass:
      • Tilt and move the Micro:bit until the pattern is complete.
  4. Test the Direction Finder
    • Hold the Micro:bit flat like a compass.
    • Rotate your body slowly:
      • Check that N shows when facing North.
      • Rotate to find E, S, and W.

6. Required Challenge

If you want to push your stronger students:

  1. Add Diagonal Directions
    • Add NE, SE, SW, NW using smaller angle ranges.
    • Hint: You can scroll text like "NE" using display.scroll("NE").
  2. Arrow Images Instead of Letters
    • Use Image.ARROW_N, Image.ARROW_E, etc. (or custom arrow images) instead of letters.
  3. Direction + Degrees
    • Use display.scroll(str(heading)) to show the actual degree value occasionally.
  4. Compass Game
    • Randomly choose a direction (N, E, S, W).
    • Have the Micro:bit show ?, and the player must rotate until they face that direction.

7. Reflection Questions. Must answer questions to get secret work.

Have students answer in 2–3 sentences each:

  1. Why does the Micro:bit need to be calibrated before using the compass?
  2. How did you decide the angle ranges for N, E, S, and W?
  3. How could a compass program like this be useful in a real-world project (e.g., navigation, robotics)?

The name of the file will be:
PX_lastname_Directions

Files to save on Google Drive under your class and submit to Google Classroom

  1. PX_lastname_Directions.png — Screenshot inside Python editor showing your code.
  2. PX_lastname_Directions.py — Your Python source file.
  3. PX_lastname_Directions.txt — Plain-text copy of your code.
  4. PX_lastname_Directions.hex — Hex file to run on the physical micro:bit.
  5. PX_lastname_Directions.mp4 — Short demo video of the program running on the board.

Submission requirements