Activity

# Compass North

Intermediate | MakeCode, Python | Compass, LED display | Boolean logic, Magnetism, Relational operators, Selection, Sensors, Using a compass, Variables

## Step 1: Make it

### What is it?

This simple compass will show you which way is North.

### How it works

• Your micro:bit has a compass sensor called a magnetometer that measures magnetic fields. It can sense the Earth’s magnetic field and so you can use it as a compass.
• When you first use the micro:bit compass you have to calibrate it. A little game appears on the screen where you have to tilt the micro:bit to light up every LED, then you’re ready to go.
• The program uses an infinite (forever) loop to keep taking compass readings and it stores them in a variable called ‘bearing’. It then uses selection: an if… else statement to show N for North on the LED display if the bearing is greater (>) than 315 degrees or less than (<) 45. This means that it will show you where North is as long as your micro:bit is pointing in roughly the right direction.

### What you need

• micro:bit (or MakeCode simulator)
• MakeCode or Python editor
• battery pack (optional)
• a planet with magnetic poles to stand on, such as Earth!

## Step 2: Code it

``````1from microbit import *
2compass.calibrate()
3
4while True: