Use a Sound Sensor with micro:bit


Sound is a type of energy made by vibrations. When we hear something, we are sensing the vibrations in the air.  

In this guide, learn how to use a sound sensor module with the micro:bit. Program it in the Arduino IDE or MakeCode editor. When the vibration or sound level goes above the threshold level, an LED will light up. 

Complete this guide to get started with creating your own sound detection device using the micro:bit

Parts Used in This Guide


Step 1  The Module

Let's take a closer look at the Sound Sensor Module.

It has four pins:

  • DO: Digital Output 
  • 3.3V  : This pin is marked as '+' on the module. We'll connect it to 3.3V on the micro:bit
  • GND: In electronics, we define a point in a circuit to be a kind of zero volts or 0V reference point, on which to base all other voltage measurements. This point is called ground or GND.
    • Note: Voltage is the difference in potential between two points. As it is difficult to talk about voltage without a reference point, we need another point to compare it to. 
  • AO: Analog Output

Step 2  Connect module to breadboard

Step 3  Connect P1 to AO

Step 4  Connect GND to GND

Step 5  Connect 3.3V to +

Step 6  Connect P5 to DO

Step 7  Connect GND to LED (Negative lead)

Step 8  Add a resistor

Step 9  Connect P2 to resistor

Step 10  Open Arduino IDE

Note: If you would like toe use MakeCode editor, please jump to Step 16! 

We can also use the Arduino IDE to program the micro:bit. 

Open up the Arduino IDE.

 If you have not already got it installed, please follow our previous guide on how to install and set up the Arduino IDE for the micro:bit!

Step 11  The code

              const int sensorPin = 1; // analog input pin for sound sensor
const int ledPin = 2; // pin for LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup () {
  pinMode(sensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin (9600);
void loop () {
  sensorValue = analogRead (sensorPin);
  Serial.println (sensorValue, DEC);
  digitalWrite (ledPin, HIGH);
  delay (1000);
  digitalWrite (ledPin, LOW);
  delay (1000);
  • We connected the analog pin (AO) of the sound sensor to Pin 1 on the micro:bit, therefore the value of int sensorPin is A1 here.
  • We connected the positive leg of the LED to pin 2 of the micro:bit, so int ledPin is set to 2.
  • We will also create a variable, 'sensorValue' to store the value coming from the sound sensor.
  • While the circuit has current flowing through it, the LED will blink on and off with a delay of 1 second in between. Feel free to change the delay value. Right now it is set at '1000' which is 1 second. 
  • Serial.println (sensorValue, DEC);' outputs the analog value to the serial monitor

Step 12  Upload the sketch

  • Click on the tick icon on the top left hand-corner to verify the code
  • Click on the upload button next to it to upload the sketch

Step 13  Serial monitor values

Click on Tools > Serial monitor 

 Now you can see the the output values you are getting in a quiet room.

Step 14  Sound level goes above threshold

              const int sensorPin = 1; // pin for sound sensor
const int ledPin = 2; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor

void setup ()
  pinMode(sensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin (9600);

void loop ()
  sensorValue = analogRead (sensorPin);
  if (sensorValue >= 32){
    digitalWrite (ledPin, HIGH);
    digitalWrite (ledPin, LOW);
  Serial.println (sensorValue, DEC);

The code has been updated so that when the sensorValue goes above or equal 32, the LED will light up. If not, the LED will not light up.

Upload the sketch as you did in Step 7.

Step 15  Test it

You can update the sound detection threshold level so that when a loud sound is made, the LED comes on. You can do so by changing '32' in 'sensorValue >= 32' to a higher value.

Step 16  The MakeCode!

Get the MakeCode below by clicking the Download button, or create your own by following these steps. 

  • Open up the MakeCode editor and start a new project. 
  • From 'the Basic' tab, drag and drop another 'forever' block into the editor.
  • In 'Variables' tab, create a new variable and name it 'soundLevel'. 
  • Drag and drop a 'set soundLevel to 0' block into the first 'forever' block.
  • Under 'Pins' tab, drag and drop an 'analog read pin ... ' block. Set it to P1 and attach it to 'set soundLevel to ... ' block.
  • Again in 'Basic' tab, drag and drop a 'pause (ms)' block. Change its value to 100. 
  • In the other 'forever' block,  add an 'if ... else' block, this is found under 'Logic' tab. 
  • Grab a '0 < 0' block and place this in the 'If ... then' condition 
  • Change its values to soundLevel >= 32. You can get the 'soundLevel' block from 'Variables' tab.
  • Grab a 'digital write pin ... to ... ' block in 'Pins' tab. Change its values to P2 and 1.
  • Duplicate and place a 'digital write pin ... to ... ' block under 'else'. This time set its values to P2 and 0. 
    • To duplicate, right-click on the first 'digital write pin ... to ... ' block and select 'Duplicate'
  • Add a 'pause (ms) ... ' block after the 'if ... else' block.

Note: You may want to change '32' in 'soundLevel >= 32' to a different value, so that when a loud sound is made the LED comes on.