pythonbbc-microbitmu

Trouble using microphone on micro:bit


I wrote code that detects the loudness of sound using a micro:bit and updates a variable once every second.

from microbit import *
import time

sound_level = 0

while True:
    sound_level = input.sound_level()
    display.scroll(str(input.sound_level))
    time.sleep(1)

And when I flashed it to the micro:bit with Mu editor, the following error occurred.

line 7, attributeError'function' object has no attribute 'sound_level'

I changed this code slightly and ran it on micro:bit's site, but there was no error. I think it's an error that occurs because the input.sound_level() function is not executed, but I can't resolve it.

I expected the results to display the noise level from 0 to 255 with micro:bit's LED. I ran this in the editor on the microbit site and it worked as expected. However, when running it in Mu editor, an error regarding "basic" occurred, and the code was changed to run with a while statement. But I still get an error elsewhere, namely in input.sound_level().


Solution

  • Have you tried replacing input.sound_level() with microphone.sound_level()?

    Here's a guide for using the sound-meter.

    input() is a built-in python function, so not part of the microbit library.