BBC micro:bit Compass

Introduction

The micro:bit has a built-in magnetometer. Magnetometers are used to sense the direction of the strongest magnetic force they feel. When combined with an accelerometer, they can be used give compass bearings.

If you've used a compass App on a smartphone, you'll know that you tend to have to calibrate the compass first by rotating the phone a few times. This is also true with the micro:bit and it happens automatically. You need to tilt the circuit board to move a dot on the matrix until you have made a circle. You might find this hard the first few times, but it does become quite straightforward after a while. If you have the appropriate battery supply, you might find this one easier than when connected via USB cable.

I'll leave it for you to work out how the reading relates to the exact orientation of the circuit board.

Programming

The following program shows an 'N' on the matrix when the compass is pointing 0-44°, from North to North East.

Extending the idea a little, we can make something a little more useful. Let's plan out that program now. Here is a diagram showing the points of the compass, with North shown at the top.

It's going to be tricky to hold the micro:bit still on the exact reading, so we can allow for a little slack. To start with, let's go with 20° either side of the exact figure. You can make a more refined version later. Here is a table of the logic that we need.

Compass PointImageLogic