Microsoft MakeCode is a helpful tool to learn or teach beginners of all ages how to code, with the added bonus of allowing you to use software to control hardware devices.
This tutorial will teach you how to use the MakeCode platform to write a simple program that sends live data from the BBC micro:bit to Microsoft Excel using the Data Streamer add-in.
The example program we use in this tutorial prints the micro:bit accelerometer data into Excel. You can modify and adapt the basic program to work with other micro:bit sensors or external sensors.
Using the MakeCode Blocks to Print Data to SerialThe serial port on your computer is how Excel's Data Streamer add-in gathers data from external devices. This section shows you how to use the MakeCode blocks to print data to the serial port.
1. Plug in your micro:bit and go to the MakeCode website.
2. Select the “micro:bit” option and create a new file. This will take you to the MakeCode block interface.
3. Download the HackingSTEM Data Streamer add-in.
Go to Extensions, located at the bottom of the code blocks, and search for “Data Streamer”. Click on the HackingSTEM Data Streamer add-in to enable it.
4. Back in the Block section of MakeCode, locate and open the yellow/orange “Data Streamer” blocks in the top section.
5. Set the Baud Rate by dragging the “Set Baud Rate” block into “On Start”. Use the default setting (9600).
Note: Baud Rate is the speed at which the computer and the micro:bit communicate.
6. Next, print data to serial with the “write number array” block. Drag this into the “forever” block.
7. In the “array of” slots, insert the data you want to print to serial. For this example, we are printing the micro:bit accelerometer x, y, and z values.
Accelerometer blocks are in the “Input” section. Remember to change the selected variable from “x” to y and z accordingly.
8. Print a new line after the array to denote the end of the data packet in Data Streamer. Do this by dragging the “write line” block from the Data Streamer section.
9. Optional: Add a pause to more easily see the data as it is printed.
Downloading Code onto the micro:bitOnce you've got the MakeCode block program, you are ready to upload it to your micro:bit! There are two ways to do this which are outlined below.
Note: Option 1 is faster as it involves fewer steps. However, some computers may not permit you to download files directly onto the micro:bit, so you may need to use Option 2.
Option 1
Download and save the code file directly onto the micro:bit drive.
Option 2
Download and save the code file into a folder on your computer.
Open File Explorer, navigate to the folder in which you saved the code file, then drag the code file onto the micro:bit drive.
Using Data StreamerYou are ready to use Data Streamer! Open Excel and navigate to the Data Streamer tab.
1. Click “connect your device” to select the micro:bit USB device.
Note: if you see more than one device listed, unplug your micro:bit and observe which one disappears, then reconnect and select that USB device.
2. Click “start data” in the Data Streamer tab and navigate to the Data In sheet to see micro:bit accelerometer data printed in three separate columns.
3. You can plot the data to more easily observe changes over time and do data analysis with Excel’s built-in functions.
Visualizing the micro:bit DataA helpful method for visualizing data is to use a Scatter Plot.
To add a scatter plot, navigate to the “Insert” tab in Excel and select the scatter plot icon as shown in the image above.
Next, add some data to your scatter plot! Right click on the chart and choose “Select Data”. This will open a new window in Excel.
To add a new set of data, choose the “Add” option. This will open a new window titled “Edit Series”.
Add a title for your data series to keep track of what you are plotting.
Next, select the data you want to plot in the “Series Y-values section.” Click the up arrow which will take you to a new mini-window, then navigate to the Data In sheet. Left click and drag the mouse down to select the entire first column of data. If done correctly, you will see the column values printed in the “Edit Series” window.
Note: the x-axis, or horizontal axis, is a plot of time. You can leave the x-axis blank or select the timestamp in the Data In page.
Click “Ok” to save your selection and take you back to the Scatter Plot.
You can now see your first set of live data!
Excel will autoformat parts of the chart for you, but you can change various chart elements in the “Chart Tools -> Design” tab. It is highly recommended to include chart axes labels and a chart title.
The horizontal axis of time is plotted in milliseconds, or ms. The vertical axis label will depend on your data.
If you are plotting the micro:bit accelerometer values, the data is given in units of “milli-g’s”, where “g” is the acceleration due to gravity (9.8 m/s^2).
To add more data to the same chart, right click on the chart and again choose “Select Data”.
Go Forth and Play!You can now visualize accelerometer data from the micro:bit in Excel! Use the live data and/or your scatter plot to determine how the accelerometer functions, use it for experiments, or deploy it for gesture-controlled projects.
More to Explore!Looking for project ideas? The Microsoft HackingSTEM team has tons of open-source projects and curriculum! Below are some of the HackingSTEM projects that use the micro:bit.
Check out the full HackingSTEM activity library here.
Please reach out to us if you have any questions, comments, or ideas! We also love seeing what folks build with our projects, so please share your creations and tag us on social media!
Comments