Microbit using the accelerometer in the block editor

In this next example using the block editor we will use the accelerometer that is on the Micro:bit board  to detect when the micro:bit is being shaken, if it is shaken we will display a number between 1 and 6 on the LED’s, similar to a dice.

The pick random block generates numbers in a range starting at 0. If we generate the numbers 0-5 we then need to add 1,and then we get a die roll from 1 to 6.

You need to create the following


Compile, copy the hex file into your Micro:bit and test by shaking your Micro:bit and looking at the LEDs

Of course that’s not really a proper die, so what you really want to do is depending what number die is equal to display an image that represents that on the LEDs, here is part of an example. I couldn’t fit all of the numbers in but should get the general idea from the image below, a bit harder to create but the effect is much better


Microbit touch example using block editor

The pins on your Micro:bit that are labelled 0, 1 and 2 can be used as touch inputs. To do this you need to touch the ground pin with one finger and one of the 3 pins with another.  A better method of testing is to use alligator leads and connect one end to an input and the other to a metallic object such as a coin.

We will be using the Block editor here – go to https://www.microbit.co.uk/create-code and select the Microsoft block editor

Here is an image of what you will be creating



Click on the compile button and this will generate a hex file on your computer, copy and paste this into your Microbit which if connected will show up as a drive on your computer.

Now touch the ground with one finger and the 3 pins and you should see the number displayed on the Micro:bit LEDs

LED pattern using the Microsoft Block Editor for Micro:Bit

Go to the micro:bit web site and follow the hyperlink to Create Code. Choose the Microsoft Block Editor and make a New Project.

Look in the Basic section for the show leds block.


Drag it into the code window and click on some of the check boxes to make a pattern. Here is our example pattern


Click the Run button in the browser and you can see a preview of the image on the right hand side of the page.


Simulations are fine but we want to see this on our Micro bit, Click on the Compile button to create a .hex file for the micro bit. You will be prompted to download the file, you can either download to your computer or straight to your Micro Bit.

The Micro Bit is similar to the MBed system in that it has an online compiler which creates hex files that you save to your computer or can save to your device as it appears as a drive on your computer, I save to my computer and drag it into my Micro Bit’s drive – just my preference

Once complete you should see the pattern on your Micro Bit