micro:bit How-to: Build Your Own Digital Compass

Description

This project uses the compass sensor embedded on the back of the micro:bit to determine direction and display it on the LED array. The compass uses the earth’s magnetic field to determine the direction of magnetic north, which varies in magnitude and direction based on the earth’s movement.

True north is a fixed point on the earth. An adjustment needs to be made to translate magnetic north to true north, and that adjustment varies according to location. This project only uses magnetic north.

The compass is a sensor that is located in the lower left corner on the back of the micro:bit. It is labeled ‘compass’.

Programming Language

MakeCode Blocks

Difficulty Level

Intermediate

Age Range

11 and up

Materials

Optional materials:

Programming the micro:bit

Develop the problem statement

I want to create a regular compass that helps me navigate a path, so the problem statement I am using is:

Identify the current direction of the micro:bit.

Write the algorithm

My problem statement is focused on identifying the current direction. My algorithm needs to show how I plan to display the direction. I know that the micro:bit represents direction as a degree, with 0 or 360 degrees being north and 180 degrees being south. The degree increases in a clockwise direction, so east is at 90 degrees and west is at 270 degrees. I need to establish a range of values for each direction I want to display. I want to show northeast, southeast, southwest, and northwest, in addition to the standard four directions, so I drew a diagram to help me find the ranges for the 8 directions I plan to display. Here is the diagram and a table of the ranges I will use:

Using the diagram and table, I developed the following algorithm:

  • To start:
    • Create a variable to store the current angle and set the variable to zero
  • Forever:
    • If the angle is between 23 and 67, display NE
    • If the angle is between 68 and 112, display E
    • If  the angle is between 113 and 157, display SE
    • If the angle is between 158 and202, display S
    • If the angle is between 203 and 247, Display SW
    • If the angle is between 248 and 292, display W
    • If the angle is between 293 and 337, dispay NW
    • If the angle is lessthan 22 OR greater than 338 display N

Write the pseudocode

The most efficient way to write the pseudocode is to put the check for north as the first comparison. This eliminates the need for doing two comparisons for any other range, as long as I then do the comparisons in order, either from low to high or high to low. Here is my version of the pseudocode:

  • onStart:
    • Calibrate the micro:bit
    • Create the variable mbAngle
  • Do Forever:
    • If (mbAngle ≤ 23) or (mbAngle >338)
      • Display ‘N’
    • Else If (mbAngle ≤ 67)
      • Display ‘NE’
    • Else if (mbAngle ≤ 112)
      • Display ‘E’
    • Else if (mbAngle ≤ 157)
      • Display ‘SE’
    • Else if (mbAngle ≤ 247)
      • Display ‘SW’
    • Else if (mbAngle ≤ 292)
      • Display ‘W’
    • Else
      • Display ‘NW’

Write and test the code

The next step is to have your students translate the pseudocode into block code.

Here is the link to the Makecode file for my version for you to use. Click it and it will open in a MakeCode window.

Academic Connections

Programming Concepts

  • Decision-making using Boolean Logic

Math

  • The geometry of dividing a circle into areas using degrees

NGSS Concepts

  • PS2: Forces and Interactions
  • ESS2: Earth’s Systems

Online Resources