# Sound Sensor with micro:bit

#### Step 1   The Module

![](https://littlebirdelectronics.com.au/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBa3pQIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--87aefb381c78b4afa3bd3b10e1d5d9fb29c5667e/step_731_img1.png)

* Let's take a closer look at the Sound Sensor Module. It has four pins:

  DO: Digital Output&#x20;

  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.

  AO: Analog Output
* 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.&#x20;

#### 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   Add a resistor

#### Step 8   Connect GND to LED (Negative lead)

#### Step 9   Connect P2 to resistor

#### Step 10   The MakeCode

```
let soundLevel = 0
basic.forever(function () {
    soundLevel = pins.analogReadPin(AnalogPin.P1)
    basic.showNumber(soundLevel)
    basic.pause(100)
})
basic.forever(function () {
    if (soundLevel >= 32) {
        pins.digitalWritePin(DigitalPin.P2, 1)
    } else {
        pins.digitalWritePin(DigitalPin.P2, 0)
    }
    basic.pause(100)
})
```

*
* Click on 'New Project'
* Add the following code in the Javascript interface.

#### 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.&#x20;
  * Serial.println (sensorValue, DEC);' outputs the analog value to the serial monitor

  <br>

#### Step 12   Open Arduino IDE

![](https://littlebirdelectronics.com.au/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBazNQIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--b28cdf508e7792ab572f16b26c3c3fc25bfba1c7/step_741_img1.png)

* We can also use the Arduino IDE to program the micro:bit.&#x20;
* 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!<br>

#### Step 13   Serial monitor values

![](https://littlebirdelectronics.com.au/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBbERQIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--d04e392771a778faf4d3228b6fb6e9f31c13575f/step_744_img1.png)

* Click on Tools > Serial monitor \
  &#x20;Now you can see the the output values you are getting in a quiet room.

#### Step 14   Making adjustments

![](https://littlebirdelectronics.com.au/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBbEhQIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--088841c11f8053f614a1dbdcdca0cf10a547e509/step_746_img1.png)

* 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 15   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);
    }
  else{
    digitalWrite (ledPin, LOW);
  }
  Serial.println (sensorValue, DEC);
  delay(1000);
}
```

* 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 this sketch to the micro:bit.

#### Step 16   Upload the sketch

![](https://littlebirdelectronics.com.au/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBazdQIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--3396ad96e6d678943deed9137d555299cfb77af1/step_743_img1.png)

![](https://littlebirdelectronics.com.au/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBazdQIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--3396ad96e6d678943deed9137d555299cfb77af1/step_743_img1.png)

![](https://littlebirdelectronics.com.au/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBay9QIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--33e1c0f72a4fc9f3cd26b765e124b50c260bf3b5/step_743_img2.png)

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.linhkientot.vn/micro-bit-products/micro-bit-starter-kit-v1/sound-sensor-with-micro-bit.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
