Article Index

The serial port is one of the oldest of ways of connecting devices together but it is still very, very useful. The micro:bit has a single serial interface but it can be directed to use any of the GPIO piins as Rx and Tx. 

Now On Sale!

You can now buy a print edition of micro:bit IoT in C.

You can buy it from:

USA and World


 The full contents can be seen below. 

Chapter List

  1. Getting Started With C/C++
    Anyone who wants to use the BBC micro:bit to its full potential as an IoT device needs to look outside the coding environments provided by its own website. As an mbed device, however, the micro:bit  is capable of being programmed in C/C++. Here we look at how to use the mbed online compiler for a simple demo program.

  2. Offline C/C++ Development  
    We have already discovered how to use the online editor to create a C/C++ program. Now we are going to move to the desktop with an offline approach. This has the advantage that we can use any tools we care to select and no Internet connection is needed.
  3. First Steps With The GPIO 
    The most basic task when working with the micro:bit is controlling the I/O lines. This isn't difficult if you use the framework provided but there some subtle points to watch out for. This chapter looks a the basics of using the GPIO.

  4. Working Directly With The Hardware - Memory Mapping. 
    The framework makes working with the GPIO and other devices as easy as it can be but there are many layers of software to go through before you get to the hardware. Writing directly to the hardware can make things up to ten times faster and give you access to things that their framework doesn't. It is also an educational experience to deal with the raw hardware directly.

  5. Pulse Width Modulation, Servos And More
    In this chapter we take a close look at pulse width modulation PWM including, sound, driving LEDs and servos.

  6. I2C
    The I2C bus is one of the most useful ways of connecting moderately sophisticated sensors and peripherals to the any processor. The only problem is that it can seem like a nightmare confusion of hardware, low level interaction and high level software. There are few general introductions to the subject because at first sight every I2C device is different, but here we present one.

  7. I2C Temperature Measurement
    Using I2C devices is fairly easy once you have successfully used one - and hence know what information you need and what to look for in a working system. In this chapter we use the HTU21D temperature and humidity sensor as a case study of I2C in action. It also happens to be a useful sensor.

  8. A Custom Protocol - The DHT11/22

  9. The DS18B20 - One Wire Bus

  10. The SPI Bus
    The SPI bus can be something of a problem because it doesn't have a well defined standard that every device conforms to. Even so if you only want to work with one specific device it is usually easy to find a configuration that works - as long as you understand what the possibilities are. 

  11. SPI MCP3008/4 AtoD   
    The SPI bus can be difficult to make work at first but once you know what to look for about how the slave claims to work it gets easier. To demonstrate how its done let's add eight channels of 12 bit AtoD using the MCP3008.

  12. Serial Connections
    The serial port is one of the oldest of ways of connecting devices together but it is still very, very useful. The micro:bit has a single serial interface but it can be directed to use any of the GPIO piins as Rx and Tx. 

  13. WiFi 
    The micro:bit has a radio that works in Bluetooth LE and point-to-point ad-hoc mode, but at the moment it lacks WiFi connectivity. The solution is to use the low cost ESP8266 to make the connection via the micro:bit's serial port. 

  14. LED Display 
    The micro:bit's LED display may only be 5x5 but it is very versatile. If you want to make use of it directly then you are going to have to master some lower level functions.


The software that powers the micro:bit does a lot to make the single tasking mbed environment that it is based on more like a multitasking operating system. The facilities it provides for working with the serial interface are extensive and it is important to know how it all works because it goes well beyond the raw_serial facilities that it is based on. 

Serial Protocol

The serial protocol is very simple - it has to be because it was invented in the days when it was generated using electromechanical components, motors and the like. It was invented to make early teletype machine work and hence you will often find abbreviations such as TTY used in connection with it. As the electronic device used to work with serial is called a Universal Asynchronous Receiver/Transmitter the term UART is also often used. 

The earliest standards are V24 and RS232 however notice that early serial communications worked between plus and minus 24V and later 12V. Today serial communications works at logic or TTL levels 0 to 5V or 0 to 3.3V. This voltage difference is a problem we will return to later. What matters is that irrespective of the voltage the protocol is always the same.

For the moment let's concentrate on the protocol. 

As already mentioned the protocol is simple. The line rests high and represents a zero. When the device starts to transmit it first pulls the line low to generate a start bit. The width of this bit sets the transmission speed - all bits are the same width as the start bit. After the start bit there are a variable number, usually seven or eight data bits, an optional single parity bit and finally one or two zero stop bits. 

Originally the purpose of the start bit was to allow the motors etc to get started and allow the receiving end to perform any timing corrections. The stop bits were similarly their to give time for the motors to come back to their rest position. In the early days the protocol was used at very slow speeds - 300 baud i.e. roughly 300 bits per second was considered fast enough. 

Today the protocol is much the same but there is little need for multiple stop bits and communications is often so reliable that parity bits are dispensed with. Transmission speeds are also higher - typically 9600 or 115200 baud.

To specify what exact protocol is in use you will often encounter a short form 9600 8 data bits no parity one stop bit as 9600 8n1.  

You can see the letter A 01111101 transmitted using 8n1 in the logic analyzer trace. 


The first low is the start bit, then the eight dots show the ideal sampling positions for the receiver. The basic decoding algorithm for receiving serial is to detect the start of the start bit and then sample the line at the center of each bit time. notice that the final high on the right is the stop bit.

For a serial connection to work it is essential that the transmitter and the receiver are set to the same speed, data bits and parity. Serial interfaces most often fail because they are not working with the same settings. 

A logic analyzer with a serial decoder option is an essential piece of equipment if you are going to anything complicated with serial interfacing. 

What is a baud?

Baud rate refers to the basic bit time. That is 300 baud has a start bit that is 1/300s wide. For 9600 this means a bit is 1/9600 wide or roughly 104 microseconds. At 115200 baud a bit is 1/115200 or roughly 8.6 microseconds. 

Notice baud rate doesn't equate to speed of sending data because there is an overhead in stop, start and perhaps parity bits to include in the calculation.