Programmers and technologists build tools that sense the world and turn what they sense into data — think of weather apps, fitness trackers or the sensors in a smart thermostat. Today you become that kind of technologist. You'll code a micro:bit into a mini weather station that can read the temperature, the light level, the sound and even movement.
Watch the board first as we build each piece together, then try it on your own device and run it to see what your sensors pick up. By the end you'll gather a few real readings and figure out what they tell you.
Set the scene: devices should be logged in to MakeCode. Explain that you'll model each piece on the board first, then pupils build it in pairs and run it. Ask: <em>What everyday gadgets sense the world around us?</em> Keep this brief and move into the build.
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 IWB and naming it. Watch for pupils who stay in an old project or forget to name it — a quick, clear name helps them find their work later. Remind pairs to agree who drives first and to swap.
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 adding the two variables on the board and say plainly what each is for: one decides which sensor to show, one holds the reading. Common confusion: pupils may not see anything happen yet — reassure them this is set-up and the display comes later. Ask: <em>Why might a program need to remember a value?</em>
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
})
Model wiring up Button A on the board. Watch for the button block not being fully snapped in. Ask: <em>What do we want to happen when someone presses A?</em> For support, have pairs test just this button before moving on.
Next, let's configure Button B. When this button is pressed, we want 'mode' to be 2. Add the following code:
input.onButtonPressed(Button.B, function () {
mode = 2
})
Repeat the pattern for Button B, letting a confident pair predict the code before you show it. Bug to watch: pupils placing the new block inside the Button A block by mistake — check the two are separate.
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.