In this article, we'll program a Micro:bit v2 using a web browser.
We'll connect a Micro:bit to our computer, open python.microbit.org and begin coding our first script.
Contents
- What You'll Need
- Connect your Micro:bit
- Open the Programming Website
- Code Remix
- Saving and Loading a File
- Projects with Multiple Files
- Conclusion
What You'll Need
To follow along, you will need:
- A Micro:bit V2
- A computer connected to the internet, with Google Chrome installed. Chrome is recommended because the USB drivers work best at the time of writing.
Connect your Micro:bit
Use a USB cable to connect your Micro:bit to your computer. If you're using a GO kit, the correct cable is supplied.
If this is your first time powering the Micro:bit, it will say hello with a demonstration of its features.
Open the Programming Website
In Chrome, navigate to python.microbit.org - you will be greeted by a programming screen with a short example script:
Let's give it a try! In the top menu bar, click Connect and select your Micro:bit from the list, then click the blue Connect.
The Connect button has changed to a smiling Micro:bit to show that we are now connected.
Click the Flash button to upload the default script. Watch how a message scrolls across your Micro:bit's display, then it shows a heart.
You can change the text by modifying the contents of display.scroll()
Code Remix
Copy the following code and paste it into your editor, replacing all the default code.
# Print the thirteen-times table from microbit import * multiple = 1 while True: result = multiple * 13 print(str(multiple) + " times 13 is " + str(result)) multiple = multiple + 1 sleep(1000)
Flash the code, then click Open Serial. Now the Micro:bit is calculating the thirteen-times tables.
This serial console is a great tool for debugging, and seeing what goes on inside the Micro:bit.
Saving and Loading a Project
We can save this new script to our computer by opening the Load/Save menu. Click "Download Python Script" to download the current contents of the web-editor if you would like to keep a copy on your computer.
Aside: If your project is made up of multiple files, you download the whole project as one file by selecting Download Project Hex. This will capture a more complex project, but the .hex file will not be human-readable. You have to re-upload it to the web-editor to make any changes.
Now we'll load another script - right-click the following link (microbit-button-demo.py) and select "save link as..." to download it to your computer.
Now in the Load/Save menu, upload the same file to your web-editor by either dragging the file into the menu, or click the Browse for a file option.
Once the file is loaded, click flash to see what it does! With some experimenting you should find that pressing the A-button on the Micro:bit will print the next multiple of thirteen in the console; and pressing the B-button causes the microbit to make a happy sound, and show a smile on its display.
Projects with Multiple Files
Some more advanced MicroPython projects may need multiple files, you can always add multiple files using the Add file button under Project Files. If you don't see this option, you may have to click Show Files, which is in the Project Files section.
Once you've added the files, you will need to click the Flash button again to actually copy them onto the Micro:bit.
Conclusion
We've written our first MicroPython script for Micro:bit and flashed it using the web-editor python.microbit.org. We were able to print information to the console and save/load files to/from our computer.
If you have any questions, start the conversation below! We're full time makers and happy to help.
Happy coding!