[This is a reblog of a post I wrote at scratchmathland.com]
The BBC micro:bit contains various sensors for measuring temperature, light intensity, direction, movement, and so on. These sensors might be valuable in a project in which measurement data collected by one micro:bit (the sensor) can be communicated to another micro:bit (the receiver) and displayed on the LEDs. However, there are methods by which this sensor data can be collected and saved in a data file.
The micro:bit does not have the capability to open and write directly to a file but it does have the capability to write to your computer’s serial port (via the USB connection – remember – USB stands for Universal Serial Bus). A terminal emulation program can be used to display the data in real time. Additionally, that data can be easily saved to a text file.
This post describes this process with an example project that I coded and tested on a Mac running macOS High Sierra (ver 10.13.6) and on a Chromebook running ChromeOS (ver 71.0.3578.127).
Click to jump to a section:
- Programming the sensor micro:bit
- Programming the receiver micro:bit
- Capturing the serial data on your computer…
- …Using macOS
- …Using a Chromebook
Programming the sensor micro:bit
This code simply sends data through the radio so that another micro:bit can receive the data. The sensor will measure and transmit six readings: temperature, light level, compass heading, acceleration strength, rotation pitch, and rotation roll. You can access this code here: https://makecode.microbit.org/_4ds5jqKbWAWA This project sends sensor data to the receiver about once per second via the radio; depending on your experiment, you might need to adjust the “pause (ms)” blocks to collect sensor readings more or less frequently.
Because the “radio send string” block has a limit of 19 characters, I have coded the sensor information to be sent in two separate strings. In order to keep the data organized, the letter “A” or the letter “B” is the first character of the string transmitted by the radio. This will become the method by which the receiver micro:bit can correctly identify what information it is receiving.
Note that the “join” code within the “radio send string” in the code below could easily be modified to create comma separated values (CSV) so that the output from the receiver micro:bit could be quickly imported into a spreadsheet and analyzed / graphed.
Programming the receiver micro:bit
This code takes action when it receives data via the radio. The variable CheckTag looks at the first character in the string received and takes one of three actions as indicated in the code below. If CheckTag is A, then Data1 will be assigned the first set of three sensor readings (see code above). If CheckTag is B, then Data2 will be assigned the second set of three sensor readings (see code above). If CheckTag is C, then Data1 and Data2 will be joined and sent to the serial port. Some extra code was included (button A press) so that a new header could be sent to the serial port in case the data is being view in real time and needs to be identified. Access to this code is available here: https://makecode.microbit.org/_AP7LLd4wU1U3
Capturing the serial data on your computer…
How does this project work using the serial port? The “serial write line” block (or any of the other serial write blocks) sends text data to the serial port via the USB connector on your micro:bit. Your computer can read this incoming data by using a terminal program. These kinds of applications have been around for decades and were commonly used in communication between two computers on a network starting in the 1970s. Perhaps the most common terminal was DEC VT100 (pictured). Today, there are apps you can download to your computer that “emulate” these terminals. In the past, they were actual devices that looked much like an old style desktop computer but they could not perform actual computing tasks on their own (sometimes they were called dumb terminals).
For this project, I used used a free macOS application called SerialTools as my terminal instead of using a built-in terminal emulator. The serial reference information on the makecode site suggests opening the built-in Terminal program on a Mac (or analogous one on a PC or in Linux) and then using a command like:
sudo screen /dev/cu.usbmodem1422 115200. There are command line suggestions for Linux and Windows 10 as well… but I found that using a terminal emulator app, like SerialTools, worked better.
Once both of your micro:bits have been flashed with the HEX files, and both are running, make sure the receiver micro:bit is plugged into your computer via the USB connector.
Then, in my case, I started SerialTools on my Mac. Look for something like “usbmodem1422” in the serial port list (the ‘1422’ might be a different number on your computer):
Then, click on the “Connect” button to start displaying the data coming into the serial port from the micro:bit:
To observe how the sensor data changes in real time, pick up the sensor micro:bit and move it around; bring it close to a bright light source, or place your thumb onto the CPU chip to see the temperature rise.
Once you have the data you want, highlight that data in the terminal window and right click to copy. Then, open TextEditor on a Mac and paste in the values:
As I noted above, I separated the sensor values in this project with a space character for but you can easily change that to a comma in the sensor code so that you could produce a CSV file of the sensor data. CSV files are easily imported into your favourite spreadsheet for further analysis or graphing.
…Using a Chromebook
Similar to adding the SerialTools app on macOS above, on your Chromebook you also need to add an app. Head over to the Chrome Web Store and look for Beagle Term (there are other ChromeOS terminal emulators but I used this one successfully). Once the app has been added to Chrome, make sure both of your micro:bits have been flashed with the HEX files, and both are running. Make sure the receiver micro:bit is plugged into your computer via the USB connector.
Now, start up Beagle Term. You will be asked to configure but just go with the defaults:
(Note: if you do not see a device listed beside “port” then the microbit either isn’t plugged in or the Chromebook has not detected it yet. Notice also that on the Chromebooks, my micro:bit connection to the serial port is identified as
/dev/ttyACM0 which is very different to the device name my macOS gave:
usbmodem1422– just interesting to note.)
Now, click on “Connect.”
Once connected, the app will display the data being sent by the serial write line blocks from the receiving micro:bit. Once all of the measurements have been collected, you will need to copy and paste all of the data from the terminal window to a Google Doc or Sheet:
- Double click at the start of the text that you want to select in the Terminal window.
- Scroll the window to the end of the text you want select.
- Shift + click the end of your selection.
- All text between your first click and your last Shift + click is now selected.
- Ctrl + C to copy your selection.
- Open the Docs file where you wish to paste the data.
- Click Ctrl + V to paste your selection.
As noted above, I separated the sensor values in this project with a space character for but you can easily change that to a comma in the sensor code so that you can ultimately produce a CSV file of the sensor data. CSV files are easily imported into your favourite spreadsheet for further analysis or graphing.
Let me know in the comments below how you have captured data from the micro:bit in your projects!
Please Leave a Reply