Digital Technologies

micro:bit — code the hardware

Pick a topic on the left, or tap a box on your assessment sheet to open that tutorial.

Your assessment sheet

Tap a box
Tutorial
Developing
Consolidating
Extending
1: Control the Light + Sunshine Micro:bit
Control: Sun: Upper:
2: Smart Guard + Ultrasonic Meter
Smart: Ultra: Middle:
3: Rainbow on the Desk
Lower:
4. Variables (class activity)

Work down the yellow boxes first, then follow the arrow

Topic

Hardware

A micro:bit is a simplified version of a computer that you can program, provide input to, and receive output from.

Look at the board

What do you notice? With this small device you have a simplified version of a keyboard and a monitor. Can you work out where they are?

The front and back of a BBC micro:bit, with the processor, compass, accelerometer, pins, USB, BLE antenna, reset and battery connector labelled.
micro:bit — front and back

Think it through

Buttons A and B take what you press — that is your keyboard. The 5 × 5 grid of red LEDs shows you what the program is doing — that is your monitor.

Peripheral devices

A peripheral device is any piece of hardware you plug into a computer to get information in or out. Here are the ones you will be using with the micro:bits. Each sensor has its name and type printed on the circuit board — check the board itself if you are not sure what you are holding.

Grove shield for micro:bit
Grove shieldThe micro:bit slides in; sensors plug into the numbered ports
Grove red LED module
Red LEDOutput — a single light you can switch on and off
Grove light sensor module
Light sensorInput — measures how bright the room is
Grove ultrasonic ranger module
Ultrasonic rangerInput — measures distance with sound
Grove four digit display module
4-digit displayOutput — shows numbers, like a clock
Grove rotary angle sensor with a black knob
Rotary dialInput — turn the knob to send a changing value
Grove RGB LED strip
RGB LED stripOutput — a line of colour-changing lights

Input or output?

An input device sends information into the computer. An output device takes information out of the computer and turns it into something you can see, hear or hold. A few devices do both.

Your turn — pick a category for each device

0 of 6 sorted

Topic

Code transfer

Your code lives in the browser until you send it to the board. Follow these steps to pair your micro:bit and download straight onto it.

Six steps for transferring code: connect the micro:bit, click the three dots beside the Download button, choose Connect Device, select Next then Pair, choose your micro:bit and select Connect, then press Download.
Pairing and downloading in MakeCode

If pairing does not work

Check the USB cable is a data cable and not a charge-only one, then reload the MakeCode page and try again. You can also choose Download as File and drag the file onto the MICROBIT drive.

Topic

Assessment

This is the sheet you were given. Every tutorial you finish gets signed off on it.

  • As you complete each tutorial, you will need to get the teacher to sign your sheet.
  • If things are getting busy in the class, you will be asked to write your name onto the list on the whiteboard, and your teacher will get to you as soon as possible.
  • Once you have completed the “developing” sections for each tutorial, you can then begin to work through the “consolidating” tasks in the direction of the arrow on the sheet (these get progressively more difficult). Use the main menu to navigate to the instructions.

The three levels

Developing

You are given the code for these.

Consolidating

You need to think a little here.

Extending

The biggest challenge — but you have all the required knowledge!

Your copy of the sheet

Lost your sheet, or want to check the tasks at home? Download it here and print a new one.

Download the assessment sheet DOCX
The micro:bit assessment sheet, showing the tutorial list with Developing, Consolidating and Extending columns and an arrow running from the Control box down and back up to the Upper box.
Micro:bit assessment sheet

Topic

Digital vs analogue

Tutorial 1A (Control the Light) uses a digital signal. Tutorial 1B (Sunshine Micro:bit) is analogue. What is the difference?

A digital signal only has set steps to choose from — on or off, 1 or 0, nothing in between. An analogue signal is a smooth curve that can sit at any value along the way. To store an analogue signal digitally, a computer samples it at regular moments and rounds each sample to the nearest step.

Diagram comparing a smooth analogue wave with sample points against the stepped digital version of the same signal.
The same signal, sampled and stepped

You can see the steps if you zoom in

A digital image is made of a fixed grid of squares. Zoom in far enough and the steps appear.

A game character zoomed in until the individual square pixels are visible.
Digital — zoom in and you find pixels

Real life has no grid. Zoom in on skin and there is always more detail — the steps never appear, because there are none.

A close-up of a face with a magnified circle showing the texture of skin.
Analogue — the detail keeps going

Watch the difference

Digital

Jumps between steps

Each square is either on or off. The mouth snaps open and shut — there is no halfway.

Analogue

Slides through every value

The light passes through every brightness on its way up and back down again.

Watch the explainer video

Tutorial 1A — Developing

Control the Light

