In this lesson, we will introduce how to use Arduino to test the sound detection sensor. This test is divided into two parts, the sound sensor test in digital mode and analog mode.

Note: We are using Arduino UNO development board here, if you are using the Mega2560 in the experiment, please change the development board type in Arduino IDE to Mega2560 before uploading the code.

DIGITAL MODE TEST

Hardware Preparation

SOFTWARE

Hardware Settings

1. Connect the sound sensor to the Arduino board by using jumpers.
Coneect the sensor”+” to Arduino “5v”, “-” to Arduino “GND”, “S” to Arduino “D2”.
2.Connect the Arduino Board to PC via USB cable.
3. Sound sensor settings: Turn the switch on the module to “D”. Adjust the blue miniature potentiometer until the blue LED “DATA” is at the critical value of on and off, and then let the LED go out by default.

Code Program

After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up the following sketch onto your Arduino.

int digit_sensor = 2; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int digitValue ;  //  value from the digit input pin

void setup () {
  pinMode (ledPin, OUTPUT);
  pinMode (digit_sensor, INPUT);
  Serial.begin (9600);
}

void loop () {
  digitValue=digitalRead(digit_sensor);
  if (digitValue==LOW)
  {
    digitalWrite (ledPin, HIGH);
    delay(50);
    }   
  else 
  {
    digitalWrite (ledPin, LOW);
    delay(10);
    }
}

Running Result

A few seconds after the upload finishes, you can clap or knock something next to the sensor, when the volume reaches to a certain value, the LED attached to pin 13 on the Osoyoo Uno board will light up.

Note: If the LED is not lighting up or the LED is always bright, you need to change the sensor sensitivity by rotating the potentiometer.

ANALOG MODE TEST

In this example, we will show how to use the analog pin to detect the sound. The microphone sensor will detect the sound intensity of your surroundings and will light up an LED if the sound intensity is below a certain threshold.

Connection

Coneect the sensor”+” to Arduino “5v”, “-” to Arduino “GND”, “S” to Arduino “A0”.

Sound sensor settings: Turn the switch on the sound sensor module to “A”. Adjust the blue miniature potentiometer until the blue LED “DATA” is at the critical value of on and off, and then let the LED go out by default.

Code Program

After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up the following sketch onto your Arduino.

const int ledPin =  13; //pin 13 built-in led
const int soundPin = A0; //sound sensor attach to A0
int threshold = 600; //Set minimum threshold for LED lit
void setup()
{
  pinMode(ledPin,OUTPUT);//set pin13 as OUTPUT
  Serial.begin(9600); //initialize serial
}
void loop()
{
  int value = analogRead(soundPin);//read the value of A0
  Serial.println(value);//print the value
  if(value < threshold) //if the value is less than 600
  {
    digitalWrite(ledPin,HIGH);//turn on the led
    delay(2000);//delay 2s
  }
  else
  {
    digitalWrite(ledPin,LOW);//turn off the led
  }
  delay(1000);
}


When the sound sensor module does not detect sound, the “DATA” LED on the module remains off. The analog value read by the sensor will be displayed on the Serial Monitor, which is about “600” to “650”.

When the sound sensor module detects sound, the “DATA” LED on the module will be lit. The analog value read by the sensor will be displayed on the Serial Monitor, which is less than “600”. So we set the minimum threshold to light up the LED as “600”

NOTE: The threshold of each sensor may be different, please adjust the threshold in the code according to your actual test situation