You can already drag blocks together and run a program. Today we step up to hardware: we'll code a micro:bit to read its built-in sensors — temperature, light, sound and movement — and show the readings on the LED screen.
This is the first part of a weather-station project we'll build over a few lessons. We'll predict first, then build it step by step, run it, and fix anything that misbehaves.
Open the lesson and set the scene: today we move from screen-only code to a real device that senses the world. Ask students what kinds of things a weather station measures (temperature, light, sound) to surface prior knowledge. Have devices logged in and the editor open. Differentiation cue: reassure less confident students they'll work in pairs and can use the simulator.
Before anyone runs anything, look at what we're about to build. The program will use the buttons to switch between different sensors and show their readings.
Commit to a prediction: What will you see on the LED screen first when the program starts? What do you think will happen when you press button A? Share your guess — we'll come back to it.
Run the PRIMM predict beat before any building. Ask: 'What will appear on the screen first? What happens when you press a button?' Collect two or three predictions and write them on the board to revisit later. Keep it quick — the value is in committing, not in being right.
Start by creating a new project. Navigate to MakeCode for Microbit and click on 'New Project'. Give your project a name, such as 'Weather Station'.
Model creating a new project on the board and naming it, so everyone starts from the same clean slate. Key question: 'Why give a project a clear name?' Watch for students who skip naming and later can't find their work. Support cue: walk round and confirm each pair has the editor open and a named project.
First, we need to declare and initialize our two main variables: 'mode' and 'reading'. 'Mode' will help us determine which sensor's data we want to display, and 'reading' will store the sensor data. Add the following code:
let reading = 0 let mode = 0 mode = 1
The following are the different modes we will have:
Model declaring the two variables on the board. Key question: 'What is the job of mode versus reading?' Make clear mode is a label for which sensor we want, and reading holds the value. Common slip: students set mode to the wrong starting value or mistype a variable name. Extension cue: ask why we set a starting mode at all.
Let's start by configuring Button A. When this button is pressed, we want 'mode' to be 1. Add the following code:
input.onButtonPressed(Button.A, function () {
mode = 1
})
Demonstrate adding the button A handler. Ask: 'When does this code run?' to reinforce event-driven thinking. Bug to watch: assigning the wrong mode number to A — readings then look swapped. Encourage a quick test of A before moving on.
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.