Control micro:bits from your PC

BBC Micro Bit, (micro:bit) is an open source hardware ARM-based embedded system designed by the BBC for use in computer education in the UK. The device is half the size of a credit card and has an ARM Cortex-M0 processor, accelerometer and magnetometer sensors, Bluetooth and USB connectivity, a display consisting of 25 LEDs, and two programmable button.

Depending on where you purchase it the price ranges between $15-$20. So this is a very attractive module for the beginning programmer.

The micro:bit module has 2 buttons to interface to it and a small 5×5 LED screen. This is good for small tests but its a little limiting if you want to see or do more complex tasks.

In this blog I wanted to discuss how to connect a micro:bit to a PC so that commands can be sent to the micro:bit and text responses can be viewed on the PC.

First Impressions

My first impression was that the micro:bit was super easy to hook up and get started on. All you need is a Web browser (https://makecode.microbit.org/) and a USB cable to get started on some programming.

Some of my other comments are:

  • It’s great how you can toggle between block programming and Javascript
  • Python support is not as complete as block or Javascript (i.e. no out of the box serial support)
  • MIT Scratch is supported but limited. You can not write out to pins, only read

 

PC to micro:bit Serial Connections

My goal was to create a menu example where I could read/control logic from a PC to a micro:bit. For my testing I used:

  • T – read the temperature of the micro:bit
  • L – return the light sensor value
  • 1 – set an output (LED on pin 8 is turned on)
  • 0 – reset an output (LED on pin 8 is turned off)

Micro:bit Logic

The micro:bit block logic is added by selecting an item from the left code block palette. To start catching serial communications a “Serial on data received” block is used.

mb_serial_logic

After this block is added other blocks are inserted into it. For this example I’m saving the “Serial read line” data into a variable called cmd. Because a new line character is sent from the PC after the command, a substring block is used to caption only the first character sent. (So cmd = “T” and not “T\n” for a temperature request).

The next step is to use an if block to determine which command is sent and do the requested action. The serial write line block is used to send back text to the PC.

The full logic for this example is shown below:

serial_blocks

This same logic could be done in JavaScript.

javascript

PC side communications

For the PC side communications I used Python. Depending if you are using Windows, MacOS or Linux you will need to determine which serial port you are using. If you are using Window you can use Device Manager to see which port is connected to the micro:bit.

device_manager

For the Python interface I used the Tkinter graphic library and I used 4 buttons to send commands and a label to show the feedback.

Some important points in the Python program:

  • The serial communication is by default at 115200 baud
  • The micro:bit is expecting a new line character at the end of a command, so a “\n” is needs to be appended to the end of a command.
  • The serial library needs the strings to be sent out encoded

 

# Python serial interface to a micro:bit
#
import serial
import tkinter as tk

# Send a command to the micro:bit and show the response
def myfunc(action):
   print ("Requested action: ",action)
   out = action + "\n"
   out2 = out.encode('utf_8')
   ser.write(out2)   
   lstatus.config(text = ser.readline())

# configure the serial connections (this will differ on your setup)
ser = serial.Serial(
    port='COM7',
    baudrate=115200
)

root = tk.Tk()
root.title("Micro:bit")

# Button for temperature request
bt_T = tk.Button(root, width=10,text= "Get Temp",bg='silver' ,
  command = lambda: myfunc("T"),relief="raised")
bt_T.grid(row = 1,column = 1)

# Button for light sensor data request
bt_L = tk.Button(root, width=10,text= "Get Light",bg='silver' ,
  command = lambda: myfunc("L"),relief="raised")
bt_L.grid(row = 1,column = 2)

# Button to set LED ON
bt_1 = tk.Button(root, width=10,text= "LED ON",bg='silver' ,
  command = lambda: myfunc("1"),relief="raised")
bt_1.grid(row = 2,column = 1)

# Button to set LED OFF
bt_0 = tk.Button(root, width=10,text= "LED OFF",bg='silver' ,
  command = lambda: myfunc("0"),relief="raised")
bt_0.grid(row = 2,column = 2)

# Label to show results
lstatus = tk.Label(root, width= 25, text= "                 ", relief="raised")
lstatus.grid(row = 3,column = 1,  columnspan = 2)

root.mainloop()

Microbit_menu

Summary

For this example I used a simple Python menu to send some commands down to the micro:bit and get a response back. Other projects could include:

  • Have the micro:bit periodically scan I/O (like moisture sensors) and sent the data to a PC for historical trending or alarms
  • control physical devices like servos and motors

 

 

 

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s