Microphone¶
Note
This feature is for micro:bit V2 boards only
The new micro:bit V2 has a built-in microphone that can detect loud and quiet events, and also measure how loud a sound is. The microphone input is located on the front of the board alongside a microphone activity LED, which is lit when the microphone is in use.
This simple Clap hearts program reacts to current loud sounds like claps to animate a heart:
from microbit import *
while True:
if microphone.current_event() == SoundEvent.LOUD:
display.show(Image.HEART)
sleep(200)
if microphone.current_event() == SoundEvent.QUIET:
display.show(Image.HEART_SMALL)
Sound events¶
The microphone can respond to a pre-defined set of sound events that are based on the amplitude and wavelength of the sound.
These sound events are represented by instances of the SoundEvent class, accessible via variables in microbit.SoundEvent:
microbit.SoundEvent.QUIET: Represents the transition of sound events, from loud to quiet like speaking or background music.
microbit.SoundEvent.LOUD: Represents the transition of sound events, from quiet to loud like clapping or shouting.
Functions¶
current_event()
- return: the name of the last recorded sound event,
SoundEvent('loud')orSoundEvent('quiet').
was_event(event)
- event: a sound event, such as
SoundEvent.LOUDorSoundEvent.QUIET.- return:
trueif sound was heard at least once since the last call, otherwisefalse.was_event()also clears the sound event history before returning.
is_event(event)
- event: a sound event, such as
SoundEvent.LOUDorSoundEvent.QUIET.- return:
trueif sound event is the most recent since the last call, otherwisefalse. It does not clear the sound event history.
get_events()
- return: a tuple of the event history. The most recent is listed last.
get_events()also clears the sound event history before returning.
set_threshold(event, value)
- event: a sound event, such as
SoundEvent.LOUDorSoundEvent.QUIET.- value: The threshold level in the range 0-255. For example,
set_threshold(SoundEvent.LOUD, 250)will only trigger if the sound is very loud (>= 250).
sound_level()
- return: a representation of the sound pressure level in the range 0 to 255.
Example¶
Clap lights¶
Use logic to make a light that switches on and off when you clap your hands.
How it works
- This program uses a special kind of variable to track if the light is on or off. Boolean variables can only have two values:
TrueorFalse. - The
lightsOnvariable is set to False at the start of the program, so the lights are off. - An infinite loop,
while True, keeps the rest of the code running: - If the microphone detects a loud sound, the code uses the not logical operator to change the value of the
lightsOnvariable to be the opposite of its current value: - If
lightsOnis False (and the lights are off), it becomesTrueand the program lights up the LEDs by showing a heart. - If
lightsOnis True (and the lights are on), it becomesFalseand the code switches the LEDs off by clearing the screen. sleep(100)pauses the code for a short time, 100 milliseconds or one tenth of a second, before repeating the loop.
from microbit import *
lightsOn = False
while True:
if microphone.was_event(SoundEvent.LOUD):
lightsOn = not lightsOn
if lightsOn:
display.show(Image.HEART)
else:
display.clear()
sleep(100)
Exercises¶
- Create a sound meter that will display how load the sound is on the LED screen.