Support

How can we help you today?

Outputing serial data from the micro:bit to a computer

Modified on: Mon, 7 Jan, 2019 at 9:49 AM

When your micro:bit is connected to a computer with a USB cable it is able to send and receive data using 'serial' communication. This is useful for debugging programs as well as data logging, or using your micro:bit as a controller for your computer.

Coding serial on the micro:bit

You can send and receive data over serial on the micro:bit using both Python and Makecode for micro:bit.

Python

In Python, the 'print' statement sends a string from the micro:bit to the connected computer over serial. You can read serial messages from the computer using the UART module.

Using serial on the computer

Once you have programmed your micro:bit you will need a way of sending and receiving serial data on the connected computer using one of these options:

In the Chrome browser with WebUSB

Using the Windows 10 App

Using the Mu Python Editor program

Using a computer terminal or serial program.

WebUSB

WebUSB allows you to read serial data in your web browser using the Python or JavaScript Blocks editors. WebUSB is still in beta; we're currently looking for help testing it, and we'd love for you to get involved.

Both editors allow you to stream and plot serial data in-browser. The Python editor also features REPL (Read-Eval-Print-Loop), allowing you to write & test code on your micro:bit in real-time.

Using the Windows 10 App

The MakeCode app for Windows 10 allows you to read and plot serial data from the micro:bit, allowing you to accurately log data from the device's sensors.

Connect your micro:bit to the computer with a USB cable then open the MakeCode app. Write a program using the serial blocks and flash it to the micro:bit, then click Show console Device to view a plot of serial data. Clicking the blue button in the right corner will generate a CSV file, which allows you to view and edit the serial data in Excel.

Using Mu

The Mu editor for Python allows you to read and plot serial input, and also features REPL (Read-Eval-Print-Loop), allowing you to write & test code on your micro:bit in real-time. Click the REPL button to view raw output from the device, or click Plotter to see a live plot of data from the device

Using a computer terminal

Windows

When installing the Windows mbed driver, you should make sure that the micro:bit is plugged into the USB port first, before trying to install the driver - some users have experienced 'unknown install errors' of this driver, and it seems to be related to not having the micro:bit actually plugged in at the time you do the install. You will also need full admin rights on your PC to install the driver.

For example in Tera Term:

Plug in micro:bit and open Tera Term

Select Serial as the port

Choose Setup > Serial port from the setting menu and configure these settings:

Baud rate = 115200

Data = 8 bits

Parity = none

Stop = 1 bit

You should now see serial data output in the console.

Windows users will require the In Windows 10, Windows Update will fetch the driver automatically.

For earlier versions, or if you have used mBed before and upgraded, you may need to re-install the driver.

Mac OS

Plug in the micro:bit and open a new terminal window.

Type ls /dev/cu.* to get a list of connected serial devices; one of them will look like /dev/cu.usbmodem1422 (the exact number depends on your computer).

Type screen /dev/cu.usbmodem1422 115200, replacing the 'usbmodem' number with the number you found in the previous step. This will open the micro:bit's serial output and show all messages received from the device.

To exit, press Ctrl-A then Ctrl-D.

Linux

Plug in the micro:bit and open a new terminal window.

Typing dmesg | tail will shows you which /dev/ node the micro:bit was assigned (e.g. /dev/ttyUSB0).

Type screen /dev/ttyUSB0 115200, replacing the number with the number you found in the previous step. You may need to install the screen program if you don't already have it.

To exit, press Ctrl-A then Ctrl-D.

Further Information

One way of handling serial is using the pyserial module for Python. Note that this module is not developed or maintained by the Micro:bit Educational Foundation, so please see the pyserial documentation for support and setup instructions.