Friday Fun: Sending Serial Data From micro:bit to laptop

Cereal Killer

If you haven't seen Hackers yet, then rectify that ASAP. ^^^ Yes this is "Cereal Killer" aka the dude who plays Shaggy in Scooby Doo...jinkies!

Serial, no not the stuff that is loaded with sugar, that's cereal. Serial is a connection protocol...

In computing, a serial port is a serial communication interface through which information transfers in or out one bit at a time (in contrast to a parallel port). Throughout most of the history of personal computers, data was transferred through serial ports to devices such as modems, terminals and various peripherals.

Thanks wikipedia

Serial is a basic connection, slow but useful! Nearly all computers have a serial interface, on older machines we saw a 9 pin connection made just for this work, but on newer machines which do not have a serial port we have to use a USB lead that converts the serial connection to USB. But for devices such as Raspberry Pi, Arduino and micro:bit the serial port is integrated into the USB port, so we can send serial data over USB to a waiting computer.

What will you need?

In this short project we shall use

micro:bit

Firstly attach your micro:bit to your computer using the micro USB lead. After a few seconds the micro:bit will appear like a USB / Flash drive.

I used the offline version of the Python Editor, Mu. It is rather lovely!

The code for the micr:bit is a simple import statement, that imports all of the micro:bit library for use. Then inside of a infinite / forever loop, I print the temperature using the micro:bit's built in sensor on the processor. Then we go to sleep for 1 second, before the loop repeats.

from microbit import *
while True:
    print(temperature())
    sleep(1000)

By printing the temperature it is automatically sent to Repl (Read, Eval, Print, Loop) a basic shell console. This in turn finds its way to the serial interface...handy!

If you are using the online editor, click on the Download button to download the code to your computer. Then copy the .hex file to your attached micro:bit.

If you are using Mu, then click on Flash to send the code directly to your micro:bit.

Ok so that's the code for the micro:bit done. Let us now move on to our computer.

Computer / Laptop

Boot up your machine, before we write any code we need to install a Python library using pip3.

Ubuntu / Raspberry Pi

Open a terminal and type

sudo pip3 install pyserial

Followed by your password, after a few moments, the library will be installed. You can now close the terminal and proceed onwards.

Windows

We need to open a Command Prompt. The quickest way is to search for "command prompt" and then click on the suggested application. In the command prompt navigate to your Python install folder. Mine was at C:\Python34 We need to be in the "scripts" folder.

cd c:\Python34\Scripts\

In there we can see the pip3.4.exe application (you may have a different 3.X version, but don't worry just substitute the numbers to match yours)

Now enter this command to install pyserial

Remember to change the 3.4 to match your Python3 install version

pip3.4.exe install pyserial

After a few moments, the library will be installed. You can now close the command prompt and proceed onwards.

Now using your favourite Python 3 editor lets start writing the code.

I'm using IDLE3 on my Ubuntu laptop.

*Can you spot the typos? I noticed after the screenshot that the first two lines inside the loop, that set the port and baud rate are repeated every time the loop goes round. We don't need to do that, so I moved them out of the loop for this project FAIL :)

Create a new file, and call it fridayfun-serial.py

Remember to save often!

We shall start by importing two libraries. The first is our serial library, used for the connection to our micro:bit. The second is time, which we use to control the pace of the project.

import serial, time

Next we create a variable called port that will contain the port used to connect our computer to the micro:bit. As I am using Ubuntu, my port looks a little different than a Windows port, which is typically COMx where x is a number.

port = "/dev/ttyACM0"

Then we create another variable that will store the connection speed needed for our micro:bit and computer to correctly communicate.

baud = 115200

Now we use those two variables to configure our serial port and the speed of connection. We also shorten the serial library reference to just "s", much quicker to type.

s = serial.Serial(port)
s.baudrate = baud

We need to create a loop that will constantly run our code, for this a while loop is best.

while True:

Now all of the code is indented by four spaces or one Tab key press. Don't mix them up or Python will get cranky!

To store the data that is being read from the serial connection, we create a variable called data and we then read the lines of data being sent via the serial connection.

    data = s.readline()

The data that we receive is a little messy, in that it has characters around the temperature data, b'23\r\n' so we can see the temperature is 23C but we have some tidying up to do. So in the next line we update the data variable and slice the string data[0:4] of data received so that we only see the first 4 characters, then we wrap the newly sliced values inside of a function that will convert the string into an integer int().

    data = int(data[0:4])

And for our last two lines of code we need to see the data printed to the Python shell, and instruct the code to wait for a second between each loop, otherwise it will get awfully busy on screen!

    print(data)
    time.sleep(1)

Complete Code Listings

micro:bit

from microbit import *
while True:
    print(temperature())
    sleep(1000)

Computer

Remember to change the port to reflect your operating system, during my test with Windows, it came up as COM6.

import serial, time
port = "/dev/ttyACM0"
baud = 115200
s = serial.Serial(port)
s.baudrate = baud
while True:
    data = s.readline()
    data = int(data[0:4])
    print(data)
    time.sleep(1)

Running the code

Ubuntu / Raspberry Pi

I had to run the code from the terminal as my default user (les/pi) did not have permission to use that serial port /dev/ttyACM0

Open a terminal and navigate to the same directory as the code, now in the terminal type the following.

sudo python3 fridayfun-serial.py

Enter your password, and then watch as the temperature data from our micro:bit is sent to our computer!

Windows

I tested the project on Windows and it worked from inside IDLE3, the Python 3 Editor so no secret handshakes or elevated privileges are needed.

You have no idea now long it took to get my Windows 10 machine ready to test this script....

So there you have it!

We can now send serial data from our micro:bit, to a computer, which can then act upon it.

What can I do with it?

Well I used this application to create a better timing rig for Makersphere's supersonic car race.

The micro:bit was used with two sensors to capture the start and finishing time of a car. This was sent to my laptop using a serial connection, where I wrote a pygame user interface that presented the data in a "digital screen" format :)

More on that later though ;)