Let's get set up with scripting in Thonny for the Micro:bit. We'll install Thonny, configure for Micro:bit, and write our first script.
All you'll need to follow along is a Micro:bit v2 GO kit
Contents
Download Thonny here and run the installer.
Once the installer finishes, run Thonny.
Connect your Micro:bit V2 to your computer with the USB cable.
Open Thonny, and in the menu bar find Run > Select interpreter and choose MicroPython (BBC micro:bit)
It's also a good idea to install or update firmware. This will update your micro:bit with the latest version of MicroPython, or install MicroPython if it wasn't already.
Make sure the Files pane and Plotter are visible by selecting them in View > Files, and View > Plotter.
Click the red STOP button to restart the MicroPython on your micro:bit if necessary. The Shell tab should display a block of text that indicates MicroPython is running:
We can immediately start executing code in the REPL - Enter this code in the shell tab: print("Hello, World!")
The command will be sent to your micro:bit, which will execute the command and display back the message - nice!
Next, we can also take control of the on-board speaker by executing the following code:
from microbit import * import audio audio.play(Sound.HAPPY)
This code will play a happy tone from the micro:bit's speaker! If you the LED. If you keep executing audio.play(Sound.HAPPY), the tone will repeat.
The REPL is great for test-driving some new commands and performing short experiments - the real power is in scripting though.
Create a new script with File > New and paste in the following code:
from microbit import *
import audio
print("Hello!")
multiple = 1 # initialise the counter
while True:
if button_a.was_pressed(): # get the next multiple of the thirteen
result = multiple * 13 # Calculate the result
print("13 times " + str(multiple) + " is " + str(result)) # print the multiplication
multiple = multiple + 1 # increment the multiple
if button_b.was_pressed(): # Say Hello
display.show(Image.HAPPY)
audio.play(Sound.HAPPY)
display.clear()
sleep(10) # a 10 millisecond delay
Save the script - you will be prompted to save to your computer OR the micro:bit. Select save to micro:bit and name the file main.py
Return to the REPL and press Ctrl+D to restart your micro:bit. If something went wrong, use the Stop/Restart button, then Ctrl+D.
Now, when we press the A button, the Shell will print the next multiple of 13, or if we press the B-button, our micro:bit gives us a smile and a hello sound.
Notice the plot is also showing some lines, and they're colour-coded to numbers in the print statement! In my case:
The Blue line is the constant 13,
The Orange Line is the multiple variable that increases slowly, and
The Red line is the result variable, which increases really quickly.
You can stop a running script by selecting the Shell window and pressing Ctrl+C. This is useful if we want to make changes to the file(s) stored on the micro:bit.
Reboot your micro:bit (or other MicroPython device) by selecting the Shell window and pressing Ctrl+D
If something goes wrong, you can always click the red STOP button in the menu bar
We've been working with a file stored directly on the micro:bit - if you'd like to keep a copy on your computer, right-click the main.py file and select the 'download' option. Similarly, you can always upload code to your micro:bit by right-clicking files on your computer and selecting the upload button.
We've installed Thonny and uploaded our first script to our micro:bit. The script includes branches depending on which button is pressed and can generate audio tones and perform basic arithmetic. Now we can write scripts, move them between micro:bit and computer, or test code interactively using the REPL.
If you have any questions feel free to start the conversation below or open a topic in our forums - we're full-time makers and happy to help!
Makers love reviews as much as you do, please follow this link to review the products you have purchased.