The micro:bit module is a tiny computer with lights, pushbuttons, and sensors. Follow this tutorial to get ready for programming your micro:bit with the Python text language.
This is the first tutorial in the cyber:bot, cybersecurity, or "What's a Microcontroller" with Python & micro:bit tutorial series.
A Windows, Mac or Chromebook computer with:
*If your computer only has USB C ports, you will also need a USB C to A Adapter.
A script, also called a program, is a set of instructions that a computer runs to make the computer perform tasks. Scripts make phone apps do their jobs, and they also make web pages respond to your clicks and taps. Scripts for the micro:bit can make it do all kinds of interesting apps as you will see.
For the micro:bit to run a script, it first needs to be transferred to the micro:bit module’s flash memory.
In this activity, you will use the micro:bit Python Editor V3 to write scripts and to transfer scripts into the micro:bit module’s flash memory.
The micro:Bit Python Editor v3 runs in your Chrome or Microsoft Edge browser. If you must be offline while writing and loading scripts for your micro:bit, you may use the optional Mu editor installed on your Windows or Mac computer
The instructions below guide you step-by-step through connecting your micro:bit to your computer and running your first script. The video and red checklist instructions are about the same.
Make sure to follow along and repeat the steps with your own micro:bit.
Now that your micro:bit module is connected, the Python Editor will remember that connection. So, it will load the script right away without the popup asking you to choose your micro:bit.
It’s always a good idea to save your scripts so that you can open and improve them later. This activity’s step-by-step video and matching checklist instructions show how to save and reopen a script.
Make sure to follow along with your own computer and micro:bit module.
# Imports go at the top from microbit import * # Code in a 'while True:' loop repeats forever while True: display.show(Image.HAPPY) sleep(1000) display.scroll('Hi!')
The micro:bit Python Editor has editor controls for changing font size, undoing and redoing editor actions, and collapsing and restoring the side panels.
The left side panel has three tabs for learning more about scripting with Python for the micro:bit. These tabs are named Reference, Ideas, and API.
This activity’s step-by-step video and matching checklist instructions show how to make use of these editor controls and left side panel tabs.
The micro:bit Python Editor’s serial monitor is great for displaying messages from the mico:bit. You can also type messages into the serial monitor to send them to the micro:bit.
Another nice thing about the serial monitor is that there’s no need to wait for messages to scroll across the micro:bit module’s LED matrix display. You’ll be able to see the whole massage, all at once.
Different Python statements display messages in the serial monitor. Instead of display.scroll and display.show for the LED matrix, you’ll be using print statements to display messages and input statements to collect the text of messages you type.
This activity will use an example script that makes the micro:bit communicate with the serial monitor. The micro:bit will prompt you to type some text, and then repeat back what you typed. The video goes through the same steps as the checklist instructions.
from microbit import * while True: text = input('Type a message: ') print('You typed:', text)
Links
[1] https://python.microbit.org/v/3