Maker.io main logo

How To Write a Hello World Application in the micro:bit Python Editor

2022-12-28 | By Maker.io Staff

micro:bit

A recent article explored some of the most important features the micro:bit Python editor has to offer for beginners and experienced programmers. That article introduced the drag-and-drop capabilities of the web-based IDE and how it aids newcomers in writing their first programs for the micro:bit platform.

This article explains how to use the built-in program blocks and the code editor to get started with creating exciting micro:bit programs in no time. Read on to learn more!

Starting a New Project

The Python editor should already display a working example application when you first visit the website. However, if you have been experimenting with its many features before, the IDE might display a previously modified file. Either way, make sure to manually delete all the code in the editor before you proceed. You can also rename your project:

How To Write a Hello World Application in the micro:bit Python Editor Click the pencil icon to rename your project.

Using the Drag-And-Drop Code Blocks

To get started with writing your first application, use the reference tab on the left-hand side of the web interface to find the loops section:

How To Write a Hello World Application in the micro:bit Python Editor You can find the loop snippets in the reference tab of the UI.

Once selected, search for the infinite while loop code snippet and drag-and-drop it into the text editor:

How To Write a Hello World Application in the micro:bit Python Editor Drag and drop the infinite while loop snippet into the code editor.

It also adds an import together with the while block because the loop contains a function that requires the program to load an external module. This snippet instructs the microcontroller to repeat all actions within the while loop indefinitely as long as the board is powered on. In this case, the LEDs will output “micro:bit” once you upload the program or run it on the built-in simulator.

Either way, go back to the reference menu and scroll down to find the text input and output section. Then, select the “Print output to the serial console” snippet and drag it into the existing while loop:

How To Write a Hello World Application in the micro:bit Python Editor Combine the new snippet with the already existing infinite while loop from before.

As you can see, the editor is smart enough to only add the body of the new snippet to the already existing while loop in the code to combine the two snippets into a larger loop instead of creating a nested loop structure.

Using the Text Editor

Next, modify the print statement you pulled into your code editor in the last step. Click in the editor panel, select the text within the parentheses of the print command and replace it with ‘Hello World’ as shown in the image below:

How To Write a Hello World Application in the micro:bit Python Editor Use the text editor to manually adjust the output text.

The print command outputs text to the development board’s serial console. In the default snippet, the code displays the ambient light level. However, you just replaced that code so that it periodically displays a short text message. Next, move your cursor to the last line of your program and type sleep(1000). As you start typing, you’ll notice that a box pops up that suggests commands that start with the letters you already typed. The website also lets you know which parameters you can supply with a command:

How To Write a Hello World Application in the micro:bit Python Editor The auto-complete feature is a helpful tool when writing code.

The sleep instruction makes the board wait for a certain number of milliseconds, which is 1000 in this case. Change the text of the scroll command similarly to how you altered the print command earlier, and then use the reference menu on the left again to find the sound commands.

Using the Built-In API to Learn more about Commands

In the sound menu, scroll down to find the speech command. You can click the ‘more’ link to display a more detailed description of the command. Some commands offer an even more comprehensive explanation in the built-in API:

How To Write a Hello World Application in the micro:bit Python Editor Use the links in the reference tab to learn more about a command.

Once you know what the speech command does, drag, and drop the pre-built block into the last line of the existing while-loop so that your program looks as shown in the following picture:

How To Write a Hello World Application in the micro:bit Python Editor The finished hello world application should look like this.

Running the Application in the Simulator

Finally, you can use the built-in simulator to test your application without uploading it to a development board. Instead, you can start the simulation by pressing the play button on the micro:bit board in the top-right corner of the IDE. Make sure to also open up the virtual serial monitor in order to see the output, and turn on your computer’s volume to hear the speech command’s output:

How To Write a Hello World Application in the micro:bit Python Editor You can utilize the built-in simulator for testing your programs.

Summary

And just like that, you wrote your first micro:bit application that uses three different forms of output! It might not look like much, but you can expand it from this point to perform even more tasks. As you learned throughout this article, you can drag and drop pre-made code snippets into the editor or use the editor to change your program’s behavior. In addition, you can utilize the built-in API reference to learn more about a command and its parameters and return values.

制造商零件编号 2012414
MICRO:BIT V2 SBC-BOARD ONLY
OKdo
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum