This article is contributed. See the original author and article here.
Today is a look back at a guest blog post from a teacher, Jez Dean…
Originally written on 09/13/2017.
In this blog post: Build a computer-to-micro:bit connection with Small Basic.
- Also, see the article from Nonki, Play with micro:bit and Small Basic.
- See the article, Small Basic: How to Use micro:bit, for another solution from Nonki that moves the Turtle.
You can write a simple GUI to send commands to a micro:bit running Python:
The program sends Python commands over a serial connection to the micro:bit. I’ve kept it as simple as possible so you can customize it further.
Clicking PIN0 High sends the command pin0.write_digital(1)
to the micro:bit. You could type display.show(Image.HAPPY)
and click Send Command would show a smiley on the micro:bit.
Instructions
- Flash a Python program to the micro:bit. You can just flash an empty program from within mu.
- Install the lit dev extensions for Small Basic. These are used to communicate over a serial connection.
- Find out the COM port of your micro:bit.
- Add your COM port at the top of the Small Basic code.
Code
comPort = “COM4”
Initialise()
While (“True”)
Program.Delay(10)
EndWhile
Sub Initialise
‘draw window’
GraphicsWindow.Width = 500
GraphicsWindow.Height = 100
GraphicsWindow.Title = “Small Basic Microbit Controller”
‘draw pin control buttons’
pin0high = Controls.AddButton(“PIN0 High”, 20, 10)
pin0low = Controls.AddButton(“PIN0 Low”, 100, 10)
‘draw status and command buttons & boxes’
statusbox = Controls.AddTextBox(20,50)
value = Controls.AddTextBox(210,13)
send = Controls.AddButton(“Send Command”, 380, 10)
‘Open connection to microbit’
Controls.SetTextBoxText(statusbox,”Status : “+ LDCommPort.OpenPort(comPort, 9600))
‘Assign variables’
Controls.ButtonClicked = OnButtonClicked
nl = Text.GetCharacter(10) ‘new line character’
EndSub
Sub OnButtonClicked
button = Controls.LastClickedButton
val = Controls.GetTextBoxText(value)
‘if button pressed, send command and new line character’
If (button = pin0high) Then
LDCommPort.TXString(“pin0.write_digital(1)+nl”)
ElseIf (button = pin0low) Then
LDCommPort.TXString(“pin0.write_digital(0)+n1”)
EndIf
If (button = send) And (val <> “”) Then
LDCommPort.TXString(val+nl)
EndIf
EndSub
Adapted from litdev on the Small Basic blog.
Notes
- This is not interactive; it will not return any value the microbit sends.
- This will only work over USB. Python does not work over Bluetooth.
See Also
- Play with micro:bit and Small Basic – Blog Post from Nonki
- Small Basic with micro:bit – Forum Thread from Nonki
- Small Basic: How to Use micro:bit – Wiki Article from Nonki
Other Resources from Jez Dean (website is down now)
- Small Basic to Python REPL on Microbit – This article on Jez’s site
- Add Packages to micro:bit with MakeCode
- Microbit Python Program with pySerial and GUIZero
- Add Python Modules to the Microbit
- microbit & Scratch
Brought to you by Dr. Ware, Microsoft Office 365 Silver Partner, Charleston SC.
Recent Comments