Step 1: Make it
What is it?
This simple compass will show you which way is North.
![micro:bit showing N for North on LED display next to compass pointing North](https://cdn.sanity.io/images/ajwvhvgo/production/db90b21b419e4140635f958476ff548bb5ee8b1e-1911x1171.png?w=653&q=80&fit=max&auto=format)
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
Step 3: Improve it
- Make the compass more accurate by reducing the range of bearings: make the number 45 smaller and 315 bigger.
- Add other points of the compass to show when the micro:bit is pointing East, West and South.
- Add sound so it makes a noise when pointing North so someone who is visually impaired can use the compass.
![Compass rose showing numerical bearings for N, S, E, W, NE, SW etc](https://cdn.sanity.io/images/ajwvhvgo/production/c1ba4627f246bc638f48cd51afb80342fd1db540-2019x1878.png?w=653&q=80&fit=max&auto=format)
This content is published under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) licence.