This project turns the BBC micro:bit into an electronic dice, programming the device to display a random number from 1-6 on the LED array when shaken.

What you'll need

1 x BBC micro:bit

1 x Micro USB

1 x Computer or tablet

2 x AAA Batteries & Battery Holder

Project walkthrough

• From the Input commands menu, drag in an 'if [shake] do' tile.

• When the device is shaken, we want it to display a random number from 1-6. To do this, first we need to create a new variable.

• Attach a 'set [item]' tile, and rename the variable to 'roll'.

• Now go to the Math command menu and attach a 'pick random [0] to [4]' tile. Change the figure in the dialog box to 5, like so:

• Because the roll has a range of 0-5 rather than 1-6, we have to tell the device to show the number one when it rolls a zero, the number two when it rolls a one, and so on.

• Drag in an 'if/do' tile and, for 'if', attach a '[0] = [0]' tile from the logic menu. Drag a 'roll' variable into the left dialogue box.

• For 'do', attach a 'show leds' tile and set it to display the 'one' side of a standard dice by lighting up a single bulb at the co-ordinate 2/2.

• Click on the blue cog symbol in your 'if/do' tile and add an 'else if/do' option.

• Use this to program your BBC micro:bit to display the 'two' side of the dice when the device rolls a one.

• Using more 'else/if' and 'else' tiles, repeat the steps above so that when the roll is 2, 3, 4 5, the device shows a 3, 4, 5 and 6 dice side respectively.

• You're done! Run your program to test it in the right-hand emulator, then click compile to download the script and transfer it to your BBC micro:bit.

When your students have mastered this project, what else can they do with the random command? Challenge them to develop different projects using the LED array, such as a dice that displays images rather than numbers, or a 'trick dice' that only rolls even numbers. Let us know what they come up with in the comments section below...