SparkFun Inventor's Kit for micro:bit Experiment Guide
Experiment 0: Hello, micro:bit!
Introduction
"Hello World" is the term we use to define that first program you write in a programming language or on a new piece of hardware. Essentially it is a simple piece of code that gives you a quick win (fingers crossed) and a first step in learning. It also gives you a chance to make sure everything is up and running and A-OK.
Parts Needed
You will need the following parts:
- 1x micro:bit
- 1x Micro B USB Cable
Running Your Script
For your first "Hello World" we are going to create a simple animation on the LED array that repeats forever. If you just want the complete program, you can see it here. To see a step-by-step explanation of how we built the program, continue reading!
Either copy and paste, or re-create the following code into your own MakeCode editor by clicking the open icon in the upper right-hand corner of the editor. You can also just download this example by clicking the download button in the lower right-hand corner of the code window.
Building 'Hello World'
A "Hello World" on the micro:bit is a little different than on a normal run-of-the-mill microcontroller. The micro:bit has no single LED to blink on its own, as you would find on the Arduino or similar boards. What the micro:bit does have is an LED array! So, the "Hello World" for the micro:bit is to draw something using the LED array!
When you open MakeCode you are greeted with two blocks: the On Start
block and the forever
block. The On Start
block is all of your code that will execute at the very beginning of your program and only run once. The forever
block is code that will loop over and over again...forever.
We are going to use the forever
block for building this "Hello World." We now need to start adding blocks to forever
.
First, click on the Basics category. These blocks are, well, the basic building blocks of a BuildCode program. It will expand into a number of options. Click and drag the show leds
block over and place it inside of your forever
block. Notice that the block is keyed to fit inside of the forever
block, and if you have the volume up on your computer you will hear a satisfying 'click' noise when you let go of the block.
The show leds
block has an array of squares that symbolize the LED array. If you click on a square, it will turn red, which means that it is on. Draw a simple pixel art shape by turning different LEDs on or off; you should be able to see the outcome in your simulator on the lefthand side of your window.
To turn this static image into an animation, we need another show leds
block to place just under the first block. You can then make a second drawing with this set of rectangles. In your simulator you will see the images switching really, really fast. We need to slow this down!
To slow your animation down, you will use the pause
block, which is under the basic block set. The pause
block is just what it says; it tells the micro:bit to pause and wait for a certain amount of time. Place two pause
blocks in the program as shown.
The reason we are using two and placing one at the end is that this program is a loop. Without the block at the end, the image in your animation will change really, really fast.
We have built up an example in the next section where you can download the file and try it out on your own micro:bit, or use the simulator. If you want to play around with the code and make some changes, go ahead and click the Edit button in the widget, and it will open a MakeCode editor for you to start hacking "Hello World." Enjoy!
Download Your Program
Either copy and paste, or recreate the following code in your own MakeCode editor. You can also just download this example by clicking the download button in the upper righthand corner of the code window.
This will download your program file to your standard download location, probably the Downloads folder on your computer, or whatever location you have set in your download preference.
You then simply click and drag your program file from its download location to your micro:bit drive, which shows up as an external device.
If you have not already, insert the USB cable into the micro:bit. Once it shows up as a drive, drag and drop the file into the micro:bit. That's it!
Your micro:bit will flash for a few seconds, and then your program will start automatically. Yes! Win!