Hello World!¶
Your First Program¶
Coding using the micro:bit is composed of these 4 steps. You can expect to go around the loop quite a few times before you get your code working.
Design the Code¶
First of all you are going to write a program to display the message “Hello World!” followed by an image on the display of your micro:bit. There’s not much planning and design to do here, but just so that you understand what a plan might look like:
My Plan
1. Display "Hello World!!"
2. Display a heart image
3. Prints "Hi There" on the REPL
4. Wait for 3 seconds
Write the Code¶
We will use a special text editor (Mu or the online python editor) to write our programs, it looks like the one shown here:
Let’s go through this line-by-line
from microbit import *
This line will give you access to use the micro:bit module (library)
# Repeat Forever
The ‘#’ operator is used to add comments on your program. It will not be executed, but instead its function is to describe what the line/s of code do.
while True:
This means do something (whatever follows this statement and is indented) forever and ever
and ever. This is called a loop, it’s a bit like a video clip that’s stuck on repeat. True
and False have a special meaning in python. True is always, well True. The rest of
the program is straightforward
from microbit import *
# Repeat Forever
while True:
# Display 3 messages from LED
display.show('Hello World!')
# Display heart
display.show(Image.HEART)
# Print 'Hi There' on the REPL
print('Hi There')
# Pause for 3 seconds
sleep(3000)
# Clears display
display.clear()
This displays Hello World on the LED display one character at a time and then shows the heart. You can also use display.scroll()
to scroll your name instead of displaying the character one by one.
The statement print('Hi There'), will print the message in the REPL. Press the REPL button in the menu now to show the REPL window:
On the online python editor the REPL window will be slightly different. When the micro:bit is connected to usb there will be a new menu at the bottom to show the serial console. To show the serial console, click the Show serial text.
The REPL window shows us messages from the micro:bit and also allows us to send commands directly to the micro:bit. For now, we’ll just be using the REPL to see messages that we print and error messages.
But what is a REPL anyway? REPL stands for R*ead, *E*valuate, *P*rint, *L*oop. It is an interactive way of talking to the computer in Python. You can use this to run some commands interactively with your mico:bit when connected via USB only. On the online editor, this is done through the serial console. To run commands interactively, you need to show the serial console and press the ellipsis button (those 3 vertical dots) and press *Send Ctrl-C for REPL
You might be wondering why we’ve asked the
micro:bit to sleep for 3000! This value is in microseconds so we’ve really only asked it to sleep for 3 seconds. That will give us enough time to see the image before the micro:bit starts all over again.
Download the Code¶
Final checks. Is your micro:bit connected to your computer? Yes? Then press the flash button (or Send to micro:bit button) if you are using Mu:
You should see the message and the image displayed on the micro:bit and the message “Hello There” should be printed on the REPL.
On the online Python editor this is what it looks like. You press Send to micro:bit to directly send the code to your micro:bit if it is connected, or download the hex file by clicking Save.
Make a change¶
Change the text that is displayed on the screen and make it scroll across the LED display. You can do this by changing the word show to scroll. Don’t forget to save your program and remember to flash the new code to the micro:bit.
You have written your first program. Carry on and see what else you can do with the micro:bit.
Indentations¶
Indentation refers to the spaces at the beginning of a code line. Python uses indentations to show which instructions are inside and outside a block of code, such as a control block. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.
from microbit import *
while True:
display.show('Hello World!')
display.show(Image.HEART)
This example uses a while loop to repeat the the display of “Hello World!” continuously.
The display.show(Image.HEART) line is not indented, so it’s not on the while True loop and will not be executed since the loop will go on forever and will not reach this line.
The most micro:bit Python editors will add indentations for you when it thinks you need one, for example when you press ‘enter’ after typing while True:
Indentations are usually 4 spaces or one tab character. You must be consistent, you can’t use 4 spaces on one line and 3 on another. The code highlighting in the editor helps you check that your indentations line up correctly.
You can indent any lines you have highlighted by pressing the tab key on your keyboard. You can remove indentations by pressing shift+tab.
Syntax error¶
Python will give you an error if you skip the indentation:
from microbit import *
while True:
display.show(Image.HEART)
This is called a syntax error since the code didn’t follow the correct syntax (or format). Look out for other errors along the way as you progress on this course. This will indicate that there is something wrong with the code.
Comments¶
Comments help other people understand and modify your code. They’re useful if you come back to your code a long time after you wrote it. You can also use the # sign to temporarily stop a line from being run when you’re debugging.
You can add comment by putting a # symbol at the start of a line.
from microbit import *
# displays Hello World
display.scroll('Hello World!')
You can also add comments at the end of a line:
from microbit import *
display.scroll('Hello World!') # displays Hello World
Introduction to Variables¶
In Python, variables are created when you assign a value to it:
x = 5
y = "Hello, World!"
Python has no command for declaring a type of variable and it is automatically determined based on the value. You will learn more about variables on a separate chapter.
Exercises¶
- Create a simple program in your micro:bit that will do the following:
- Display your name by scrolling it on the LED
- Pause for 2 seconds
- Display your age
- Pause for 1 second
Use comments to describe the function of each lines of code. Use both the mu editor and the online editor for this.
- Use this code below
from microbit import *
while True:
value = 5
display.show(value)
sleep(200)
Then open the REPL window (or serial console window) and type on the >>>
>>> display.show(4)
What happened on the display in your micro:bit?
- Type the following on the REPL window or serial console window
>>> print(value)
What do you see on the REPL window?
- Add comments on the line describing what is happening on the code.