About this project
In this project, students can learn how to turn their BBC micro:bit into a working compass by programming a simple script that displays the direction in which the device is facing on the LED array.
What you'll need
1 x BBC micro:bit
1 x Micro USB
1 x Computer or tablet
2 x AAA Batteries & Battery Holder (optional)
Project walkthrough
- Go to the BBC micro:bit website, click 'create code' and begin a new Microsoft Block Editor project.
- Begin by dragging in a 'forever' loop from the Basic commands menu on the left-hand side of the screen.
- Connect a 'set [item] to' tile from the Variables menu, and rename the variable to 'Degrees' by clicking the dropdown.
- Attach a 'compass heading (°)' tile from the Input menu.
- Now drag in an 'if/do' tile from the Logic menu, and attach to your loop.
- For 'if' attach a [0] < [0] tile, and drag a 'degrees' variable tile into the left dialog box. Set the right dialog box to [45], so that the tile reads 'If [degrees] < [45]
- For 'do' attach a 'show string' tile and set the dialog box to 'N' so that the LED will display the letter 'N' (for 'North') when the device is facing at an angle of less than 45 degrees.
- In the same 'if/do' tile, click the small blue cog icon to open up a pop up menu with some optional extra tiles. Drag in three 'else/if' tiles and one 'else' tile, so that your full block of code looks like this:
- Using the same methodology, tell the device to read 'E' at angles of less than 135, 'S' at less than 225 degrees and 'W' at less than 315 degrees.
- If the device is pointing at an angle greater than 315 degrees, it must be facing North. We don't need to specify the angles here - if none of the previous commands return as true, the bottom 'else' command will be enough to tell the BBC micro:bit to read 'N' by default if we tell it to.
- Now run your program to test it on the emulator, hit compile to download and transfer it to your BBC micro:bit device to test it out in the real world!
When your students have mastered this project, encourage them to develop it by adding additional degrees of accuracy (NorthWest, NorthEast etc). They could also program the device to display different images on the LED array when the device is facing in each direction - a North star for North, the South pole for South, etc. Let us know how they get on in the comments section below.
Top Comments