Lesson 13 Compass


Do you know what a compass is? I believe most of you have played with a compass. Today we are going to use the micro:bit to create a compass and display its direction on the Rainbow ring.

What you need

  • micro:bit board
  • Micro USB Cable
  • micro:bit breadboard adaptor
  • Breadboard
  • LED x2
  • 100 ohm resistors x2
  • Male to male jumper wires


Here, a compass in reality is a magnetometer. The magnetometer is a separate chip that provides magnetic field strength sensing. A software algorithm in the standard runtime uses the on-board accelerometer to turn these readings into a board orientation independent compass reading. The compass must be calibrated before use, and the calibration process is automatically initiated by the runtime software. This device is connected to the application processor via the I2C bus.

Hardware Connection

Please complete hardware connection according to the breadboard diagram below.

After connecting the circuit, you should see the following:

We have to pay attention when we affix the Rainbow LED Ring according to the direction shown in the picture so that it can point in the right direction.


Please open Microsoft Makecode, write your code in the edit area.

Of course, you can download the whole code from the link below.

Code Explanation

Compass Heading
Find which direction on a compass the micro:bit is facing.
The micro:bit measures the compass heading from 0 to 360 degrees with its magnetometer chip. Different numbers mean north, east, south, and west. 0 degree mean the true north.
Rainbow ring has 8 LED beads, which can stand for 8 directions: North, North East, East, South East, South, South West, North West.

We divide this 360 degree coordinate system into 8 segments in average to help us judge the output value of the compass heading. When the value comes to a segment, it means at that time micro:bit point at the direction of the segment shows.

The function has set 2 pixel point in a diagonal into red and blue separately while other LEDs closed. We use red color to stand for true north, blue color to show true south and 0 color value of RGB for extinguish the LED light.

Rotate micro:bit, the direction will change. However, we have to make Rainbow LED Ring always point at the same direction. So we must make Pixels LED light rotate in the opposite direction. Here, we use rotate pixels to define the offset value of Rainbow LED Ring.

The key point of this program is to judge the output value of Compass Heading appears in which direction segment and offset the ring color value in the opposite direction. The whole code seems quite long enough, but actually most of it is very similar.


Rotate the whole device, you can see the Rainbow LED Ring always point at the same direction.
Note: Every time you start to use the compass (for example, if you have just turned the micro:bit on), the micro:bit will start to calibrate compass (adjust itself). It will ask you to draw a circle by tilting the micro:bit. If you are calibrating or using the compass near metal, it might confuse the micro:bit.

If this experiment do not use Rainbow LED Ring but use the arrow displayed on micro:bit as indicator, then how to design circuit and program? We look forward to your comment and further discussion with us.

Taking it further

If this experiment does not use the Rainbow LED Ring but uses the arrow displayed on micro:bit as indicator, then how would you design circuit and program it?

micro:bit Starter Kit Lessons

  • Lesson 01 – LED
  • Lesson 02 – Button
  • Lesson 03 – Trimpot
  • Lesson 04 – Photocell
  • Lesson 05 – RGB LED
  • Lesson 06 – Self-lock Switch
  • Lesson 07 – Temperature Sensor
  • Lesson 08 – Servo
  • Lesson 09 – Buzzer
  • Lesson 10 – Motor
  • Lesson 11 – Rainbow LED
  • Lesson 12 – Accelerometer
  • Lesson 13 – Compass
  • Lesson 14 – Ambient Light


First published at 4:35pm on July 9, 2019
Last updated at 4:35pm on July 9, 2019