Microbit
Beginner
45 mins
Teacher/Student led
+85 XP
What you need:
Chromebook/Laptop/PC
Microbit

Building a Digital Dice

In this exciting lesson, you will learn how to build a digital dice using Microbits! Through this project, you will practice your coding skills and create a fun, interactive game tool.

Teacher Class Feed

Load previous activity

    1 - Creating a New Project

    Start by creating a new project. Visit the Microbit coding website at https://makecode.microbit.org/ and click on 'New Project'. Give the project a suitable name, such as 'Digital Dice'.

    2 - Create a 'roll' variable

    We will need a variable to store what number we roll. Create a variable called roll and we will use this in the next step.

    3 - Coding the Dice Shake

    In this step, you will code the Microbit to generate a random number when it's shaken. This simulates the action of shaking a dice to get a number. Add the following code:

    input.onGesture(Gesture.Shake, function () { let roll = Math.randomRange(1, 6) })

    This code uses an input event tied to the 'Shake' gesture, and generates a random number between 1 and 6, storing it in the variable 'roll'.


    4 - Displaying the Dice Roll

    Next, you will code the Microbit to display the number that was generated from the dice roll. Add the following code:

    input.onGesture(Gesture.Shake, function () { 
    let roll = Math.randomRange(1, 6) 
    basic.showNumber(roll) 
    })
    

    This code extends the previous one by using the 'showNumber' function to display the 'roll' variable on the Microbit's LED matrix.


    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 - 2026. 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