X
Home > Blog > STEM for ESP32 > HOW to Detect Sound by Microphone with ESP32

HOW to Detect Sound by Microphone with ESP32

By r December 26th, 2024 153 views

1. Overview

KD2098BG

The sound sensor acts as a microphone that can capture sound information in the environment.

It is consists of a sensitive capacitor microphone for detecting sound and an amplification circuit. It works based on the propagation and vibration of sound. When sound travels near the sensor, the sound wave causes the sensor to vibrate. Then, the sensor converts sound vibrations into electrical signals and sends them for further processing or analysis.

2. Parameters

Operating voltage: DC 3.3 ~ 5V
Current: 15 mA
Maximum power: 0.075 W
Operating temperature: -10°C ~ +50°C
Dimensions: 48 x 24 x 18 mm
Positioning hole: Diameter of 4.8 mm
Interface: Telephone socket

3. Principle

When you speak loudly or play music to the MIC, these sound signals are converted into electrical ones, which are output at analog ports.

The amplifier circuit on the module amplifies the sound detected by the MIC. We can adjust the amplification by rotating the potentiometer. It is the maximum when we adjust the potentiometer clockwise to the end.

a24

4. Wiring Diagram

The sound sensor is an analog module. It needs to be connected to a red socket of the main board.

IOT-red

Here we connect the module to socket 7. From the port view of the kidsIOT board, socket 7 is the analog port io35.

b6

5. Test Code

/*  
  Project name: 9_sound
  Function: Read the analog value of the sound sensor
  Author: keyestudio    
  Hardware connection:  
    - Connect to port 7
  Library:  
    - none (no need to import additional library)  
  Cautions:  
    - Connect to port 7   
    - Before uploading test code, please correctly connect to the development board and port
    - Set the baud rate to 9600 before using serial monitor printing
*/
#define sound_Pin 35  //set pin to IO35

void setup() {
  //Set the baud rate
  Serial.begin(9600);
  //set IO35 to input
  pinMode(sound_Pin, INPUT);
}

void loop() {
  //Read the sound analog value
  int sound_value = analogRead(sound_Pin);
  //Print the sound analog value
  Serial.print("Analog value:");
  Serial.println(sound_value);
}

6. Test Result

After uploading the test code, open the serial monitor and set the baud rate to 9600, and the serial monitor prints the analog values of the sound sensor. Make some noise to the microphone, and these values changes.

We can’t add a delay to the code because the sound grabs the sound signals very quickly.

a25

HOW to Read Light Intensity by Photoresistor with ESP32
Previous
HOW to Read Light Intensity by Photoresistor with ESP32
Read More
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Next
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Read More
Message Us