How Do I Track My Luggage with micro:bit

With just a micro:bit and an Android smartphone, you can build a simple luggage tracker: LugTracker. It is cheap, simple and reliable.

LugTracker with micro:bit

It will turn the Bluetooth advertising off when in flight and turning it back on once the plane comes to a full stop. It turns itself back on once the micro:bit stands still for 12 minutes. With the Bluetooth proximity feature, you can check your luggage from your Android smartphone when it is headed your way at the baggage carousel.

Part List

  • 1 x BBC micro:bit
  • 1 x Battery Holder
  • 2 x AAA Batteries

You will also need the following equipment:

  • A computer with a USB port and internet access.
  • Android smartphone with the custom app installed to scan the unique Eddystone Bluetooth URL.

Step by Step Guide

Go to https://makecode.microbit.org, click “Projects” and “New Project”.

Pick a name for your project and save it. Let’s practice using the block system to build some code.

Step 1

First we need to create a “forever” loop. This is the block that the rest of our code will sit inside. The “forever” loop runs all the blocks inside it starting from the top and working its way to the bottom. Once it gets to the bottom block it starts again at the top and this process will continue forever.

Step 2

We need to create three variables to capture the information from the accelerometer. The micro:bit measures movement along three axes:

  • X — tilting from left to right.
  • Y — tilting forwards and backwards.
  • Z — moving up and down.

To do this we will first create a variable, this is a location where we can store data in the BBC micro:bit’s memory. We will then pull data from the accelerometer and store it in that variable.

Let’s create three variables to track the movement from the accelerometer, we will name it “Xmovement”, “Ymovement” and “Zmovement”. Select “Variables” category and click “Make a Variable”

Create variable to store the value of the movement from X, Y and Z direction.

Now go into the “Variables” category again and select “set items to” and drag it to the workspace. Drag the “set items to” block to “forever” block that we created earlier. By default, the variable name is “item”. We can change this to something that makes more sense for our project. We will be using this variable to track movement in the X direction so let’s call it “Xmovement”.

Select “Input” category and take the “acceleration(mg)” block and attach it to your “set items to” block. Make sure the “acceleration(mg)” block is set to the X axis. These two blocks combined will set the variable “Xmovement” to the X value of the accelerometer.

Do the same logic for “Ymovement” and “Zmovement” variables.

Now the block would be like this:

Step 3

Let’s put that variable to use with an “If” statement. An “If” statement will perform a process but only “If” certain conditions are met. They are great as they allow us to create programs that react to changes.

Select “Logic” category and drag the “if then” block to the workspace and put it under the last block.

We need to create new variables “Xold”, “Yold” and “Zold” to hold the existing data from the accelerometer and compare with the new value to get the latest update. Also create a variable “movement” to indicate if there is movement (true) or not (false).

Select the “Logic” category again and drag the “condition check” block and attach to the “if then” block. Attach the “Xmovement” and “Xold” variables to the “condition check” block. We need to set the condition check logic to “not equal” or “!=”.

It means if the value of the existing accelerometer data is not equal with the new value, then there is a movement. Here is the updated block looks like:

We need to set a threshold so the program will not always response to the movement. A little movement will not trigger the program. We don’t want any little movement detected will trigger the action. Only certain movement that passes the threshold will trigger the action.

Create the variables “Xthreshold”, “Ythreshold” and “Zthreshold” for X-axis, Y-axis and Z-axis and set the initial value to 300. You can play around with this value to get the better responsiveness.

Create a condition that checks if the variable “Xmovement” goes under Xmovement-Xthreshold OR “Xmovement” goes under Xmovement+Xthreshold. If the condition is met, set the value of variable “Xold” to variable “Xmovement”, set the variable “movement” to “true” and shows the LED status.

Do the same logic for “Ymovement” and “Zmovement” variables. Here is the updated block looks like (yeah, I know it is too small to see, but don’t worry, you can find the complete source code at the end of this blog):

We are done with the block logic to check the movement within the defined thresholds for X-axis, Y-axis and Z-axis.

Step 4

Before we going into the logic, we need to enable the Bluetooth feature. We will be using the Bluetooth Eddystone URL advertising to communicate with Android smartphone, so the mobile app in the Android will know if our micro:bit that we put in our luggage is nearby. The mobile app creation is beyond of this document, so we will not go through it. It is basically a simple app that always detect the Eddystone URL advertised by the micro:bit, if there is, we assume our luggage is nearby (we need to put the micro:bit in the luggage).

Go back to our block editor and select “Add Package” and select “bluetooth”. You will get the window saying the existing radio package is incompatible with bluetooth. Click “Remove package(s) and add bluetooth”.

Once you have added the bluetooth package, you will see the “Bluetooth” category.

Bluetooth Category appears

Step 5

Now we need to check if there is movement (variable “movement” is “true”). Select “Logic” category and drag the “if then else” block to the workspace and put it under the last block inside the “forever” block. Attach the “movement” variable to the “if then else” block.

Create three new variables: “now”, “now1”, “timet” and “advertise”:

  • Variable “now” is to hold the value of the running time when the movement is detected (variable “movement” is “true”).
  • Variable “now1” is to hold the value of the running time when the movement is not detected (variable “movement” is “false”).
  • Variable “timet” is to set the quite period. I am setting for 12 minutes. If there is no movement (micro:bit stands still) for more than 12 minutes, advertise the Eddystone Bluetooth URL.
  • If we already advertise the Eddystone Bluetooth URL, the value of variable “advertise” is “true”, otherwise is “false”.

If the variable “movement” is “true” — there is continues movement, do the following:

  • Set the “movement” variable to “false”
  • Get the current running time in the variable “now1”
  • Set the “advertise” variable to “false”
  • Stop the bluetooth advertising
  • Pause for 500 ms and clear the LED screen

If the variable “movement” is “false”, do the following:

  • Get the current running time in the variable “now”
  • Check if the time since the last movement detected is greater than 12 minutes
  • If there is no movement then advertise the Eddystone Bluetooth URL and set the variable “advertise” to “true”

Here is the updated block looks like:

Step 6

The last step is we need to set the initial values on the “on start” block:

Set the init values

Complete Source Code

You can find the complete code in my Git link below:

Or see the code online on the Microsoft MakeCode website below:

Thank you for reading my tutorial and let me know if you have comments or questions. As always Happy coding!! and until next time :)