Create Compass Using BBC Micro - Bit

In this article, I will show you how we can use our BBC Micro: Bit as a compass. We will be able to see directions based on the angle we read from the Micro: bit. So let us see how we can do this.

Basically, we're going to read the value from the sensor that is built within the Micro: bit. This is called magnetometer, which is actually used to detect the earth's magnetic field along three perpendicular axes, X, Y and Z. As we can see below, there is a sensor which is going to give us the readings and based on those reading we are going to decide the directions.

 
Tools You Need
  1. Micro: Bit (1 pcs)
  2. USB Cable (1 pcs)
  3. Battery AAA 1.5 v(2 pcs)
  4. Battery Box (1 pcs)
So this is very easy and it won't take long to read the values from the Micro: bit. We're going to work on the basis of the following image. Based on this we're going to set our Micro: bit to decide on the directions. We can see the trigger zone for every direction and we're going to set the values in the Micro: bit so that when it detects these angles it is going to decide the directions. We can see the trigger zones for:
  1. North 20 to 340 degrees.
  2. West 250 to 290 degrees.
  3. East 70 to 110 degrees.
  4. South 160 to 200  degrees.
 
 
Now, let us code the Micro: bit for this. So go to makecode.microbit.org and create a new project and choose a forever block there. Now, go to the Logic block and choose if then else and then other logic blocks as shown.
 
 
 
Now, we need to take the compass reading so that we can place the compass reading in the logic block, and we can make the decision for the direction. And for this you need to go to input and choose compassheading(). Actually, copassheading() is a variable which reads the values from the compass or we can use the magnetometer which is built in the Micro: bit so that we can capture these values and use them for our compass.
 
 
 
So, now follow the steps and let's build our compass.

First drag "if", "then", "else" inside the Forever block and place logical AND-OR  block inside the if then else block.
 
 
 
You need to place other logical blocks like this:
 
 
 
 Now place the compassheading()  inside the values block; in other words,  replace the 0 to 0 block with compassheading().
 
 
 
The same goes for another side of the AND  part. So let us first understand what and why we are doing this. Compassheading() returns integer value so we're comparing using logical operators here and based on our comparison we will make decisions. Now we just need to put in the values, and if it is true then it will display the directions, otherwise it does nothing. And we are doing this according to the following truth table.
 
 
So, if we will use AND  then in this case the values of LHS of the AND is A and  RHS value of the AND is B. This is the complete part.
So, here if the compassheading() value on both sides is TRUE then it will only return TRUE, then the appropriate action in the THEN block will be performed. 
 
 
 
Then let us code for NORTH first. You will find Show String block in Basic just drag and drop it inside THEN.
 
 
 
In this if compassheading() value will be greater or equal to 340  or if compassheading() value will be lesser or equal to 20 according to the trigger zones  it will display N.
 
Now, you need to duplicate the first block and provide the values for the other directions also. Your code should look like this.
 
 
 
That's all.You're ready to go.Get the code HERE  And if you want to do something using this then you build one physical compass using the servo that will read the values and drive the servo according to the values. Why not try it? For te basics, you can refer  to these articles.
You can see a demo HERE.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now