Microbit Python Computer Science
Beginner
40 mins
Teacher/Student led
What you need:
Chromebook/Laptop/PC or Microbit

Compass Navigator

In this lesson, you'll build a digital compass with your Micro:bit, using its magnetometer to point north and show directional arrows. Learn to calibrate it, map headings, and use coding concepts like functions and control structures.
Learning Goals Learning Outcomes Teacher Notes Lesson Files

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Introduction to the Compass Project

    In this lesson, you'll create a digital compass using your Micro:bit's built-in magnetometer. The compass will point north and display directional arrows on the LED grid. You'll learn to calibrate the compass using functions, map headings to arrow images stored in a tuple, and use control structures like if-elif statements. We'll also reinforce string manipulation for error messages and use lists for direction data.

    By the end of this lesson, your Micro:bit will act as a portable compass. Open the Micro:bit Python editor at python.microbit.org and start a new project.

    Test your code often in the simulator. Tilt the virtual Micro:bit to simulate direction changes and observe the compass heading.

    2 - Import Modules and Basic Setup

    Start by importing the necessary modules. We'll need the microbit module to access the hardware, including the compass.

    Add the following complete code to your editor:

    from microbit import *
    This imports everything from the microbit module. Run the code; nothing visible will happen yet, but it should execute without errors.

    3 - Calibrate the Compass

    Before using the compass, it needs calibration. We'll create a function to handle this and display a message. Functions help organise code and make it reusable. We'll use string manipulation to create an error message if calibration fails.

    Update your code to the following complete version:

    from microbit import *
    
    def calibrate_compass():
        compass.calibrate()
        display.scroll("Calibrated")
    
    calibrate_compass()
    Avoid copy-pasting the code. Type each line to understand the logic and structure. This will help with exams and real coding tasks.
    Run the code. Follow the on-screen instructions to tilt the Micro:bit and draw a circle with the LED. It should scroll 'Calibrated' when done.
    Calibration aligns the magnetometer. If it fails, you might see an error; try adding a check like if not compass.is_calibrated(): display.scroll('Error: Try again').

    4 - Get and Display Heading

    Now, let's get the compass heading (0-359 degrees, where 0 is north) and display it. We'll use a loop to continuously update it.

    Update your code to this complete version:

    from microbit import *
    
    def calibrate_compass():
        compass.calibrate()
        #display.scroll("Calibrated") #Note: comment this out
    
    calibrate_compass()
    
    while True:
        heading = compass.heading()
        display.scroll(str(heading))
        sleep(500)
    Run the code. After calibration, it scrolls the current heading every half second. Use the compass slider tool in the project editor to simulate a change in direction for the microbit.
    We convert the integer heading to a string with str() for display, reinforcing string manipulation. The while True loop makes it continuous.

    5 - Define Directions and Arrows

    We'll use a list for direction names and a tuple for arrow images. Tuples are immutable, perfect for fixed data like images. We'll map headings to 8 directions.

    Update your code to this complete version:

    from microbit import *
    
    directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
    
    arrows = (
        Image.ARROW_N,
        Image.ARROW_NE,
        Image.ARROW_E,
        Image.ARROW_SE,
        Image.ARROW_S,
        Image.ARROW_SW,
        Image.ARROW_W,
        Image.ARROW_NW
    )
    
    def calibrate_compass():
        compass.calibrate()
        #display.scroll("Calibrated")
    
    calibrate_compass()
    
    while True:
        heading = compass.heading()
        display.scroll(str(heading))
        sleep(500)
    Run the code. It still shows the heading, but now we have directions list and arrows tuple defined. Nothing new displays yet.
    Lists are mutable, but tuples aren't – good for constants. Try display.show(arrows[0]) to test showing the north arrow.

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more