Microbit
Beginner
40 mins
Teacher/Student led
+55 XP

Studio: Digital Dice Roller

Students build a shake-to-roll digital dice in Python on the micro:bit, using lists, random selection and a function, then test that all six faces come up fairly.

Teacher Class Feed

Load previous activity

    1 - Start: What We're Building ~4 mins

    Illustration for Start: what we're buildingToday you are coding in Python. You will build a Digital Dice Roller on the micro:bit: shake it (or use the simulator) and a random face from 1 to 6 appears on the LEDs.

    You will predict first, then build, run and fix, step by step at your device.

    2 - The Plan: Four Parts to Build ~3 mins

    The Digital Dice Roller has four parts:

    1. Dice faces stored as images.
    2. A shake the micro:bit can detect.
    3. A short rolling animation so it feels like a roll.
    4. A random face chosen and displayed.

    At the end you wrap it in a function, so one named piece of code does the whole roll and you can call it again.


    3 - Import Modules ~2 mins

    In this step, we'll import the necessary modules. We need the microbit module for hardware access, including the accelerometer and display, and the random module to generate a random dice roll.

    Start by adding the following complete code to your editor:

    from microbit import *
    import random
    This imports everything from microbit and the random module. Run the code; nothing will happen yet, but it should run without errors.
    If you see any import errors, check your spelling and ensure you're using the online editor.

    4 - Define Dice Faces ~5 mins

    Next, we'll create a list to store the dice faces as images. Each face is an Image object representing the dots for numbers 1 through 6 on a dice. Lists are perfect for holding these related items, and we'll use this list to select a random face later.

    Update your code to the following complete version:

    from microbit import *
    import random
    
    dice_faces = [
        Image('00000:' '00000:' '00900:' '00000:' '00000'),  # 1 - single dot in the center
        Image('90000:' '00000:' '00000:' '00000:' '00009'),  # 2 - dots in top-left and bottom-right corners
        Image('90000:' '00000:' '00900:' '00000:' '00009'),  # 3 - dots in top-left, center, and bottom-right
        Image('90009:' '00000:' '00000:' '00000:' '90009'),  # 4 - dots in top-left, top-right, bottom-left, and bottom-right corners
        Image('90009:' '00000:' '00900:' '00000:' '90009'),  # 5 - dots in four corners and the center
        Image('90009:' '00000:' '90009:' '00000:' '90009')   # 6 - three rows of two dots each (left and right positions)
    ]
    
    display.show(dice_faces[0])
    Run the code. You should see the image for '1' on the display. This confirms the list is set up. The list dice_faces holds six Image objects.
    Try changing the index in display.show(dice_faces[3]) to show '4'. This practises accessing list elements. Note: Images are strings representing brightness levels (0-9) for each LED in the 5x5 grid.
    To save time, it's ok to copy and paste this code but make sure you understand what it is doing.

    5 - Detect Shake Event ~3 mins

    Now, let's detect when the Micro:bit is shaken using the accelerometer. We'll use a loop to continuously check for the 'shake' gesture.

    Update your code to this complete version:

    from microbit import *
    import random
    
    dice_faces = [
        Image('00000:' '00000:' '00900:' '00000:' '00000'),  # 1
        Image('90000:' '00000:' '00000:' '00000:' '00009'),  # 2
        Image('90000:' '00000:' '00900:' '00000:' '00009'),  # 3
        Image('90009:' '00000:' '00000:' '00000:' '90009'),  # 4
        Image('90009:' '00000:' '00900:' '00000:' '90009'),  # 5
        Image('90009:' '00000:' '90009:' '00000:' '90009')   # 6
    ]
    
    while True:
        if accelerometer.was_gesture('shake'):
            display.scroll('Rolling!')
        sleep(100)
    Run the code. Shake the virtual Micro:bit in the simulator, and you should see 'Rolling!' scroll on the display. The loop continuously checks for the shake gesture.
    This uses the accelerometer's was_gesture method, which is a control structure for event detection. Test it multiple times.

    123learn · Online learning platform

    Unlock the full learning experience

    You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.

    Hundreds of curriculum-aligned lessons
    Interactive activities in every lesson
    Printable resources & progress tracking
    Copyright Notice
    This lesson is copyright of Coding Ireland 2017 - 2025. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more