Skip to content

Activity

Sound logger

Intermediate | MakeCode, Python | LED display, Microphone | Arithmetic operators, Data handling, Input/output, Variables

Step 1: Make it

What is it?

Make a sound level logger to monitor how loud or quiet different places around you get over time

Introduction

Coding guide

What you'll learn

  • How to use the new micro:bit's built-in microphone sensor to measure how loud a sound is
  • Use variables and operators to track maximum values when gathering real-world data

How it works

  • The new micro:bit's microphone measures sound levels in numbers between 0 and 255, just like the light sensor.
  • A loop constantly compares the current sound level with a variable maxSound storing the loudest sound. If the current sound is louder than the loudest previous sound, it resets maxSound to the new loudest sound value.
  • Inside the loop, an if statement checks if you have pressed button A. If you have, it shows the sound level number on the LED display output. You can use this to monitor how loud it gets in different places over time.
  • Reset the maximum value by pressing the reset button on the back of the micro:bit.

What you need

  • new micro:bit with sound (or MakeCode simulator)
  • MakeCode or Python editor
  • battery pack (optional)

Step 2: Code it

1from microbit import *
2maxSound = 0
3lights = Image("11111:"
4              "11111:"
5              "11111:"
6              "11111:"
7              "11111")
8# ignore first sound level reading
9soundLevel = microphone.sound_level()
10sleep(200)
11
12while True:
13    if button_a.is_pressed():
14        display.scroll(maxSound)
15    else:
16        soundLevel = microphone.sound_level()
17        display.show(lights * soundLevel)
18        if soundLevel > maxSound:
19            maxSound = soundLevel
20            

Step 3: Improve it

  • Modify the project so it also records the quietest, or minimum, sound level
  • Use radio to send sound levels to another micro:bit so you can monitor sound levels remotely