In this tutorial, I will show you how to develop compass that displays which direction the BBC micro:bit board is pointing. These are versatile instruments that play a crucial role in modern technology and scientific research, including navigation, surveillance and astronomy.
Parts Required
- BBC micro:bit;
- BBC micro:bit USB cable;
- BBC micro:bit battery holder.
What is a compass?
A compass is a navigational instrument used to determine direction relative to the Earth’s magnetic poles (Figure 1). The Earth itself acts as a giant magnet with a magnetic north and south pole. The compass needle, being magnetized, aligns itself with the Earth’s magnetic field lines. One end of the needle points towards the magnetic north pole, and the other end points towards the magnetic south pole.
The main component of a traditional compass is a small, lightweight magnetized needle that is balanced on a pivot point. This allows the needle to rotate freely in response to the Earth’s magnetic field. As you can see in Figure 2, most compasses have a circular card marked with directions (North, East, South, West) and degrees. North corresponds to 0°, and the degrees increase clockwise, meaning that east is 90°, south is 180°, and west is 270°.
Digital Compass
Likewise, a digital compass is an input sensor that detects magnetic fields, using a magnetometer. A magnetometer is often a small chip that integrates one or more technologies, such as the Hall effect, where a voltage difference is produced across an electrical conductor through which a current is flowing, perpendicular to an applied magnetic field (for more information about the Hall effect, be sure to check this tutorial).
For our convenience, the BBC micro:bit has an inbuilt compass (LSM303AGR) that can detect the direction in which it is facing. The compass, however, needs to be calibrated in order to adjust for local magnetic interferences and ensure accurate readings. Calibration typically involves moving the device in specific patterns to recalibrate its sensors.
Code
In this tutorial, we will start by creating a basic compass to understand the fundamental concepts. Then, we will move on to designing a more detailed and sophisticated compass, incorporating advanced features and functionalities.
In order to develop a simple compass, which only displays the main cardinal directions we will use Microsoft MakeCode for micro:bit. Be sure to refer to this tutorial for more information.
Let’s code!
- First, as mentioned before, it is crutial to calibrate the compass sensor. To do so, we will place the “calibrate compass” sequence in the “on start” event;
- Then, we need to store the “compass heading” of the micro:bit in a variable, which we are going to call direction, and place it in the forever loop [1];
- If the “direction” angle is less than 45º and more than 315º, then we consider that the compass heading is mostly pointing toward North. Thus, an “N” shall be displayed on the micro:bit´s screen;
- If the “direction” angle is less than 135º, then we consider that the compass heading is mostly pointing toward East, and so an “E” shall be displayed on the micro:bit´s screen;
- If the “direction” angle is less than 1225º, then we consider that the compass heading is mostly pointing toward South, and so an “S” shall be displayed on the micro:bit´s screen;
- Finally, if the “direction” angle is less than 315º, then we consider that the compass heading is mostly pointing toward West, and so an “W” shall be displayed on the micro:bit´s screen. The end result shall be as follows:
- JavaSript:
let direction = 0
input.calibrateCompass()
basic.forever(function on_forever() {
direction = input.compassHeading()
if (direction < 45 || direction >= 315) {
basic.showString("N")
} else if (direction < 135) {
basic.showString("E")
} else if (direction < 225) {
basic.showString("S")
} else if (direction < 315) {
basic.showString("W")
}
})
- Python:
direction = 0
input.calibrate_compass()
def on_forever():
global direction
direction = input.compass_heading()
if direction < 45 or direction >= 315:
basic.show_string("N")
elif direction < 135:
basic.show_string("E")
elif direction < 225:
basic.show_string("S")
elif direction < 315:
basic.show_string("W")
basic.forever(on_forever)
Tip: in Microsoft MakeCode, you can rotate the micro:bit’s logo (Figure 4) to simulate different angles and verify if your code is working as intended.
Now, connect your micro:bit to your computer, click Download and follow the screen instructions. You can follow the screen instructions or watch this video to calibrate your compass.
For the more sophisticated compass, it will include the remaining cardinal directions, plays different notes depending on the micro:bit’s orientation, and adjusts the screen brightness accordingly. To enhance clarity, Figure 4 displays a compass rose illustrating the cardinal directions and their corresponding degrees.
The code structure remains largely unchanged. One simply needs to adjust the angles and incorporate “set brightness” and “ring tone (Hz)” blocks as intended. In this scenario, when the compass points to North, East, South, or West, the screen brightness increases compared to the other directions. When the compass aligns exactly at 0º, 90º, 180º, or 270º, the screen brightness reaches its maximum value, accompanied by a specific note (with the note for North being the highest). The following table resumes the degrees (θ) considered for each of the cardinal directions:
Cardinal direction | direction < θ | direction ≥ θ |
N | 22.5º | 337.5º |
NE | 67.5º | 22.5º |
E | 112.5º | 67.5º |
SE | 157.5º | 112.5º |
S | 202.5º | 157.5º |
SW | 247.5º | 202.5º |
W | 292.5º | 247.5º |
NW | 337.5º | 292.5º |
The end result shall be as follows:
- Blocks:
- JavaScript:
let direction = 0
input.calibrateCompass()
basic.forever(function () {
direction = input.compassHeading()
if (direction < 22.5 || direction >= 337.5) {
basic.showString("N")
led.setBrightness(200)
music.ringTone(0)
if (direction == 0) {
led.setBrightness(255)
music.ringTone(494)
}
} else if (direction < 67.5) {
basic.showString("NE")
led.setBrightness(200)
music.ringTone(0)
} else if (direction < 112.5) {
basic.showString("E")
led.setBrightness(200)
music.ringTone(0)
if (direction == 90) {
led.setBrightness(255)
music.ringTone(349)
}
} else if (direction < 157.5) {
basic.showString("SE")
led.setBrightness(200)
music.ringTone(0)
} else if (direction < 202.5) {
basic.showString("S")
led.setBrightness(200)
music.ringTone(0)
if (direction == 180) {
led.setBrightness(255)
music.ringTone(349)
}
} else if (direction < 247.5) {
basic.showString("SW")
led.setBrightness(200)
music.ringTone(0)
} else if (direction < 292.5) {
basic.showString("W")
led.setBrightness(200)
music.ringTone(0)
if (direction == 270) {
led.setBrightness(255)
music.ringTone(349)
}
} else if (direction < 337.5) {
basic.showString("NW")
led.setBrightness(200)
music.ringTone(0)
}
})
- Python:
direction = 0
input.calibrate_compass()
def on_forever():
global direction
direction = input.compass_heading()
if direction < 22.5 or direction >= 337.5:
basic.show_string("N")
led.set_brightness(200)
music.ring_tone(0)
if direction == 0:
led.set_brightness(255)
music.ring_tone(494)
elif direction < 67.5:
basic.show_string("NE")
led.set_brightness(200)
music.ring_tone(0)
elif direction < 112.5:
basic.show_string("E")
led.set_brightness(200)
music.ring_tone(0)
if direction == 90:
led.set_brightness(255)
music.ring_tone(349)
elif direction < 157.5:
basic.show_string("SE")
led.set_brightness(200)
music.ring_tone(0)
elif direction < 202.5:
basic.show_string("S")
led.set_brightness(200)
music.ring_tone(0)
if direction == 180:
led.set_brightness(255)
music.ring_tone(349)
elif direction < 247.5:
basic.show_string("SW")
led.set_brightness(200)
music.ring_tone(0)
elif direction < 292.5:
basic.show_string("W")
led.set_brightness(200)
music.ring_tone(0)
if direction == 270:
led.set_brightness(255)
music.ring_tone(349)
elif direction < 337.5:
basic.show_string("NW")
led.set_brightness(200)
music.ring_tone(0)
basic.forever(on_forever)
That’s it! If you enjoyed this tutorial, you can visit our YouTube channel and watch our many tutorials. Thanks for following us and be sure to rate, comment and share our content.
References
[1] https://makecode.microbit.org/projects/compass
Towards the Future !!! 😉