Skip to content
Learnearn.uk » Microbit Unit Home » Microbit magnetometer tutorial

Microbit magnetometer tutorial

The Microbit’s compass (magnetometer)

In this topic we will be learning how to use the Microbit’s on-board compass (aka. magnetometer). We will learn how we can use to magnetometer to find north, and use the Microbit’s direction in our projects.

A magnetometer is a device that measures magnetic fields around a device.

Magnetometers are commonly used for:

  • Electronic compasses – detecting the earths magnetic field
  • Detecting buried or hidden metal objects, such as buried treasure or land mines.

Magnetic fields

512px-Earth's_magnetic_field,_schematic.svg

 

Magnetic fields are the areas of magnetic attraction and repulsion that are exerted by magnetically charged objects.

Magnetic fields attempt to line up magnetically charged objects along the line of magnetic field lines. The earth is has a huge magnetic field that runs south to north roughly in line with our south and north poles, and so the magnet in a compass will always try to align itself south to north.

The magnetometer inside of the BBC can detect these magnetic field lines and so can be used as an electronic compass.

Calibrating the Magnetometer

Each time you turn on the Microbit it will ask you to calibrate the compass. To do this just need to tilt the Microbit in different directions until the circle on the display is full up.

To use the magnetometer you need to use the Microbit bearing block.

Watch the animation below and have a go at getting the compass working!

bearing - Microbit magnetometer tutorial

using the microbit bearning block - Microbit magnetometer tutorial

Challenge 1 – Scrolling compass bearing (Easy)

Microbit challenge 13 bearing demo - Microbit magnetometer tutorial

Create a simple compass that scrolls the bearing across the screen every 2 seconds.

 

 

Challenge 2 – Find north

Create a simple compass that displays a smiley face when the Microbit is facing north, and a sad face when it is facing any other direction.

Challenge 3 – Simple compass

Code a simple compass that displays either  N,E,S or W if the Microbit is facing at the exact angle.

Need a hint? Click here!

Here’s some code to get you started!

compassexample

 

Challenge 4 – NESW Compass (Easy)

microbit nesw compass demo

Create a simple compass that displays N/E/S/W depending on direction that the user is facing. The program should update the direction every time the user presses the A button.

Hint

In order to make the compass your are going to need to work out if the direction is within a certain range of degrees. For instance, for us to display E, the compass values will have to be as follows:

Click to enlarge

Click to enlarge

This means that you will need to include and if statement, like so:

E bearning example

 

[/tab

microbit go north compass

Challenge 5 – Go north compass (Medium)

Create a script that helps the user to orientate the compass to north.

The user should display an arrow on the screen and the direction of the arrow should help the user point the compass to north.

 

 

Challenge 6 – Advanced Compass ( e.g. NE,SW ) (Medium) 

Microbit Challenge 16 - Advanced compass demo

Create a compass that displays minor gradations too – E.g. NE

Hint

Here is the full list of compass angle bearings to say some time calculating them!

Bearing From To
N 337 22
NE 22 67
E 67 112
SE 112 157
S 157 202
SW 202 247
W 247 292
NW 292 337

 

Challenge 7 – Compass bearing game (Difficult)

Create a compass game where the game displays a bearing and the user has to change to that direction as quickly as possible. Once the user has turned the compass to the correct direction the Microbit should display how many seconds it took to turn it to the correct direction.

 

Class unplugged activity

Could you a bearings game outside where the user has to work out the bearing to certain objects within an area using the compass they have made. Will need the battery pack!

 

Quiz questions

South is how many degrees? 180

What direction is ENE?

How many milliseconds in a second?

What is 300 milliseconds represented as in seconds?