In this tutorial we will learn how to use the Micro:bit pins to establish a serial connection, instead of using the USB connector.
In this tutorial we will learn how to use the Micro:bit pins to establish a serial connection, instead of using the USB connector. That way, we can establish a serial connection to other peripherals, rather than only being able to connect the Micro:bit to a computer via USB.
Since we are going to cover the basics of initializing the serial interface and writing some content to the it, we will connect the serial pins of the Micro:bit to a serial to USB converter, so we can check the results on a program running on a computer. Naturally, this is just for testing purposes.
For this test, I’m using version 1.7.0 of uPython for the Micro:bit board. Note that, in this version, when we initialize the serial connection, a dummy byte with value 0 is sent automatically. This behavior is related with the lower level Mbed driver .
This has already been solved in the latest version of uPython for Micro:bit (the hex file can be downloaded here). Nonetheless, at the time of writing, some tools still use older versions of uPython (for example, the uPyCraft IDE).
So, this behavior needs to be taken in consideration when, for example, interacting with other devices by sending serial commands, because the dummy byte can be interpreted wrongly as the beginning of a command.
The electric schematic
As serial to USB converter I will be using this cheap device, which allows to operate in both 3.3 V and 5 V levels. In the case of the connection to the Micro:bit, we should use the 3.3 V digital level.
The connection diagram between the Micro:bit and the serial to USB converter is very simple, as illustrated in figure 1.
Figure 1 – Connection diagram.
As can be seen, we will us pin 0 of Micro:bit as Tx and pin 1 as Rx. The numbers of these pins are labeled on the Micro:bit board.
Then, we need to connect the Rx pin of Micro:bit to the Tx pin of the converter, and the Tx pin of Micro:bit to the Rx pin of the converter.
Also, note that both devices should be connected to a common ground.
To get started, we will first take care of all the module imports needed. So, we will need to import the uart object from the microbit module. We will use it to initialize the serial connection and to send data to it.
Additionally, we will also import the sleep function from that module, so we can introduce some delays in our code. You can read more about the sleep function and pausing the program execution here.
from microbit import uart, sleep
After this, we will initialize the serial interface by calling the init method on the uart object we have just imported. As arguments, we need to specify the baud rate to be used and the serial tx and rx pins.
As baud rate, we will use a value of 9600. For the pins, we will set tx to pin 0 and rx to pin 1, as we have seen on the electric diagram.
Important: After initializing the serial interface on external pins, the uPython prompt, which operates over USB, will become unavailable .
uart.init(baudrate=9600, tx = pin0, rx = pin1)
After we initialize the serial interface, we will print some content to it by calling the write method on the uart object. As input, we should pass the string with the content we want to send to the serial port. We will send a simple “Hello World” message.
uart.write("Hello from micro:bit running uPython")
After that, we will do a small 1 second delay and we will bring back the uPython console. To bring back the console, we simply need to call again the init method of the uart object, using a baud rate of 115200 .
The rest of the parameters should be kept to their defaults, which means we should only specify the baudrate parameter of this method.
The final code complete code can be seen below.
from microbit import uart, sleep uart.init(baudrate=9600, tx = pin0, rx = pin1) uart.write("Hello from micro:bit running uPython") sleep(1000) uart.init(baudrate=115200)
Testing the code
In order to test the previous code, we will need to use some computer serial tool to establish a connection with the serial to USB converter port and read the bytes sent by Micro:bit. In my case, I’ll be using the Arduino IDE serial monitor, but you can use other tool, such as Putty.
So, after wiring the Micro:bit to the serial to USB converter and power both devices, simply open the Arduino IDE and select the correct serial port under the Tools menu, as shown in figure 2.
If you are not sure which port was assigned to your converter, one easy way to find out is unplugging it, checking the list of available COM ports in the previous menu, plug it back and check the new one that was added.
Figure 2 – Choosing the serial port on the Arduino IDE.
Next, also on the Tools menu, click on the “Serial Monitor” option and the serial monitor window should open. Change the baud rate to 9600 on the lower right corner, in the corresponding dropdown.
After that, simply run the previous script in your Micro:bit board. You can use, for example, the uPyCraft IDE to upload and run the script. If you go back to the Arduino IDE serial monitor, you should get an output similar to figure 3.
Note the dummy byte printed before the message, highlighted in red, which is displayed as an empty space. If you are using the latest version of uPython, then this byte should not be sent before the message.
Figure 3 – Message sent by the Micro:bit, printed on the Arduino IDE serial monitor.
After the code executes, if you check your uPython environment, the prompt should be available again.