Science Oxford Logo

CCC - Ready to Roll

Resources for the 'Ready to Roll' Creative Computing Club.

During this workshop, you will create electronic dice with a BBC micro:bit.

This page recaps what we discussed as a group.

To skip to the examples and ideas, click here.


Creating your first micro:bit program

Click here to find the starter program, with the imports ready.

Open the Mu editor on your computer. If there is code on the screen, start a New program from the menu.

Mu menu, highlighting 'New'

Type these lines of code into your blank window:

from microbit import *
from random import randint, choice
import radio

display.show(Image.HAPPY)

Plug your micro:bit into the computer, then click Flash:

Mu menu, highlighting 'Flash'

Some code should be running on your micro:bit!

If you get a message scrolling across the micro:bit's screen, this is an error message!

Debug your code, then click Flash again to test your changes.

Once your code is working - change it!

Instead of HAPPY, can you think of another Image the micro:bit could show on its display?


Displaying a random number

When you roll a die, you don't know what number is going to come up.

To add this feature into our program, we need to use the random library.

This has already been imported into your program, with this line:

from random import randint, choice

For now, we will use randint, which means create a random integer (whole number).


Edit your code so that instead of showing an Image on the micro:bit's display, you show an integer.

display.show(5)

Now we want to simulate the rolling of the die - by using a variable called roll, and setting this to a random integer.

Add this into your code above display.show().

roll = randint(1, 6)

Finally, change your display.show() line, so that the value of the variable is shown instead.

Your code should now look like this:

roll = randint(1, 6)
display.show(roll)

Test it out - flash the code to your micro:bit and see what number you get.

Press the reset button on the back of the micro:bit to restart your code - which number do you get now?


Using loops

Loops allow the micro:bit do do things more than once.

To start with, we will get the micro:bit to roll itself, forever.

Above the two lines that choose and display a random integer, add this new line:

while True:

This is Python syntax for forever.


To put things inside this forever loop, we need to indent them - this means using the tab or space key on your keyboard to add spaces before the lines.

Your code should look like this:

while True:
    roll = randint(1, 6)
    display.show(roll)

Try out this code - what do you notice?

Computers are very quick - when running this code, the number will change so quickly that your eyes can see it! This is why it is flickering.


To fix this, we need to slow it down - telling the micro:bit to pause after each number.

Add this line to the bottom of your code:

    sleep(5000)

Choosing when to roll

Right now, the micro:bit rolls by itself - but you can choose when to roll it.

Between while True: and roll = randint(), add this new line:

    if button_a.was_pressed():

Then, the same as we did before, indent all of the lines of code that you want to happen if you press the button.

Your code should look like this:

while True:
    if button_a.was_pressed():
        roll = randint(1, 6)
        display.show(roll)
        sleep(5000)

If you would like the number to be cleared from the screen when you are not rolling, expand the code like this:

        ...
        sleep(5000)
    else:
        display.clear()

Challenge! Can you make the micro:bit roll when you shake it, instead of when you press the button? The syntax you will need is:

accelerometer.was_gesture('shake')

Controlling other dice

For this section, you will need a second micro:bit.

Download this code and flash it to your second micro:bit.

If you are using a micro:bit v1 the use this code instead!

Unplug this second micro:bit, attach a battery to it, and leave it to one side.


Plug your original micro:bit back in, and add the following code to your program, above the while True loop.

radio.on()
radio.config(channel=7)

If you are in our workshop, replace channel=7 with the number of your computer.


Inside your while True loop, before the sleep() , add this new line:

        radio.send(str(roll))

This converts the dice roll into the right format str(), then sends it over the radio.

What happens when you roll your die now? Look at the second micro:bit.


What next?

You have a basic die now, what do you want to add next?

The links below give you some other ideas of things you could add to your project.