The best tools to make your project dreams come true

Login or Signup

8/16/2018 | By Maker.io Staff

How to Make a micro:bit Compass

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!

Things You Will Need

Hardware

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.

Software

While the micro:bit can be coded using scripting languages such as MicroPython and Javascript, we will instead take advantage of the MakeCode jigsaw system. This coding technique may have its limitations (since only predefined pieces can be used), but it also allows for fast prototyping and near bugless code. The code in this project can be found here.

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.

How to Make a micro:bit Compass

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.

How to Make a micro:bit Compass

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.

How to Make a micro:bit Compass

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.

  • Point north if our heading lies between 0° and 45°.
  • Point northeast if our heading lies between 45° and 90°.
  • Point east if our heading lies between 90° and 135°.
  • Point southeast if our heading lies between 135° and 180°.
  • Point south if our heading lies between 180° and 225°.
  • Point southwest if our heading lies between 225° and 270°.
  • Point west if our heading lies between 270° and 315°.
  • Point northwest if our heading lies between 315° and 0°.

How to Make a micro:bit Compass

How to Make a micro:bit Compass

How to Make a micro:bit Compass

Compile and Program

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.

How to Make a micro:bit Compass

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.

How to Make a micro:bit Compass

How to Make a micro:bit Compass

Calibration

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).

Testing It Out

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”.

How to Make a micro:bit Compass

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.

How to Make a micro:bit Compass

Flip the micro:bit so the LED display is facing you, and the micro:bit should now be pointing an arrow toward north!

Conclusion

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.