The BBC Micro:bit is a microcontroller board that, as far as I know, was built it thinking on young people for help them to learn coding.
If you are not aware (Like me) this board will pass you away easily thinking that is limited and is only for basic stuff.
The reality is the absolute opposite: This board has so many sensors, leds, buttons and pins to use that, having the same set on an Arduino will require a full box of parts.
Of course, I am not saying this is BETTER. Just saying this is really GOOD!
Anyway, marketing aside, I have had this sitting on my desk for almost a year without even trying to understand its power. Last night suddenly something happened: I look at it, it look at me and challenged me: I challenge you to write and read data from me without writing a single line of code! – It said… ejem! Sorry… I too earlier and I have not had my coffee yet.
Nevertheless, something close to that happened. I started wondering how we could send and receive data from/to the board without using the traditional processes: Coding something in C. Reading the serial output with Python. Etc.
Well, clearly we will need to use a communication channel and the serial port is, perhaps, the simpler. In the other hand, remember, “no coding” is the premise. So, what do we have in the operating system that could help us? We will discuss this later, but first, the board.
I lied a little bit: We do need some code! But, not in the traditional way. For the micro:bit we can use blocks to create a basic sketch that opens the serial for us.
Because this is intended for young people, using this building blocks makes the code creation super easy. And the most important: trivial.
With the previous code then, we are setting the serial ready, expecting data from the serial port and using the front led matrix to write the incoming message and writing into the serial the values from the internal board compass with a refresh rate of 500ms.
So, how we read and write data? We need to know which port the board is using. For this we could go to our devices and search there OR, type some command lines. Clearly, we will type command lines.
On the previous image we can see the device is connected to the COM4. One thing that we need to notice are the properties. Depending how much control do we have over the code deployed into the board, we could decide the bauds speed and other properties. By default 9600 bauds will not work or, at least, the data transferred will be “broken” (Let use this name). We will need to set the this property to 115200 bauds.
Now we have all set and ready to write something into the board. Another command will help us on this.
“echo” is used to print into the console if we used without any other property. On the console, the arrow head (>) means direction. For this case, echo will print the text INTO something. That something is the COM4. Technically we could print to any other connection such as printer or a file.
Now we wrote into the board. How we read from it? Easy!
There are a couple of ways to read from the port, the easiest is using type command. This will open a connection with the board over the designated port and listen anything that is sent by the board.
So, I have not completely lied :). Without writing a line of code and only using what the OS provides we were able to read and write from a board like this one.