You're previewing the student experience. You don't need to complete this lesson — your progress and XP aren't saved.
Microbit
Beginner
40 mins
Teacher/Student led
+195 XP
What you need:
Chromebook/Laptop/PC
Microbit

Microbit Light Clapper

Follow this guide to build a project with your Microbit that turns LED lights on and off with a clap. Start by creating a new project, setting up variables, detecting sound, and testing the code on your device.

Teacher Class Feed

Load previous activity

    1 - Create a new project

    Go to the makecode.microbit.org website and create a new project.

    2 - Create a variable

    In this project we are going to detect when you clap using the microphone in your Microbit. When we detect a clap we will turn on the LED lights of the Microbit or turn them off if they're already on.

    To decide whether to turn them on or off we will need a variable to store if they are currently on or off.

    Create a variable called lightOn. and then add the following code to set this variable to false at the start.

    let lightOn = false

    3 - Sound threshold

    For detecting the clap, we need to set how loud a sound we need to listen for. To do this we use the set [loud] sound threshold to block.

    Add this block into the on start block and set the value to 128 (the range in 0 to 255).

    let lightOn = false
    lightOn = false
    input.setSoundThreshold(SoundThreshold.Loud, 128)

    4 - Detect a clap

    Now to detect the clap we will use the on [loud] sound block.

    Add the following code.

    input.onSound(DetectedSound.Loud, function () {
    	
    })

    5 - Add the if then else

    Next add an if then else block inside the on [loud] sound block and check if "not lightOn".

    If the light is not on then we will turn it on else we will turn it off.

    input.onSound(DetectedSound.Loud, function () {
        if (!(lightOn)) {
        	
        } else {
        	
        }
    })

    6 - Turn it on

    Now add the following new code to turn on the LED lights and set the lightOn variable to true.

    input.onSound(DetectedSound.Loud, function () {
        if (!(lightOn)) {
            basic.showLeds(`
                # # # # #
                # # # # #
                # # # # #
                # # # # #
                # # # # #
                `)
            lightOn = true
        } else {
        	
        }
    })

    7 - Turn it off

    Now add the following code into the "else" part. This will turn off the LED lights and set the lightOn variable to false.

    input.onSound(DetectedSound.Loud, function () {
        if (!(lightOn)) {
            basic.showLeds(`
                # # # # #
                # # # # #
                # # # # #
                # # # # #
                # # # # #
                `)
            lightOn = true
        } else {
            basic.clearScreen()
            lightOn = false
        }
    })

    8 - Test it in the simulator

    That's all the code added so test that it works in the simulator by dragging the sound level up and down.

    When it goes over 128 it should turn the lights either on or off depending on the lightOn variable.


    9 - Send the code

    Finally download and send the code to your Microbit and test that it works by clapping.

    You may need to adjust the value in the set [loud] sound threshold to block.

    End of lesson
    123learn · Sample lesson

    That was a sample lesson from Applied Microbit Programming

    Every lesson in the course is ready to teach like this one. See what else comes with it, and how to order it.

    Copyright Notice
    This lesson is copyright of 123Learn.org 2017 - 2026. 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