Skip to content


Clap lights

Beginner | MakeCode, Python | LED display, Microphone | Boolean logic, Selection, Sensors

Step 1: Make it

What is it?

Turn your micro:bit into a light that you can turn on and off by clapping or making any loud sound.


Coding guide

What you'll learn

  • How to switch outputs in response to sensor inputs
  • How to use Boolean logic to make a switch that toggles on and off when triggered by the same event

How it works

  • The program uses a variable called lightsOn to keep track of the light's status: whether it's switched on or off. We're using it as a special kind of variable, a Boolean variable. Boolean variables can only have two values: true (on) or false (off).
  • When the microphone sensor detects a loud sound, the code toggles the value of lightsOn by setting it to be not lightsOn.
  • This means that when you clap, if lightsOn is false (and the lights are off), it becomes true and the program lights the LEDs.
  • If lightsOn was true (and the lights were on), it becomes false and the code switches the LEDs off by clearing the screen.

What you need

  • A micro:bit
  • MakeCode or Python editor
  • battery pack (optional)

Step 2: Code it

1from microbit import *
2lightsOn = False
4while True:
5    if microphone.was_event(SoundEvent.LOUD):
6        lightsOn = not lightsOn
7        if lightsOn:
8  '99999:'
9                               '99999:'
10                               '99999:'
11                               '99999:'
12                               '99999'))
13        else:
14            display.clear()
15    sleep(100)

Step 3: Improve it

  • You can make the 'on loud sound' block more or less sensitive by adding a 'set loud sound threshold' block to an ‘on start’ block. Use smaller numbers for quieter sounds, larger numbers for louder sounds. The coding video above shows you how to do this.
  • In Python, to change the threshold for loud sounds use microphone.set_threshold(SoundEvent.LOUD, 128) - changing the number 128 to the value you want between 0 and 255.
  • Make the lights also play a tune when they turn on.
  • Use sound to control other projects, such as lighting LEDs or servo motors connected to the pins on your micro:bit.