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.

  

Makecode for micro:bit


    

You can read or write serial data in JavaScript Blocks using the advanced serial blocks.


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 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.


Note that WebUSB is currently only supported by Chrome. 


Discover how to use WebUSB in this knowledge base article.


Using MakeCode

MakeCode 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 makecode.microbit.org in a Chrome-based browser. 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.

 

Read this guide to learn more.


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


Read this guide to learn more


Using a computer terminal

Windows

You can use any terminal emulator program to send and receive data to and from the micro:bit over the USB serial lead.


For example in Tera Term:



  1. Plug in micro:bit and open Tera Term
  2. Select Serial as the port
  3. 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.


Mac OS

  1. Plug in the micro:bit and open a new terminal window. 
  2. 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).
  3. 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. 
  4. To exit, press Ctrl-A then Ctrl-D.


Linux

  1. Plug in the micro:bit and open a new terminal window. 
  2. Typing dmesg | tail will shows you which /dev/ node the micro:bit was assigned (e.g. /dev/ttyACM0). 
  3. Type sudo screen /dev/tty0 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. 
  4. To exit, press Ctrl-A then Ctrl-D.


Further Information



Keywords for search: USB, serial port, Windows, REPL, Mu