Using GUIZero—a learner-friendly tkinter wrapper—to create a serial connection to the microbit in Python with pySerial.
It is possible to send commands to the microbit over a serial connection from your computer. The
REPL function in the mu editor is simply a serial connection. For example,
display.show(Image.HAPPY) causes a face to show on the microbit’s display.
To send Python commands, the microbit must be running a micropython
.hex file (eg, created in mu).
In this example, a Python GUI program sends commands to the microbit flashed in mu with a blank script.
This program uses two Python modules: pySerial and GUIZero.
pySerial handles the serial communication. It scans for the microbit and creates a serial connection.
GUIZero is for drawing the GUI. It is a learner-friendly wrapper for TKInter.
Handles the serial connection to the microbit.
def find_microbit_comport(): ports = list(list_ports.comports()) for p in ports: if (p.pid == 516) and (p.vid == 3368): return str(p.device)
This function returns a string of the microbit’s COM port. On Windows this might be
COM3, on Linux
product ID &
vendor ID of each attached device is evaluated. If it is the same as the ones on a microbit (
3368) then return the device name (eg COM4).
serial.tools.list_ports must be imported.
Create a new instance of the Serial class as
ser = serial.Serial()
ser baud rate to 115200. This is the default baud rate of the microbit:
ser.baudrate = 115200
ser COM port to the one found by
ser.port = find_microbit_comport()
Before writing data the COM port must be opened.
Serial.open() throws an exception if a connection cannot be made.
With the COM port open, we can write a command:
ser.write("display.show(Image.HAPPY) \r".encode() )
\r is a carriage return. It tells
REPL on the microbit that return has been pressed and it should execute the line sent.
.encode() turns the string into bytes.
GUI Zero is a learner-friendly wrapper for
Tkinter. It allows beginners to easily create GUIs for the program by removing many of the complexities of
It’s still at the beginning phase and I’m hoping this is the first program using it!
The program’s window is an instance of the
App class. In this example it’s assigned to
app = App() # The app window can be modified by with parameters. # EG app = App(title="My Microbit Program") for a title. # The width, height, and other things can be changed.
connect_button = PushButton(app, text="Connect", command=connect) # create an instance of the PushButton class. # The GUI parent (or master) is app (main app window). # Text is "Connect" # When it is clicked, connect() is executed
The final line is
app.display(). This renders the GUI we have set up.
Box() in GUIZero extends the
Frame class in
Tkinter. It allows GUI elements such as
ButtonGroup() to be arranged together.
button_box = Box(app)
In the example below, the
disconnect sit within the
button_box instance of
connect_button = PushButton(button_box, text="Connect", command=connect)
This allows the collection buttons to sit together in the GUI.
Published: December 23, 2016 Updated: December 23, 2016