Getting Started with MicroPython and the SparkFun Inventor's Kit for micro:bit
Experiment 0: Hello, micro:bit!
Hello World: A programmer's first program
A "Hello World" on the micro:bit is a little different. On most microcontrollers this program would be executed using a serial terminal. Instead of using a serial terminal, you can interact with your micro:bit using the built-in LED array. So, the "Hello World" for the micro:bit is to draw something using the LED array!
Let's first run the program from the REPL, and then we will build a .py script and upload it to the micro:bit. Open Mu, and make sure your micro:bit is connected to your computer with a USB cable.
To open the REPL, click the icon, and you should see a second window appear at the bottom. Type
help() and see what happens.
From the REPL, type
display.scroll("Hello World") and watch your micro:bit's 5x5 LED array.
language:python display.scroll("Hello World")
Now close the REPL by clicking the REPL icon!
For the first MicroPython script, we are going to cover how to add comments, how to import a module, and how to create a loop that will run forever. At the top of the program (above), you'll see three lines of comments. Comments are created by using the # sign and one space. You can access modules by using
import. These words tell the interpreter which classes to import from which modules. In this case, we are importing everything from microbit.
while True: is how to create a forever loop in Python. The colon is how Python blocks code the way Arduino uses a set of curly brackets. Everything indented under a colon will execute as a block of code.
Let's display an image next instead of text. Type (or copy) the program into your Mu editor, or download all the programs from this GitHub Repository. Save it, then click the Flash icon to program your micro:bit.
language:python # SparkFun Electronics # Experiment 0.1 # Display an image from microbit import * while True: display.show(Image.DUCK)
display.show(Image...), let the helpful info box show you what images come built in. Then try to code your own custom image.
language:python # SparkFun Electronics # Experiment 0.2 # Display a custom image from microbit import * while True: star = Image("00900:99599:05950:09590:90009") display.show(star)