Small Basic + micro:bit

Published Dec 08 2020 12:18 PM 1,590 Views
Microsoft

Today is a look back at a guest blog post from a teacher, Jez Dean...

Originally written on 09/13/2017.

 

SmallBasicUI.jpg

 

In this blog post: Build a computer-to-micro:bit connection with Small Basic.

 

You can write a simple GUI to send commands to a micro:bit running Python:

 

SBMController.png

 

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

  1. Flash a Python program to the micro:bit. You can just flash an empty program from within mu.
  2. Install the lit dev extensions for Small Basic. These are used to communicate over a serial connection.
  3. Find out the COM port of your micro:bit.
  4. 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

Other Resources from Jez Dean (website is down now)

%3CLINGO-SUB%20id%3D%22lingo-sub-1968424%22%20slang%3D%22en-US%22%3ESmall%20Basic%20%2B%20micro%3Abit%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1968424%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EToday%20is%20a%20look%20back%20at%20a%20guest%20blog%20post%20from%20a%20teacher%2C%26nbsp%3B%3C%2FSPAN%3E%3CEM%3EJez%20Dean%3C%2FEM%3E%3CSPAN%3E...%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EOriginally%20written%20on%26nbsp%3B09%2F13%2F2017.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22SmallBasicUI.jpg%22%20style%3D%22width%3A%20220px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F238739iCA7411134F3BF93E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22SmallBasicUI.jpg%22%20alt%3D%22SmallBasicUI.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EIn%20this%20blog%20post%3C%2FSTRONG%3E%3A%20Build%20a%20computer-to-micro%3Abit%20connection%20with%20Small%20Basic.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EAlso%2C%20see%20the%20article%20from%20Nonki%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fsmallbasic%2F2017%2F08%2F28%2Fplay-with-microbit-and-small-basic%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%20data-linktype%3D%22external%22%3EPlay%20with%20micro%3Abit%20and%20Small%20Basic%3C%2FA%3E.%3C%2FLI%3E%0A%3CLI%3ESee%20the%20article%2C%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fsocial.technet.microsoft.com%2Fwiki%2Fcontents%2Farticles%2F40340.small-basic-how-to-use-micro-bit.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%20data-linktype%3D%22external%22%3ESmall%20Basic%3A%20How%20to%20Use%20micro%3Abit%3C%2FA%3E%2C%20for%20another%20solution%20from%20Nonki%20that%20moves%20the%20Turtle.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20write%20a%20simple%20GUI%20to%20send%20commands%20to%20a%20micro%3Abit%20running%20Python%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22SBMController.png%22%20style%3D%22width%3A%20502px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F238740i8A9734F060BFE8AF%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22SBMController.png%22%20alt%3D%22SBMController.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20program%20sends%20Python%20commands%20over%20a%20serial%20connection%20to%20the%20micro%3Abit.%20I've%20kept%20it%20as%20simple%20as%20possible%20so%20you%20can%20customize%20it%20further.%3C%2FP%3E%0A%3CP%3EClicking%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3EPIN0%20High%3C%2FEM%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Esends%20the%20command%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Epin0.write_digital(1)%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eto%20the%20micro%3Abit.%20You%20could%20type%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Edisplay.show(Image.HAPPY)%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%20click%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3ESend%20Command%3C%2FEM%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ewould%20show%20a%20smiley%20on%20the%20micro%3Abit.%3C%2FP%3E%0A%3CH3%20id%3D%22instructions%22%20class%3D%22heading-anchor%22%20id%3D%22toc-hId-1316366153%22%20id%3D%22toc-hId-1316366156%22%3EInstructions%3C%2FH3%3E%0A%3COL%3E%0A%3CLI%3EFlash%20a%20Python%20program%20to%20the%20micro%3Abit.%20You%20can%20just%20flash%20an%20empty%20program%20from%20within%20mu.%3C%2FLI%3E%0A%3CLI%3EInstall%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22http%3A%2F%2Flitdev.co.uk%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20data-linktype%3D%22external%22%3Elit%20dev%20extensions%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efor%20Small%20Basic.%20These%20are%20used%20to%20communicate%20over%20a%20serial%20connection.%3C%2FLI%3E%0A%3CLI%3EFind%20out%20the%20COM%20port%20of%20your%20micro%3Abit.%3C%2FLI%3E%0A%3CLI%3EAdd%20your%20COM%20port%20at%20the%20top%20of%20the%20Small%20Basic%20code.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CH3%20id%3D%22code%22%20class%3D%22heading-anchor%22%20id%3D%22toc-hId--491088310%22%20id%3D%22toc-hId--491088307%22%3ECode%3C%2FH3%3E%0A%3CPRE%20class%3D%22has-inner-focus%22%20tabindex%3D%220%22%3E%3CCODE%20class%3D%22lang-highlight%22%20style%3D%22box-sizing%3A%20inherit%3B%20font-family%3A%20SFMono-Regular%2C%20Consolas%2C%20'Liberation%20Mono'%2C%20Menlo%2C%20Courier%2C%20monospace%3B%20font-size%3A%201em%3B%20outline-color%3A%20inherit%3B%20direction%3A%20ltr%3B%20position%3A%20relative%3B%20border%3A%200px%3B%20padding%3A%200px%3B%20display%3A%20block%3B%20line-height%3A%2019px%3B%22%20data-author-content%3D%22%20comPort%20%3D%20%26quot%3BCOM4%26quot%3B%0A%0AInitialise()%0A%0AWhile%20(%26quot%3BTrue%26quot%3B)%0A%20%20Program.Delay(10)%0AEndWhile%0A%0ASub%20Initialise%0A%20%20'draw%20window'%0A%20%20GraphicsWindow.Width%20%3D%20500%0A%20%20GraphicsWindow.Height%20%3D%20100%0A%20%20GraphicsWindow.Title%20%3D%20%26quot%3BSmall%20Basic%20Microbit%20Controller%26quot%3B%0A%0A%20%20'draw%20pin%20control%20buttons'%0A%20%20pin0high%20%3D%20Controls.AddButton(%26quot%3BPIN0%20High%26quot%3B%2C%2020%2C%2010)%0A%20%20pin0low%20%3D%20Controls.AddButton(%26quot%3BPIN0%20Low%26quot%3B%2C%20100%2C%2010)%0A%0A%20%20'draw%20status%20and%20command%20buttons%20%26amp%3B%20boxes'%0A%20%20statusbox%20%3D%20Controls.AddTextBox(20%2C50)%0A%20%20value%20%3D%20Controls.AddTextBox(210%2C13)%0A%20%20send%20%3D%20Controls.AddButton(%26quot%3BSend%20Command%26quot%3B%2C%20380%2C%2010)%0A%0A%20%20'Open%20connection%20to%20microbit'%0A%20%20Controls.SetTextBoxText(statusbox%2C%26quot%3BStatus%20%3A%20%26quot%3B%2B%20LDCommPort.OpenPort(comPort%2C%209600))%0A%0A%20%20'Assign%20variables'%0A%20%20Controls.ButtonClicked%20%3D%20OnButtonClicked%0A%20%20nl%20%3D%20Text.GetCharacter(10)%20'new%20line%20character'%0AEndSub%0A%0ASub%20OnButtonClicked%0A%20%20button%20%3D%20Controls.LastClickedButton%0A%20%20val%20%3D%20Controls.GetTextBoxText(value)%0A%0A%20%20'if%20button%20pressed%2C%20send%20command%20and%20new%20line%20character'%0A%20%20If%20(button%20%3D%20pin0high)%20Then%0A%20%20%20%20LDCommPort.TXString(%26quot%3Bpin0.write_digital(1)%2Bnl%26quot%3B)%0A%20%20ElseIf%20(button%20%3D%20pin0low)%20Then%0A%20%20%20%20LDCommPort.TXString(%26quot%3Bpin0.write_digital(0)%2Bn1%26quot%3B)%0A%20%20EndIf%0A%20%20If%20(button%20%3D%20send)%20And%20(val%20%26lt%3B%26gt%3B%20%26quot%3B%26quot%3B)%20Then%0A%20%20%20%20LDCommPort.TXString(val%2Bnl)%0A%20%20EndIf%0AEndSub%0A%22%3E%20comPort%20%3D%20%22COM4%22%0A%0AInitialise()%0A%0AWhile%20(%22True%22)%0A%20%20Program.Delay(10)%0AEndWhile%0A%0ASub%20Initialise%0A%20%20'draw%20window'%0A%20%20GraphicsWindow.Width%20%3D%20500%0A%20%20GraphicsWindow.Height%20%3D%20100%0A%20%20GraphicsWindow.Title%20%3D%20%22Small%20Basic%20Microbit%20Controller%22%0A%0A%20%20'draw%20pin%20control%20buttons'%0A%20%20pin0high%20%3D%20Controls.AddButton(%22PIN0%20High%22%2C%2020%2C%2010)%0A%20%20pin0low%20%3D%20Controls.AddButton(%22PIN0%20Low%22%2C%20100%2C%2010)%0A%0A%20%20'draw%20status%20and%20command%20buttons%20%26amp%3B%20boxes'%0A%20%20statusbox%20%3D%20Controls.AddTextBox(20%2C50)%0A%20%20value%20%3D%20Controls.AddTextBox(210%2C13)%0A%20%20send%20%3D%20Controls.AddButton(%22Send%20Command%22%2C%20380%2C%2010)%0A%0A%20%20'Open%20connection%20to%20microbit'%0A%20%20Controls.SetTextBoxText(statusbox%2C%22Status%20%3A%20%22%2B%20LDCommPort.OpenPort(comPort%2C%209600))%0A%0A%20%20'Assign%20variables'%0A%20%20Controls.ButtonClicked%20%3D%20OnButtonClicked%0A%20%20nl%20%3D%20Text.GetCharacter(10)%20'new%20line%20character'%0AEndSub%0A%0ASub%20OnButtonClicked%0A%20%20button%20%3D%20Controls.LastClickedButton%0A%20%20val%20%3D%20Controls.GetTextBoxText(value)%0A%0A%20%20'if%20button%20pressed%2C%20send%20command%20and%20new%20line%20character'%0A%20%20If%20(button%20%3D%20pin0high)%20Then%0A%20%20%20%20LDCommPort.TXString(%22pin0.write_digital(1)%2Bnl%22)%0A%20%20ElseIf%20(button%20%3D%20pin0low)%20Then%0A%20%20%20%20LDCommPort.TXString(%22pin0.write_digital(0)%2Bn1%22)%0A%20%20EndIf%0A%20%20If%20(button%20%3D%20send)%20And%20(val%20%26lt%3B%26gt%3B%20%22%22)%20Then%0A%20%20%20%20LDCommPort.TXString(val%2Bnl)%0A%20%20EndIf%0AEndSub%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EAdapted%20from%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fsmallbasic%2F2016%2F03%2F22%2Fsmall-basic-arduino%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%20data-linktype%3D%22external%22%3Elitdev%20on%20the%20Small%20Basic%20blog%3C%2FA%3E%3C%2FP%3E%0A%3CH3%20id%3D%22notes%22%20class%3D%22heading-anchor%22%20id%3D%22toc-hId-1996424523%22%20id%3D%22toc-hId-1996424526%22%3ENotes%3C%2FH3%3E%0A%3CUL%3E%0A%3CLI%3EThis%20is%20not%20interactive%3B%20it%20will%20not%20return%20any%20value%20the%20microbit%20sends.%3C%2FLI%3E%0A%3CLI%3EThis%20will%20only%20work%20over%20USB.%20Python%20does%20not%20work%20over%20Bluetooth.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH3%20id%3D%22see-also%22%20id%3D%22toc-hId-188970060%22%20id%3D%22toc-hId-188970063%22%3E%3CSTRONG%3ESee%20Also%3C%2FSTRONG%3E%3C%2FH3%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fsmallbasic%2F2017%2F08%2F28%2Fplay-with-microbit-and-small-basic%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%20data-linktype%3D%22external%22%3EPlay%20with%20micro%3Abit%20and%20Small%20Basic%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E-%20Blog%20Post%20from%20Nonki%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fsocial.msdn.microsoft.com%2FForums%2Fen-US%2F0a6e1323-993c-4378-a181-2d08d45374df%2Fsmall-basic-with-microbit%3Fforum%3Dsmallbasic%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%20data-linktype%3D%22external%22%3ESmall%20Basic%20with%20micro%3Abit%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E-%20Forum%20Thread%20from%20Nonki%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fsocial.technet.microsoft.com%2Fwiki%2Fcontents%2Farticles%2F40340.small-basic-how-to-use-micro-bit.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%20data-linktype%3D%22external%22%3ESmall%20Basic%3A%20How%20to%20Use%20micro%3Abit%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E-%20Wiki%20Article%20from%20Nonki%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH3%20id%3D%22other-resources-from-jez-dean%22%20id%3D%22toc-hId--1618484403%22%20id%3D%22toc-hId--1618484400%22%3EOther%20Resources%20from%20Jez%20Dean%3C%2FH3%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22http%3A%2F%2Fmicrobit-playground.co.uk%2Fhowto%2Fmicrobit-to-small-basic%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20data-linktype%3D%22external%22%3ESmall%20Basic%20to%20Python%20REPL%20on%20Microbit%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E-%20This%20article%20on%20Jez's%20site%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22http%3A%2F%2Fmicrobit-playground.co.uk%2Fhowto%2Fmicrobit-using-adding-packages%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20data-linktype%3D%22external%22%3EAdd%20Packages%20to%20micro%3Abit%20with%20MakeCode%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22http%3A%2F%2Fmicrobit-playground.co.uk%2Fhowto%2FGUIZero-pySerial-microbit-how-to-connect%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20data-linktype%3D%22external%22%3EMicrobit%20Python%20Program%20with%20pySerial%20and%20GUIZero%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22http%3A%2F%2Fmicrobit-playground.co.uk%2Fhowto%2Fadd-python-module-microbit-micropython%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20data-linktype%3D%22external%22%3EAdd%20Python%20Modules%20to%20the%20Microbit%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22http%3A%2F%2Fmicrobit-playground.co.uk%2Fhowto%2Fmicrobit-in-scratch%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20data-linktype%3D%22external%22%3Emicrobit%20%26amp%3B%20Scratch%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1968424%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EToday%20is%20a%20guest%20blog%20post%20from%20a%20teacher%2C%26nbsp%3B%3CEM%3EJez%20Dean%3C%2FEM%3E...%26nbsp%3B%26nbsp%3B%3CSTRONG%3EIn%20this%20blog%20post%3C%2FSTRONG%3E%3A%20Build%20a%20computer-to-micro%3Abit%20connection%20with%20Small%20Basic.%26nbsp%3BYou%20can%20write%20a%20simple%20GUI%20to%20send%20commands%20to%20a%20micro%3Abit%20running%20Python.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Version history
Last update:
‎Dec 08 2020 01:29 PM
Updated by: