Sound Sensor¶

Description |
This sensor is sensitive to sound intensity. It can be used to detect noises. |
Usage |
It outputs an analog sine wave. The louder the sound the larger the magnitude of the sine wave. A higher peak amplitude means louder sound. |
Voltage Range |
3 - 5V |
Interface Type |
Analog Input |
Example Usage |
from microbit import *
LOUD_THRESHOLD = 50 # raw
SAMPLE_WT = 0.01 # each new value has a 1% significance
def calc_avg(avg, new_val):
return avg * (1 - SAMPLE_WT) + new_val * SAMPLE_WT
def read_sound():
return pin0.read_analog()
# average starts at the first sensor value
avg_sound = read_sound()
while True:
sound_val = read_sound()
variation = sound_val - avg_sound
if variation > LOUD_THRESHOLD or variation < -LOUD_THRESHOLD:
# loud noise detected
# ...do something
avg_sound = calc_avg(avg_sound, sound_val)
|