Today you are coding with the micro:bit. You will build a small weather-station style program that reads sensors and shows the values on the LED screen.
You will predict, then build, run and fix as you go. By the end you should see temperature, light, sound and movement readings change when you press the buttons or make a loud sound.
Keep this short. Devices ready, students logged in, MakeCode for micro:bit available. Optional predict before anyone builds: ask what they think will appear on the LEDs when a sensor is read, and park one or two ideas to revisit later. Do not ask them to open saved work; this is a fresh start. Then send them to create the new project.
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 and naming it clearly, for example Weather Station. Watch for students opening an old project by mistake. Pair stronger navigators with anyone new to MakeCode. Key question: is everyone on a blank project ready for blocks?
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 reading and mode, and setting mode to 1. Stress that mode is just a number that will choose which sensor to show later. Common bug: forgetting to set the starting mode, so nothing sensible shows until a button is pressed. Differentiation: let some pairs only set mode at first and add reading when they reach the loop.
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 the Button A handler setting mode to 1. Ask: what should happen when A is pressed once the forever loop is in place? Watch for handlers placed inside the forever loop by mistake. Keep pairs moving; this block is short.
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
})
Same pattern as Button A. Ask students to predict the difference between mode 1 and mode 2 before they add Button B. Common slip: both buttons set the same mode number. Quick fix on the spot: read the mode values aloud against the planned list.
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.