Getting Started with MicroPython and the SparkFun Inventor's Kit for micro:bit

Pages
Contributors: LightningHawk
Favorited Favorite 2

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.

help menu

Click any of the images for a closer look.

From the REPL, type display.scroll("Hello World") and watch your micro:bit’s 5x5 LED array. 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 from and 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.

Type the program above in your Mu editor, or download all the programs from this GitHub Repository. Save it, then click the Flash icon to program your micro:bit.

Let’s display an image next instead of text.

alt text

When typing display.show(Image...), let the helpful info box show you what images come built in. Then try to code your own custom image.

alt text