micro:bit Compass

1 min read

micro:bit Compass

1 min read

The other day I bought a micro:bit for my daughter. It’s a small computer with bluetooth, an accelerometer, light sensor, magnetometer and temperature sensor. It also has a couple of buttons for input and a matrix of LEDs for output. It’s become obvious that I’m way more interested in playing with it than my daughter is but I’m hopeful that after I show her what it can do, she’ll take an interest in writing some code for it herself.

My first small program takes the input from the compass and writes out the cardinal directions, N, S, E and W.

This is what the program looks like in Blocks

and here is the equivalent JavaScript code

let heading = 0
basic.forever(() => {
    heading = input.compassHeading()
    if (heading < 45 || heading > 315) {
    } else if (heading < 135) {
    } else if (heading < 225) {
    } else {

Below is an emulator running this code. Drag the compass near the top and you will see the text on the micro:bit change.

Finally, here is a video of the code running on the actual device

If you’d like to play with this project yourself, you can do so here: https://makecode.microbit.org/_Db2YbcfvcLME

No Comments

Leave a Reply

I accept the Privacy Policy