Microbit
Beginner
40 mins
Teacher/Student led
+130 XP

Sensing with a Micro:bit

Students program a micro:bit to read light, temperature, sound and movement sensors and show the readings on the LED screen, then test and tune so the values make sense.

Teacher Class Feed

Load previous activity

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

    Illustration for Start: what we're buildingToday 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.

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

    3 - Initialize Variables ~5 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).

    4 - 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
    })

    5 - Configure Button B ~3 mins

    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
    })
    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