keyestudio
Share your creative ideas with us here
By ran | 28 February 2025 | 0 Comments

HOW to Use Sound Sensor on Raspberry Pi with ESP32

Overview

In this kit, there is a Keyestudio DIY electronic block and a sound sensor. In the experiment, we test the analog value corresponding to the sound level in the current environment with it. The louder the sound, the larger the ADC, DAC and the voltage value, and the “shell” window will display the test results.

Working Principle

It uses a high-sensitive microphone component and an LM386 chip. We build the circuit with the LM386 chip and amplify the sound through the high-sensitive microphone. In addition, we can adjust the sound volume by the potentiometer. Rotate it clockwise, the sound will get louder.

Components

wps97

wps98

wps66

ESP32 Board*1

ESP32 Expansion Board*1

Keyestudio DIY Sound Sensor*1

image-20230602113648311

wps100

 

3P Dupont Wire*1

Micro USB Cable*1

 

Connection Diagram

Test Code

//**********************************************************************************
/*  
 * Filename    : MicroPhone
 * Description : Read the basic usage of ADC,DAC and Voltage
 * Auther      : http//www.keyestudio.com
*/
#define PIN_ANALOG_IN  34  //the pin of the Sound Sensor

void setup() {
  Serial.begin(9600);
}

//In loop(),the analogRead() function is used to obtain the ADC value, 
//and then the map() function is used to convert the value into an 8-bit precision DAC value. 
//The input and output voltage are calculated according to the previous formula, 
//and the information is finally printed out.
void loop() {
  int adcVal = analogRead(PIN_ANALOG_IN);
  int dacVal = map(adcVal, 0, 4095, 0, 255);
  double voltage = adcVal / 4095.0 * 3.3;
  Serial.printf("ADC Val: %d, \t DAC Val: %d, \t Voltage: %.2fV\n", adcVal, dacVal, voltage);
  delay(200);
}
//**********************************************************************************

Test Result

Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully,we will use a USB cable to power on, open the serial monitor and set the baud rate to 9600. We need to press the reset button on the ESP32, then the serial monitor will display the sound sensor’s ADC value, DAC value and voltage value. Rotate clockwise the potentiometer and speak at the MIC. Then you can see the analog value get larger, as shown below:

Leave a Reply

Your email address will not be published.Required fields are marked. *
Name
E-mail
Content
Verification code