< All Topics
Print

Micro:Bit Sound Level Detection Tutorial

Tutorial Aim

To display the sound level using LED lights, where the green LED is turned on for low sound levels, yellow LED for medium sound levels and red LED for high sound levels .The three levels are calculated according to the max reading of 1023. To adjust the sound sensor’s sensitivity the onboard potentiometer must be adjusted.

Requirements

This tutorial makes use of the Micro:bit Electronics Learning Package

Pin Layout:

Micro:Bit Pins:Sound Sensor Pins:
GNDGND
3.3VVCC
2OUT
Micro:Bit Pins:Green LED Pins:
GNDGND
13VCC
Micro:Bit Pins:Red LED Pins:
GNDGND
12VCC

Setup:

Please refer to image below for wiring.

  • Place the LED pins on the breakout board using the resistor on the longer leg, and connect to the Micro:bit pins using the FM jumper wires 
  • Place the sound sensor on the breadboard and use the FM jumper wires to connect it to pin 2 on the Micro:bit
  • Open Microsoft Makecode
  • Plug in the Micro:bit to the computer
Click to expand

Code Walk Through:

Initializing the sound levels:
The sound detection threshold can be calculated by dividing the max reading (1023) in half : 1023 / 2 = 511. The code for this section will go inside the ‘on start’ block.

  • Creating sound level variables: Select “Make a Variable” in the Variable tab and name it “loudLevel”.

  • Setting the noise threshold: Select “set (variable name) to number” in the Variables tab, set the variable name to loudLevel with the value of 511 and place the code snippet in the ‘on start’ block
Click to expand

Turning off the LED lights:

The LED lights are turned on & off by sending a signal to its corresponding pin. We can turn off all LED lights using a function. The code for this section will go inside the ‘turnOff” function’ block.

  • Creating a function: Select “Make a Function” in the Functions tab in the Advanced section and name it “turnOff”
  • Switching an LED off: Select “digital write pin (pin number) to (value)” in the Pins tab, select the pin number to be P12 & the value to be 0
  • Repeat the above step so that pin 13 (P13) is also set to 0
Click to expand

Reading & storing the sound sensor’s value:


We can access the sound sensor value by reading its pin’s output and storing it in a variable called ‘soundLevel’. The code for this section will go inside the ‘forever’ block.

  • Turning off all LEDS while detecting the sound level: Select “call turnOff” in the Functions tab.
  • Creating a variable to store the sensor reading: Select “ Make a Variable” in the Variables tab & name it “soundLevel”.

  • Retrieving the sensor reading: Under the Advanced section in the Pins tab, select “analog read pin (pin number)” and select P2 for pin number.

  • Setting the soundLevel variable to the sensor reading: Select “set (variable name) to (value)”, change the variable name to soundLevel & drag the code snippet for getting the sensor’s reading (previous step) into the value.
Click to expand

Detecting the sound level:

We can determine the sound level by comparing the sensor reading to the noise threshold and turning on that sound level’s LED light. In order for the sound level to be displayed, the code must be paused for a specified amount of time so that the LED has enough time to switch & remain on. The code for this section will go inside the ‘forever’ block.

  • Creating a logic comparison block: Select an if else conditional block in the Logic tab, and then pressing the small + on the bottom right hand side so that there’s a ‘if’ & ‘else if’ conditional statements and an else section. 

  • Checking if the sensor reading is above the loud sound threshold: Select the “(value) < (value)” comparison block in the Logic tab, change the values to “soundLevel < loudLevel” (variable names can be accessed in the Variable tab) and placing the code snippet inside the ‘if’ conditional statement

  • Turning on the no noise detected LED if the sensor reading is low: Select “digital write (pin number) to (value)”, change pin number to 12 and value to 1 before placing that code snippet underneath the ‘if’ conditional block.

  • Displaying a cross to indicate no sound detected: Select ‘show icon’ in the Basic tab & change it to show a cross.

  • Turning on the noise LED if sound is detected: Select “digital write (pin number) to (value)”, change pin number to 13 and value to 1 before placing that code snippet inside the ‘else if’ conditional block.

  • Displaying a tick to indicate sound detected: Select ‘show icon’ in the Basic tab & change it to show a tick.

  • Pausing the code: Select “pause (ms)” in the Basic tab, setting it to 2000 (2 seconds) and placing it last in the ‘Forever’ block
Click to expand

Flashing the code onto the Micro:Bit:

 
  • Make sure the Micro:Bit is connected to the computer 
  • On the bottom left corner, click the “Download” button and follow the prompts

Full Block code

Full Python Code

# Turns off the led pins
def turnOff():
    pins.digital_write_pin(DigitalPin.P12, 0)
    pins.digital_write_pin(DigitalPin.P13, 0)
soundLevel = 0
loudLevel = 511
# Continuously checks the sound sensor and its level

def on_forever():
    global soundLevel
    turnOff()
    soundLevel = pins.analog_read_pin(AnalogPin.P2)
    if soundLevel < loudLevel:
        pins.digital_write_pin(DigitalPin.P12, 1)
        basic.show_icon(IconNames.NO)
    else:
        pins.digital_write_pin(DigitalPin.P13, 1)
        basic.show_icon(IconNames.YES)
    basic.pause(2000)
basic.forever(on_forever)
 

Result:

After uploading the code,  expose the sensor to a range of sound levels.  You will see a tick for Acceptable sound levels and an X for unacceptable sound levels. Adjust the sensitivity meter on the sensor to suit your project!

Click to expand

Credits:

  • Microsoft
  • The Micro:bit Community
  • The STEM Community
Table of Contents