Microbit
Beginner
40 mins
Teacher/Student led
195 points
What you need:
Chromebook/Laptop/PC
Microbit

Microbit Light Clapper

In this step-by-step lesson, you'll create a Microbit project that responds to clapping sounds. You'll learn to create a variable, set a sound threshold, detect a clap, and use conditional statements to control the Microbit's LED lights. Finally, you'll test your project in a simulator and on your own Microbit.
Learning Goals Learning Outcomes Teacher Notes Lesson Files

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
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 {
        	
        }
    })

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. 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