In this lesson, you will learn how to use a sound sensor module. This module has two outputs:
To make sure the microphone can detect your voice normally, please try to change its sensitivity by turning the blue precise potentiometer on the module. Given to its preciseness, you need to turn 10 times 360 degrees the potentiometer to start seeing some change.
(1) x Sound sensor module
(4) x F-M wires (Female to Male DuPont wires)
The sound sensor module provides an easy way to detect sound and is generally used for detecting sound intensity. This module can be used for security, switch, and monitoring applications. Its accuracy can be easily adjusted for the convenience of usage. It uses a microphone which supplies the input to an amplifier, peak detector and buffer. When the sensor detects a sound, it processes an output signal voltage which is sent to a microcontroller then performs necessary processing
These microphones are widely used in electronic circuits to detect minor sounds or air vibrations which in turn are converted to electrical signals for further use. The two legs as shown in the image above are used to make electrical connection with the circuit.
You can click the blue text link to download the program file to your local device, and double-click the file to open it after the download is complete. Please note: Before opening the file, ensure that you have installed the Arduino IDE development environment and completed the installation of relevant components such as the board support package and driver corresponding to the UNO R3. If you have any questions about this operation process, you can refer to the "part 1" chapter of the document for detailed guidance.
Click the Serial Monitor button to turn on the serial monitor. The basics about the serial monitor are introduced in details in tutorial 4 in part 2.
int sensorAnalogPin = A0; // Select the Arduino input pin to accept the Sound Sensor's analog output
int sensorDigitalPin = 3; // Select the Arduino input pin to accept the Sound Sensor's digital output
int analogValue = 0; // Define variable to store the analog value coming from the Sound Sensor
int digitalValue; // Define variable to store the digital value coming from the Sound Sensor
int Led13 = 13; // Define LED port; this is the LED built in to the Arduino (labled L)
Variables and Definitions:
int sensorAnalogPin = A0;:Arduino analog pin A0 connected to the sound sensor's analog outputint sensorDigitalPin = 3;:Arduino digital pin 3 connected to the sound sensor's digital outputint analogValue = 0;:Variable to store the analog value from the sound sensorint digitalValue;:Variable to store the digital value from the sound sensorint Led13 = 13;:Arduino built-in LED (labeled L)void setup()
{
Serial.begin(9600); // The IDE settings for Serial Monitor/Plotter (preferred) must match this speed
pinMode(sensorDigitalPin,INPUT); // Define pin 3 as an input port, to accept digital input
pinMode(Led13,OUTPUT); // Define LED13 as an output port, to indicate digital trigger reached
}
Setup Function:
Serial.begin(9600);:Initializes serial communication at 9600 baud ratepinMode(sensorDigitalPin,INPUT);:Sets the digital pin as inputpinMode(Led13,OUTPUT);:Sets the built-in LED pin as outputvoid loop(){
analogValue = analogRead(sensorAnalogPin); // Read the value of the analog interface A0 assigned to digitalValue
digitalValue=digitalRead(sensorDigitalPin); // Read the value of the digital interface 7 assigned to digitalValue
Serial.println(analogValue); // Send the analog value to the serial transmit interfaceif(digitalValue==HIGH) // When the Sound Sensor sends signla, via voltage present, light LED13 (L)
{
digitalWrite(Led13,HIGH);
}
else
{
digitalWrite(Led13,LOW);
}delay(50); // Slight pause so that we don't overwhelm the serial interface
}
Loop Function:
analogValue = analogRead(sensorAnalogPin);:Reads the analog value from the sound sensordigitalValue=digitalRead(sensorDigitalPin);:Reads the digital value from the sound sensorSerial.println(analogValue);:Prints the analog value to the serial monitorif(digitalValue==HIGH):Checks if sound is detected
digitalWrite(Led13,HIGH);:Turns on the built-in LED if sound is detecteddigitalWrite(Led13,LOW);:Turns off the built-in LED if no sound is detecteddelay(50);:Waits 50 milliseconds to avoid overwhelming the serial interface