micro:bit MakeCode Tutorials: Project 2 of 5

ages 11+ ages 7+ ages 8+ ages 9+ BBC micro:bit BBC microbit BBC microbit v2 Classroom coding digital technologies display educational electronics light sensors makecode microbit tutorials wireless

This is a series of projects that grew from an introductory set of group workshops that we wrote for a local school's open day to promote their Code Club. Projects 1 to 3 ran for about 20 minutes each, and projects 4 and 5 became workshops at the Code Club where I volunteer (these can each be completed within an hour). These projects were easily completed by kids from ages 7-11. The projects that use multiple micro:bits are scaleable and can work well with any number of micro:bits from 2 to class sets; with the option for kids to collaborate in pairs or larger groups.

Project 2: Shake a Number

There are sensors in the micro:bit that can detect when it is tilted. In this project, we will show different patterns on the micro:bit when it is tilted left and right.

In this project, you will learn:

  • How to perform an action based on an event such as shaking your micro:bit
  • How to generate a pseudo random number on your micro:bit (pseudo random means a number as random as can be comfortably created with the resources available on our device)

You will need:

  • A micro:bit (V1 or V2 is fine) and matching USB cable
  • A computer with an Internet connection and a modern web browser

Step 1: Launch your browser, and got to the MakeCode website

  • Launch your web browser by clicking on its icon
  • Once your web browser loads, click in the address bar at the top and, in the address bar, type in: https://makecode.microbit.org

Step 2: Write your code

  • Click on Input and drag across the on shake block - this will let us do something when the micro:bit is shaken
    MakeCode `on shake` event block
  • Click on Basic and drag the show number block into the on shake block. From Math drag the pick random 0 to 4 block into the show number block. Now change the 4 to a 6 by clicking on it and typing 6.

    Your code should look like this:
    MakeCode block code to generate a random number between 0 and 6 when the microbit is shaken

Step 3: Copy your code to the micro:bit and see it run

  • Hold your micro:bit carefully by the edges only, and plug in the micro:bit into the USB port on the computer. Ask for help if you don’t know how to do this. In the bottom left corner, click on the Download button.

    MakeCode Download button to be clicked to download your code onto the microbit over USB or bluetooth

  • Find the MICROBIT folder and save the .hex file there. You’ll see a light on the micro:bit flash and stop flashing. When it stops, your code will run. Shake your micro:bit to generate a number between 0 and 6.

    Congratulations! You've written another program on the micro:bit!
    microbit displaying a random number on the LED matrix (simulator version)

Continue to the next project in this series here..
    Creative Commons Licence
    This tutorial is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

    Older Post Newer Post