micro:bit Clap Lamp
Clap once and the whole LED display lights up like a lamp; one more clap turns it off. The onboard microphone hears the sound (was_event) and set_threshold sets the level. No extra parts are needed.

Video
Circuit Diagram

Source Code
1# ─────────────────────────────────────────
2# BlueGrays · bluegrays.com
3# micro:bit Clap Lamp
4# ─────────────────────────────────────────
5# Clap once and the whole LED display lights up like a lamp. One more clap
6# turns it off. The microphone on the board hears the sound. No extra
7# parts are needed.
8#
9# Pins:
10# LED -> Onboard 5x5 LED display
11# MIC -> Onboard microphone
12
13from microbit import *
14
15# ============ SETTINGS ============
16CLAP_LEVEL = 150 # Sound level that counts as a clap, 0-255
17LOOP_MS = 50 # Loop pause (ms)
18
19LAMP = Image("99999:99999:99999:99999:99999")
20
21microphone.set_threshold(SoundEvent.LOUD, CLAP_LEVEL)
22lamp_on = False
23
24while True:
25 # If a loud sound was heard since the last check, flip the lamp's state.
26 if microphone.was_event(SoundEvent.LOUD):
27 lamp_on = not lamp_on
28 if lamp_on:
29 display.show(LAMP)
30 else:
31 display.clear()
32 sleep(LOOP_MS)
33FAQ
The lamp also reacts to talking or music. What should I do?
The CLAP_LEVEL threshold is too low. The value goes from 0 to 255; write 200 instead of 150 and only louder sounds count as a clap. If the lamp does not hear your clap, do the opposite and lower it to 100. The right value depends on how noisy the room is.
How can I tell the microphone is listening?
While the microphone is in use, a small red microphone light is on at the front of the board, to the right of the logo. The microphone hole is right next to that light; if you cover it with a finger, sounds are hard to hear.
How do I put this code on the micro:bit?
The code is MicroPython. Open the micro:bit Python Editor (python.microbit.org) on a computer, delete the code that is there and paste this one. Connect the board with a USB cable and press Send to micro:bit. If that button does not work, use Save to download the .hex file and copy it to the drive called MICROBIT. You can rebuild the same project with blocks in MakeCode, but this code cannot be pasted there.
Does it work on the older micro:bit (V1) too?
The code is written for the micro:bit V2. Projects that only use the LED display, buttons A and B and the accelerometer also run on a V1. The speaker, the microphone and the touch logo exist only on the V2, so code that uses them gives an error on a V1. If your board has a speaker on the back and a gold logo on the front, it is a V2.
