Login

Please fill in your details to login.





micro:bit - compass

This page is mainly about micro:bit - compass
Compass
Where we learn how to access the compass, join strings together, use selection to make choices and stand in a field.

The built in compass uses the Earths magnetic field to work out which direction the micro:bit is pointing. You will probably have to calibrate the compass before your scripts will run - follow the instructions on the screen.

BL / JS / PY : Create a simple script which displays the compass heading, H, as 'Heading : H' scrolling on the display. You will have to use the 'join' block from the 'text' section.
BL / JS / PY : The display doesn't really respond quick enough to changes in heading because the scrolling message locks up the micro:bit until it finishes. Adapt your script so that it displays 'N', 'S', 'E' or 'W' based on the direction (in degrees) the micro:bit is facing. (HINT : There are two situations where the micro:bit should display 'N'). You might want to introduce a slight pause after the micro:bit displays the heading to stop the display flashing. You will probably also find that the simulator works perfectly but your micro:bit goes a bit crazy. This is because of local magnetic fields - connect your battery pack and take your micro:bit out into a field to see it working properly.
BL / JS / PY : Get some variables in there! You will notice that the 'compass.heading' block appears 5 times in the last script meaning that the sensor will be 'polled' 5 times during each cycle of the forever loop. Adapt your scripts so that they use a single variable, 'heading', which is set at the start of the loop and then used in the comparison statements.

(microbit-compass-heading)
(microbit-compass-heading-letter)
(microbit-compass-heading-letter-variable)

Extension : Try outputting the compass heading to the serial port so you can monitor the reading live using the simulator, REPL in Mu or a terminal window using Putty. You can either use the 'serial' blocks in the Block and Javascript editor or simply 'print' the compass heading using micropython.

Last modified: October 5th, 2021
The Computing Café works best in landscape mode.
Rotate your device.
Dismiss Warning