This month, we have learned a lot about the micro:bit. So, in this month’s project, we will learn how to make a compass using a micro:bit!
The hardware in this project is incredibly easy to use, thanks to the simplicity of the micro:bit and the fact that all the peripherals needed in this project are embedded on the micro:bit. Of course, an external power supply is still needed, and this is provided by the external power pack that comes with Digi-Key’s micro:bit kit.
The first line in our program gets the current compass heading and then stores it into a variable called Heading. However, one issue arose when designing this project, which was that the compass did not point north, but instead, it always pointed to the right of the micro:bit, regardless of the user’s direction. As it turns out, my compass had been calibrated incorrectly, either at the factory or during the calibration stage, and the compass would only work if the micro:bit was held above the user with the display pointing toward the ground. A quick fix, though, is to subtract the current heading from 360, which flips the reading and adjusts it correctly.
The next step in our program is a delay of (200ms), which is used to freeze the LED display before updating it with the new heading. If this delay was not included, then the display would flicker, which can be unpleasant to look at.
With the delay completed, the next step involves clearing all the LEDs on the micro:bit. You can do this by using graphical piece that allows you to choose which LEDs you want on or off, but this takes a while to execute. Instead, I opted for a programmer’s method, which involves two for loops. The main for loop executes five times, while a for loop inside that for loop executes five times for every single iteration of the first for loop. On each iteration of the second for loop, we “unplot” an LED on the display, whose coordinates are defined by the variable I and j (the for loop counters). While this may seem complicated, the result is very trivial; we go through each LED on the display and turn it off! This is how we clear the display.
The last section in this program involves checking the heading and then plotting the arrow onto the display so that the arrow points toward north. To determine the direction that the arrow should point, a series of if statements are performed to see if the current heading lies within a specific range of values.
With the code made, it now needs to be compiled and flashed to the micro:bit. In the old days of microcontroller programming, this would require linking to libraries and using expensive programmers, only to find that the code does not work, or that some internal configuration bit has been incorrectly fused. However, the micro:bit does away with all of these issues!
The first step is to get our hex file, which contains the instructions needed to make the micro:bit work. To do this, click the “Download” button in the MakeCode IDE.
When the hex file downloads, plug the micro:bit into the computer via the micro USB lead, and your system should detect the micro:bit as a removable flash drive. Open the micro:bit flash drive and then drag the hex file that you downloaded earlier into the micro:bit folder.
Since our project uses the compass peripheral, the micro:bit will ask you to draw a circle when the program is first loaded. This is a very easy step and only requires that you start by keeping the board horizontal and then roll the micro:bit forward and around in a circle. You should see an LED dot move around and begin to fill the edges, whose position is controlled by the angle of the micro:bit (see the video for a visual cue).
With the micro:bit programmed and calibrated, it's time to test out our compass. Before disconnecting the micro:bit from the PC, make sure to remove the device safely. For example, this can be done on a Windows machine by right clicking the drive and then clicking “Eject”.
With the micro:bit disconnected, the battery back needs to be inserted into the micro:bit power input, and then the power supply should be turned on.
Flip the micro:bit so the LED display is facing you, and the micro:bit should now be pointing an arrow toward north!
Despite the programming technique, as well as the hardware, being simple, the end result is an electronic compass that updates a display to point toward north, which would typically be very hard to do using other microcontrollers, including PICs, AVRs, and even Arduinos. Not only is the micro:bit a useful prototyping tool, but it is also incredibly compact and portable, being smaller than a credit card, which makes the micro:bit one mighty, powerful controller.