Use the two buttons on the micro:bit to turn an LED on and off.

Control the Light parts list: one Grove red LED, one Grove shield for micro:bit and one micro:bit, with the LED plugged into port P0 slash P14.
Parts and wiring — LED into P0/P14

The colour does not matter

A red LED is shown in the diagram, but blue, green or any other colour works exactly the same. Avoid the clear ones though — those are usually light sensors rather than LEDs, and you will need one of those for the next tutorial.

MakeCode steps: start a new project called Control the Light, then build on button A pressed digital write pin P0 to 1, and on button B pressed digital write pin P0 to 0.
The blocks — Input and Pins (under Advanced)

The dark red digital write pin block is hidden in Pins. Click Advanced at the bottom of the block list to reveal it.

Tutorial 1B — Developing

Sunshine micro:bit

Use the LED bar graph on your micro:bit to show the current light level. The stronger the light, the more LEDs turn on.

Sunshine micro:bit parts list: one Grove light sensor, one Grove shield for micro:bit and one micro:bit, with the sensor plugged into port P0 slash P14.
Parts and wiring — light sensor into P0/P14
MakeCode blocks: forever, plot bar graph of analog read pin P0 up to 255, pause 10 milliseconds.
The blocks — bar graph from Led, analog read from Pins

plot bar graph is in the Led section. analog read pin is in Pins, under the Advanced menu.

Tutorial 2A — Developing

Smart Guard

The ultrasonic ranger detects distance. In this demo it checks whether something is too close, and the micro:bit shows a sad face when it is.

From this tutorial onwards you need the Grove blocks. Add the extension to MakeCode before you start.

Four steps to add the Grove extension: click plus Extensions, type grove and press enter, double click the grove extension, then find Grove under Math in the block list.
Adding the Grove extension
Smart Guard parts list: one Grove ultrasonic ranger, one Grove shield for micro:bit and one micro:bit, with the ranger plugged into port P0 slash P14.
Parts and wiring — ultrasonic ranger into P0/P14

The diagram does not show the USB cable. That is assumed — your micro:bit always needs to be connected to power to work.

MakeCode blocks: forever, if Ultrasonic Sensor in cm at P0 is greater than or equal to 10 then show a happy icon, else show a sad icon, pause 100 milliseconds.
The blocks — Grove sensor inside an if/else

Where the blocks live

The purple Ultrasonic Sensor (in cm) at P0 block is inside the Grove category. From Logic you need two blocks: the comparison block (0 < 0) and the if / then / else block.

Tutorial 2B — Developing

Ultrasonic Meter

Read the distance from the ultrasonic ranger and show it as a number.

Ultrasonic Meter parts list: one Grove ultrasonic ranger, one Grove 4-digit display, one Grove shield for micro:bit and one micro:bit. The ranger plugs into P0 slash P14 and the display into P1 slash P15.
Parts and wiring — ranger into P0/P14, display into P1/P15

Two devices, two different ports. Check the ranger is in P0/P14 and the display is in P1/P15 before you download, or the numbers will not appear.

MakeCode blocks: on start, set 4digit to 4-Digit Display at P1 and P15. Forever, 4digit show number Ultrasonic Sensor in cm at P0, pause 100 milliseconds.
The blocks — set up the display once, then read the sensor forever

Watch the variable name

Drag the 4-Digit Display block straight from Grove and it creates the variable 4digit for you. Both blocks must say 4digit — if one of them gets renamed to 4digit2, this will not work. The show number block also comes from Grove; drop the sensor block into its 0 space.

Tutorial 3 — Developing

Rainbow on the Desk

Drive the RGB LED strip with the rotary dial and light up your desk.

Rainbow on the Desk parts list: one Grove rotary angle sensor, one Grove WS2812b LED strip, one Grove shield for micro:bit and one micro:bit. The rotary sensor plugs into P0 slash P14 and the LED strip into P1 slash P15.
Parts and wiring — rotary dial into P0/P14, LED strip into P1/P15

There are 30 LEDs on the strip. You will show rainbow colours along it, then use the rotary angle sensor to make the rainbow flow.

MakeCode blocks: on start, set strip to NeoPixel at pin P1 with 30 leds as RGB. Forever, strip show rainbow from 1 to map analog read pin P0 from low 0 from high 1023 to low 0 to high 360.
The blocks — NeoPixel setup, then a rainbow driven by the dial

Do this first

Add a new extension called Neopixel, or none of these blocks will exist. Drag NeoPixel at pin straight from it and the variable strip is created for you — both blocks must say strip, not strip2. show rainbow is also in Neopixel and looks skinnier until you drop the large map block into it. Both map and analog read pin come from Pins.

Code too small to read? Hold CTRL and press + to make everything on the page bigger. CTRL and 0 puts it back.

