The micro:bit has taken technology education by storm and we have been working on a number of ways to help educators make it more applicable for day to day use for students and teachers rather than just “computer science time.”
One application that shows huge potential is figuring out how to use the micro:bit for data logging. In past posts, we have used our SparkFun OpenLog to save data to a microSD card and that is how we recommend you log data when using the SparkFun micro:climate Kit that includes an OpenLog. But, not everybody has an openLog and we know that microSD cards are costly, easy to lose and add steps to the process.
Logging with Just the micro:bit
The challenge then becomes how to datalog using only the micro:bit. We approached the challenge by breaking it down in to a number of sub-challenges that we would solve with some fancy pants coding and free and available tools. Here are the challenges ahead of us…
- Systematically store data samples in a clean and organized way
- An efficient way to start and stop the logging process
- Figure out how to get the data off of the micro:bit
- Be able to take the data and get it into a spreadsheet document
We succeeded in overcoming these challenges and produced the Microsoft MakeCode program below. This program can be used as a structure for data logging in this way and tailored to log whatever data points that you are interested in.
If you are curious about how this program works, have no fear, below we share how we did it.
Systematically Store Data
The first challenge was how to store data on the micro:bit in a manageable and organized way. If you have been around programming for a while, you may have heard of an array before. If you have never heard of an array before, think of it as a way to create a list in a computer program. You can do a lot with arrays once they are created; you can add to them, remove items, read the length of it and so on.
So, for us to store data on the micro:bit, we create an empty array and called it “text list”. We do this in the On Start event block so that every time that you restart your micro:bit you create a fresh new array and erase anything that would be left over from the last time that you ran the program. You will see that we actually repeat this process in the On Button A event block as well. This allows you to create a new list when you start logging something new without having to reset the micro:bit.
We then add a data point to the list in the forever event loop using the add value to end block. In this case, we just used the internal temperature variable from the Input drawer, you can log whatever variable that you choose. This set of blocks essentially adds the temperature to the end of the list every time it is executed.
With that we have successfully stored our data in an organized way. Depending on how often you add data to the array you can store a whole lot of data in a single array.
Starting and Stopping the Logging Process
With the ability to store data completed, we turn our attention to the physical control of how to tell the micro:bit to start and stop logging as well as control when it should “download” the data.
You may have noticed that in the On Start block we set a variable called “log to 0.” This variable is essentially the control state for whether we are logging data or not. If the variable is equal to 0 we are not logging, if the variable is toggled to 1 the micro:bit will log data.
We toggle the variable using the On Button Pressed event blocks. Button A starts logging. So in the event block, you see that we set log variable to 1 and then give a visual feedback of having started logging by showing the check mark icon on the LED array.
To stop logging we use the B button. When the B button is pressed, the variable is toggled back to 0 and we display the X icon on the LED array to again give visual feedback.
The final control interface is pressing both the A and B button. This stops logging by toggling the variable to 0 and then starts the process of going through the array and sending out each data point over the serial port (more on that in a bit). Once the data transfer is complete, we show a smiley face. Awesome, challenge two (controls) solved!
Getting Data Off of the micro:bit
We have used the serial port on the micro:bit in other projects before to data log as well as send commands to the micro:bit. If you haven’t used it before, check out this past blog post that uses it. In a nutshell the serial port on a microcontroller is basically a way that other computers can communicate with the micro:bit or other microcontrollers. In this case we use it to communicate with your laptop or Chromebook using a Chrome app.
On all microcontrollers, the serial port consists of two pins - a Transmit and a Receive pin. Because, we are using a USB cable, we use the Serial Redirect to USB block in the On Start event to send all of the data to the USB port on the computer rather than the two pins we really can’t use for this application.
Now the serial port sits dormant until someone presses A and B buttons at the same time. When those buttons are pressed, the micro:bit runs through a for loop that repeats the code inside of it a certain number of times. In this case, it runs a length which is equal to the length of the array. So, for every data point in the list, we write that item over the serial port and then move to the next one, showing a little animation to give some visual feedback that we are inside of the for loop. Once all of the data points have been sent, the micro:bit will exit out of the for loop and show a smiley face icon.
OK, cool! But, how do you see the data, you may ask….
Collect, visualize and use the data sent
So, the micro:bit is sending all of the data points one at a time over the serial port, but how do you access them on the computer side?
Enter the Beagle Term app for Chrome! Beagle Term is a very basic Serial terminal app for Chrome that allows you to select a serial port on your computer, a baud rate and change other settings and then see what is coming over the serial port in a window. You can find and install Beagle Term here.
Before you start Beagle Term, make sure that you have a micro:bit with the program installed on it and preferably with some data that you have logged by pressing the A button, waiting for a minute or so and then pressing the B button. Connect your micro:bit using a micro USB cable to a USB port and start Beagle Term.
In the startup menu select which serial port you want to use. On a Mac it will look something like my screen shot, a Chromebook will look similar and a PC will have COM##. Usually the micro:bit will be the highest COM# from the list. On a Mac you want /dev/tty.usbmodemXXXX
With the serial port selected, set the baud rate to 115200 and click connect. All other settings are correct for basic use.
You should now see a black terminal window with seemingly nothing really happening. This is where the magic happens! Press the A and B buttons on your micro:bit at the same time. When you do you should start to see data getting listed off. This data is the logged data from your erray!!! Win!
Once the micro:bit is finished sending data over the serial port it will display a smiley face on the micro:bit and the list will stop growing on your monitor. All you need to do now is copy the list and paste it into something like Microsoft Excel or Google Sheets. From there, you can graph it or visualize it however you want. Congratulations, you just logged data without the use of a microSD card!
Making Improvements
This program is a basic demonstration of how you could do something like this. There is a lot of room for improvement on it, so please let us know your thoughts in the comments of this blog post. One thing we found is that if you logged a lot of data and your array is pretty big, it is a bit painful to wait for the for loop. However, if you remove the diamond animation from the for loop it will speed everything up significantly.
micro:bit, programming, microcontroller, computer science