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

Microbit Dice

In this step-by-step lesson, you'll create a new Microbit project and programme it to display a random number between 1 and 6 when shaken, emulating a dice. You'll learn how to detect a shake, show a number, and finally, show a random number.

Teacher Class Feed

Load previous activity

    1 - Create a new Microbit project

    Go to the makecode.microbit.org website and create a new project.

    2 - Detect a shake

    We are going to program the Microbit to show a number between 1 and 6 when you shake it.

    First let's detect a shake, add the following code to your project.

    input.onGesture(Gesture.Shake, function () {
    	
    })

    3 - Show a number

    Next let's show the number 0 when you shake it.

    Add the show number 0  inside the on [shake] block.

    input.onGesture(Gesture.Shake, function () {
        basic.showNumber(0)
    })

    4 - Show a random number

    Now let's show a random number between 1 and 6, just like a dice.

    Put a pick random 1 to 6 inside the  show number 0  block.

    input.onGesture(Gesture.Shake, function () {
        basic.showNumber(randint(1, 6))
    })


    5 - Microbit

    That's it you've completed the project! If you do have a Microbit you can send your code to it and use it as a dice.

    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