In this tutorial, we’ll take the first step in making your micro:bit do amazing things by learning how to write your very own “When-Do” rule in MicroCode. As you learned in the previous tutorial, these rules are the fundamental building blocks of all MicroCode programs – they tell your micro:bit what to and and when to do it.

Materials

Instructions

Step 1: Start a New Project

Power on CodeCTRL to load MicroCode.

Then, use the joystick to select ‘Load’ to open a blank save file. Think of this as opening a new document to start your coding project.

Step 2: Choose the “When” event

Hover over the leftmost empty tile and press ‘O’ to open a menu of all the cool things that can trigger an action – these are called events.

MicroCode screenshot. Leftmost block of a rule in MicroCode is highlighted. The text below the block reads 'When'.

Select the event of your choice. In this tutorial, we’ll start with the ‘light’ event, which lets your micro:bit respond to changes in light levels.

MicroCode screenshot. An open menu of all the events available in MicroCode. These include: button pressed, button released, accelerometer, sound, temperature, light sensor, radio receiver, page start, time, variable X, variable Y, variable Z, wall, and line. Light sensor option is highlighted.

Step 3: Set the “When” parameters

Select the blank tile next to your chosen event. This will open a menu where you can fine-tune how the event works. These are called parameters.

MicroCode screenshot. The user has selected the blank tile next to the chosen event.

Select from the list of parameters relevant to your chosen event.

  • 1 dot: low light
  • 3 dots: medium light
  • 5 dots: bright light
MicroCode screenshot. Parameter options for the light sensor include: 1-dot, 2-dots, 3-dots, 4-dots, 5-dots.

We’ve now programmed the first half of our rule to say: “When the light level is bright”.

Step 4: Add the “Do” command(s)

Move to the “Do” section of the rule. This is where we’ll tell the micro:bit what to do after the event is triggered.

Select the first empty tile to open a menu of all the actions your micro:bit can perform – these are called commands.

MicroCode screenshot. The user has selected the first block in the 'Do' part of the rule.

Select the command of your choice. In this tutorial, we’ll use the “show image” command to display something on the micro:bit’s LED screen.

MicroCode screenshot. The 'Do' menu has the following options: show image, show number, sound, play notes, radio send, radio set group, switch page, set variable, robot. The 'show image' option is highlighted.

Step 5: Select the “Do” parameters

Just like events, commands can also have parameters to control exactly how they work. Select the blank tile next to your new command.

MicroCode screenshot. The user has selected the block next to the 'show image' in the rule.

Then, select from the list of parameters relevant to your chosen command.

MicroCode screenshot. Parameters for 'show image' include: LED image or repeat. The user has highlighted LED image.

Since we chose “show image,” you’ll now see the LED image editor. This is where you get to draw! Click on the LEDs to turn them on or off and create your own icon or picture. Have fun with it!

We’ve now completed a rule that says: “When the light level is bright, show a sun on the LEDs!”

Step 6: Go back to the main editor

Press ‘X’ to return to the main editor. You’re almost ready to see your code in action!

Step 7: Test your new rule

Shine a bright light on your micro:bit to trigger your rule and see what happens! The best part of programming the micro:bit with MicroCode is that you’ll see your new rules take effect immediately. This instant feedback makes learning and experimenting super easy and fun.

Step 8: Save your program

To keep your awesome creation, save your program by clicking on the Drive icon, located in the top left of the editor.

MicroCode screenshot. 'Save' button in top left of MicroCode editor is highlighted.

Then, choose which of the three save files you want to use.

MicroCode screenshot. There are three save files to choose from in MicroCode. The user has selected option 1.

Step 9: Add or delete rules

You can add more rules to make your micro:bit do even more complex things, or delete rules you don’t need. Just select the box next to a rule to add a new one or delete it!

Challenge: Try adding a new rule to change the display when there is low light!

Next Steps

Feeling Stuck? Explore the Knowledge Base for common questions and troubleshooting tips.