Python:
Hello World
Knowing the micro:bit
Front
- 5x5 grid of LEDs – used as a display; each light can be programmed to be on or off and have varying intensities of light
- A & B Buttons – buttons that can be programmed to respond to a press or a release
- 3 Input/Output (I/O) rings – to attach sensors and send signals to/from the Micro:bit
- 3V and GND rings – to provide power to the external devices that use I/O pins
Back
- Antenna – for Bluetooth connection with other devices
- CPU – to run programs; acts as the “brain” of the Micro:bit
- Micro-USB – to attach to computer; transfers data and power to Micro:bit
- Battery Connector – to attach battery pack for portable power
- Accelerometer – detects shaking, tilting, and other motion of Micro:bit
- Compass – detects magnetic fields and direction Micro:bit is moving/facing
- Edge Pins – allow users to slot Micro:bit into edge connector for greater I/O capabilities
- Reset Button – press to restart the Micro:bit and its current program
The "Hello World" Program
The program is believed to have first been used in 1974 by Bell Labs technicians as they were trying to show their counterparts how the computer can be programmed to interact with humans. Since then, it has become widely accepted as the first program new programmers use to learn a new programming language or test a new device.
“Hello World” is widely used for its simplicity, to familiarize users with the coding environment they will be using, and to introduce printing (a useful tool in every programming language).
The Code
# Hello World and Scrolling Text from microbit import *
while True: display.scroll(‘Hello, World!’) display.show(Image.HEART) sleep(2000)
The very first line of the program is a comment. If a line of code begins with the '#' sign, the program will skip over it when running. This can be etremely handy for making notes for yourself and others while writing a program, but can be deleted without affecting the program.
The second line of code will be in every program you write for the micro:bit. It instructs Python to import the library of code pre-existing for the micro:bit. This allows the use of several commands and functions that are specific to the micro:bit.
After an empty line, the program contains while True:. This is a tool known as a while loop because the code loops "while" the test case is true. In our program's case, True is always true, so the code included in the while loop will repeat forever. To see this line in action, have students remove it and the indents to the left of the next three lines so the cod will only execute once.
The next line begins with the display. This instructs the program that the next bit of code is for the LED display on the micro:bit. scroll() is a command for the display that allows a string of characters to scroll across the LED display from left to right. "Hello, World!" is the string characters the program sends to be displayed.
display.scroll('Hello, World!')
Following hello world is use of the command display.show(), which instructs the micro:bit to show a pre-programmed image on the display. In this case, the image is a heart.
Finally, there is a line of code that tells the program to sleep or pause its executing for a specified amount of time. This program uses 2000 miliseconds, or two full seconds. This will cause the program to pause on the picture of the heart before it starts again with the while loop.