The best tools to make your project dreams come true

Login or Signup

How to Use the Serial Port for I/O on Your micro:bit

11/27/2018 | By Staff

The micro:bit is a fantastic learning development platform that contains a surprising number of features. One of these features, however, is especially useful for more advanced users. It allows communication between the micro:bit and a computer. In this how-to article, we will learn how to use the micro:bit serial port!

Why Use the Serial Port?

One of the biggest problems with development boards such as the micro:bit and the Arduino is that they lack debugging capabilities. Debugging tools often allow you to track changes in variables and see where the program is currently running (i.e., which piece of code is currently being executed). However, the micro:bit and Arduino both have a serial port that can be used for many things including data transfers, user input, control, and even basic debugging!

So, let’s see some simple examples where the serial port can be useful in a project!

    1. A computer can use a micro:bit as an I/O interface, and a PC program can have access to the accelerometer, magnetic sensor, light sensor, buttons, and even the Bluetooth system.
    2. A computer can be used as a debugger where a micro:bit routinely sends the values of key variables to the computer, which can then be displayed in a console window.
    3. A computer can be used to send specific files and commands to the micro:bit for it to perform some specialized functions, such as IC programming, IC testing, motor control, and CNC control.

Some Pre-Requests

Before we can use the micro:bit serial port, we need to install a serial terminal program, and we also need to install the USB serial driver. In this tutorial, we will be using RealTerm. To get the driver, start by following the links below, then follow the installation instructions.

RealTerm download

micro:bit USB serial driver

Advanced Work

The serial port feature is readily available in Block form (see makecode), and all the serial port functions can be found under the “Advanced” menu. More functions are available under the “…more” tab, but these functions will not be covered and are only useful for advanced users. For now, start by dragging the block labeled as “serial redirect to USB” and connecting it to the “on start” block. This block makes the USB connection between the PC and the micro:bit function as a serial port. Otherwise, the micro:bit would use the RX/TX pins, which are not accessible through USB.

How to Use the Serial Port on Your micro:bit

With the serial port redirected to the USB connection, we can now send and receive data via the serial port. The most rudimentary program for testing the serial port is a ping device, which sends back exactly what was sent to it. In our RealTerm program, if we send the string “Hello”, then the micro:bit will respond with the exact same message. To do this, we need to use the “serial on data received” event, which only executes upon detecting a message. In this block, we will also add the blocks “set item to”, “serial read string”, and “serial write string”, as seen in the arrangement below.

How to Use the Serial Port on Your micro:bit

When a newline is detected (hex 0x0A 0x0D), the micro:bit will store the string sent into the variable “item”, then immediately send the contents of “item” back over the serial port. To use RealTerm to do this, you need to open the serial port that the micro:bit is on, set the baud rate to 115200, and ensure that EOL has +CR and +LF ticked. These two boxes are carriage return and line feed characters, which get automatically added to the end of your string. Without these, the micro:bit will not know when the string has finished.

How to Use the Serial Port on Your micro:bit

Of course, you don’t need to use the serial data received event; you can use blocking functions to read the serial port until the end of line characters have been detected (0x0D and 0x0A). To do this, you can use the block “serial read line” in the forever loop, and this block will cause the micro:bit to sit and wait until it detects the end-of-line characters.

How to Use the Serial Port on Your micro:bit

Strings aren’t the only data types that you can use the serial port for! You can also send numbers, too, which include variables. This can be helpful when debugging. Below, you can see how the serial port can be used to watch the value of a variable in real time! This uses a loop to toggle an LED, but it also outputs the value of “index” so that I can make sure it increments properly!

How to Use the Serial Port on Your micro:bit