PXT coding tutorial - bitty data logger

micro:bit code

Bitty Data Logger can capture and chart accelerometer, magnetometer and temperature data, streamed live from your micro:bit. The code we need to wrote for the micro:bit is actually very, very simple.

All we need the micro:bit to do is to provide a visual indication of whether or not it has a Bluetooth connection and to activate the Bluetooth accelerometer, magnetometer and temperature services, which are software components responsible for making accelerometer, magnetometer and temperature data available over Bluetooth.

As with all our apps you can simply download and install a hex file which we've prepared for you or you can code it yourself, following the Bitty Software coding tutorial below. Naturally, we recommend you code it yourself!

before you start

You should install the bitty data logger application on your phone so you can use it for testing whilst you develop the micro:bit code. Check the bitty data logger page for details of where to find the app for your type of phone or tablet.

step 1 - PXT

Go to the PXT web site in your web browser or if you're already there, start a new project by clicking on the menu icon and then New Project.

Delete any blocks that are created automatically. We don't need them.

step 2 - make sure your project includes the right packages


PXT groups function blocks together in "packages" and these are listed in a column. A screenshot of a list of PXT packages is shown above. By default it includes a package called "Radio" which allows you to use a very simple wireless capability of the micro:bit. "Radio" is good if you want to broadcast a short string of characters to any / all micro:bits around. It's not Bluetooth and is not what we need for the bitty data logger application. Most importantly, a PXT project cannot include both the Radio package and the Bluetooth package at the same time. Add the Bluetooth package to your project and when prompted to, choose to remove the radio package.

Click on the menu icon, Add Package and select the bluetooth package.

When prompted, opt to remove the radio package

step 3 - display a start-up message

Next, we'll add a start-up message to be displayed on the micro:bit when it starts.

In the Basic tray, click on the "show string" block to add it to your project. Change the text to "DATA".

step 4 - Bluetooth connection tracking

Now we'll use some of the blocks in the Bluetooth package to add connection tracking to our project. What we'll do is set the "connection" variable to 1 whenever a connection is established and display "C" on the micro:bit display or we'll set the variable to 0 and display "D" if a connection gets dropped. We'll also reset some of our other variables to initial values whenever we accept a new connection.

From the Bluetooth tray, click on the "on bluetooth connected" block and then the "on bluetooth disconnected" block to add them to your project. Snap them to the previous block, one after the other. Then add "show string" blocks as shown, inside each of the bluetooth connection event blocks.

step 5 - Bluetooth services

The BBC micro:bit is equipped with extensive Bluetooth capabilities. Think of any given feature of the micro:bit and there's a good chance you can use it over Bluetooth. This is made possible through the micro:bit having a custom Bluetooth "profile" designed for it. The profile includes a variety of "services", each of which is responsible for Bluetooth use of a given aspect of the micro:bit. If you'd like a better understanding of the terms and concepts relating to Bluetooth, read this article from the Bluetooth SIG's blog.

The Bluetooth services we want to use need to be explicitly added to our code so that when the micro:bit starts up, the services are activated. To be able to capture any of the data types supported by Bitty Data Logger, we need the accelerometer, magnetometer and temperature services. So select each of them from the PXT Bluetooth package and add them to your code now.

Full details of the micro:bit's Bluetooth capabilities are documented at the Lancaster University web site.

step 6 - testing

That's it! You've created a basic micro:bit application which can send accelerometer, magnetometer and/or temperature data to the bitty data logger application. This was a fairly easy tutorial, so if you have ideas for other things your micro:bit code could do, have a go at making some changes. Could the buttons do something useful, for example?

You should now test your code carefully and fix any problems you find. Compile the PXT project and install the hex file. You'll need to forget any pairing details on your smartphone and go through the pairing process again (unfortunately!) but once you've done that you should be able to test with the bitty data logger application. Good luck and enjoy having fun with bitty data logger!

if you get stuck

Take a close look at the solution here. Look for anywhere where your project is different. And don't forget, make sure you've paired your phone or tablet and micro:bit properly.