Microbit
Beginner
50 mins
Teacher/Student led
+140 XP

Sensing with a Micro:bit

Students program a micro:bit to read its built-in sensors (temperature, light, sound, movement) and display the readings, then test and tune so the values make sense. Part 1 of a three-part weather-station project.

Teacher Class Feed

Load previous activity

    1 - Start: What We're Building ~5 mins

    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.

    2 - Predict Before You Run ~10 mins

    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.

    3 - Create a New Project ~3 mins

    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'.

    4 - Initialize Variables ~3 mins

    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:

    1. displays the temperature (press A).
    2. displays the light level (press B).
    3. displays the sound level (press A and B together).
    4. displays movement (switches on when a loud sound is detected).

    5 - Configure Button a ~3 mins

    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
    })
    123learn · Online learning platform

    Unlock the full learning experience

    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.

    Hundreds of curriculum-aligned lessons
    Interactive activities in every lesson
    Printable resources & progress tracking
    Copyright Notice
    This lesson is copyright of Coding Ireland 2017 - 2025. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more