Tutorial 4 — Class activity

Variables

A variable is a labelled container the program uses to remember something while it runs. We will build this one together as a class.

Think of a variable as a container with a name written on the side. The container stays put; what is inside it can change at any moment. When your program asks for the variable by name, it gets whatever is in there right now.

The container

A bucket holds water

The bucket does not change. The amount of water in it does — and you can pour more in or tip it out whenever you like.

The value

A box counts its marbles

Every time a marble drops in, the number written on the side changes to match. Ask the box how many it holds and it tells you the number it has right now.

In your code you will make two of these containers, called numberOne and numberTwo. The buttons and the shake will change what is inside them.

Watch the explainer video
A micro:bit sitting in the Grove shield with a USB cable attached, and no sensors plugged in.
Parts — just the micro:bit and the USB cable

No sensors for this one. Everything happens on the board itself: the two buttons, the shake, and the LED matrix.

MakeCode blocks using two variables numberOne and numberTwo: on start sets them to 1 and 2; on button A pressed sets numberOne to numberOne plus numberTwo; on button B pressed sets numberTwo to numberOne times numberTwo; on button A and B pressed resets both; on shake changes each by minus one; and a forever loop that shows a happy or sad icon depending on whether numberOne is under 10, then shows both numbers.
The blocks — two variables, changed by the buttons and the shake

Make the variables first

Create two new variables and name them numberOne and numberTwo. Each one stores a number that gets displayed on the LED matrix. Those numbers are adjusted whenever you press A, press B, or shake the micro:bit.

Code too small to read? Hold CTRL and press + to make everything on the page bigger. CTRL and 0 puts it back.

Consolidating + Extending — Upper

Challenge 1: two tutorials at once

Only after completing all of the tutorials, follow the arrow on the assessment sheet for the consolidating challenges. The extending tasks can be completed in any order.

Consolidating

Combine Tutorial 1a and 1b together, so that they both work at the same time. This means you need to get both segments of code from the first two tutorials and combine them into one code file.

Hint

The only tricky thing is that both tutorials expected you to use the same port (Port P0) for their input/output devices. You cannot use the same port for two different devices at the same time, so you will need to shift one of your devices to another port and adjust your code to match.

Extending

Alter the code so that the light meter can be turned on by pressing the A button on the micro:bit board, and off when the B button is pressed. The LED peripheral device, when on, will indicate that the light sensor is active. The light sensor will then operate as expected, and the array of lights on the micro:bit (the bar graph) will fluctuate as the light levels change — the code from the consolidating task should already achieve this.

The tricky part

When the LED is off, the bar graph will remain static and will not react to any further changes of the light.

Hint

You will need to use a variable to remember which button was last pressed, and incorporate a conditional statement into the forever loop.

Watch the walkthrough video

Consolidating + Extending — Middle

Challenge 2: how sound measures distance

Only after completing all of the tutorials, follow the arrow on the assessment sheet for the consolidating challenges. The extending tasks can be completed in any order.

Consolidating

Explain how the ultrasonic sensor works, either written or verbal. The main questions you will need to answer are:

  1. What does “sonic” mean? (It has nothing to do with a fast-running hedgehog!)
  2. What does “ultrasonic” mean?
  3. How does the sensor actually measure the distance?

If you are really stuck on question 3, look up what echolocation is — that should help you answer it.

Extending

Can you change it from 2 stages of warning to 3? (i.e. <10, 10–30, >30). Add in an extra LED light, and get this light to:

  • Turn on when sensing under 10 cm
  • Flash repeatedly when sensing 10–30 cm
  • Turn off completely when over 30 cm

Hint

Look for the + sign on the conditional statement — this will allow you to add in a third level (if, else if, else).

Consolidating + Extending — Lower

Challenge 3: swapping the sensors

Only after completing all of the tutorials, follow the arrow on the assessment sheet for the consolidating challenges. The extending tasks can be completed in any order.

Consolidating

Replace the rotary dial with a light sensor. Does your program still function? This task requires no change to your coding.

If you look closely at your code you are using an analogue signal input, but it never specified in the code that it had to be a rotary sensor. It did in the tutorial instructions, but not in the actual code. By replacing the rotary sensor with a different analogue sensor, the program can still function happily. It simply does not care, as long as it is receiving the correct type of signal — that is, not digital.

Extending

Both analogue sensors are connected, but only one works at a time.

  • Use Button A to activate the original rotary sensor only, and disable the light sensor.
  • Use Button B to activate the light sensor, and disable the rotary sensor.

Hint 1

You will need to use a variable to remember which button was last pressed, and incorporate a conditional statement into the forever loop.

If you are not sure how to use a variable for this, the walkthrough video shows you how.

Watch the walkthrough video