3.2 Sound Sensor

3.2.1 Overview

The sound sensor, with excellent sound reception, mainly contains a high sensitivity microphone and an LM358 operational amplifier. Weak sound signals are captured by the microphone and amplified by LM358, so the output is clear and recognizable, which can be effectively detected the sounds.

This sensor features high sensitivity, fast response speed, so is widely used in sound detection and recognition, providing a stable and reliable voice input solution for various intelligent devices.

3.2.2 Schematic Diagram

6-4-2

Working principle: If the built-in electret film vibrates after receiving sounds, its capacitance will change to produce a tiny voltage. The LM358 chip is adopted in circuits to amplify the sound signals detected by the high-sensitivity microphone.

3.2.3 Test Code

In Files, open 3-2-sound.py and click .

Code:

'''
 * Filename    : 3-2-sound
 * Thonny      : Thonny 4.1.4
 * Auther      : http//www.keyestudio.com
'''
# Import Pin, ADC and DAC modules.
from machine import ADC,Pin
import time

adc=ADC(Pin(34))			#set pin GPIO 34 as ADC input pin
adc.atten(ADC.ATTN_11DB)	#voltage range 0-3.3V
adc.width(ADC.WIDTH_12BIT)	#set ADC resolution

# Read ADC value once every 0.1seconds, convert ADC value to DAC value and output it, and print these data to “Shell”.
while True:	
        adcVal = adc.read()				#read the analog value of the sound sensor and assign it to variable to adcVal
        print("Sonoro ADC Val:",adcVal) #serial monitor prints adcVal value
        time.sleep_ms(50)					#delay 50mS

Result:

Click img“Run current script” to run the code. “Shell” displays the ADC value of the sensor. Press “Ctrl+C” or click img“Stop/Restart backend” to quit the execution.

QQ_1722332795073

3.2.4 Code Explanation

  1. adc = ADC(Pin(34)):

    create an ADC object and set pin GPIO 34 to input. Pin(34) assigns a pin, ADC(Pin(34)) means pin GPIO 34 is the input pin of ADC.

  2. adc.atten(ADC.ATTN_11DB):

    set the Attenuation class of ADC. Attenuation expands the voltage range of ADC. ADC.ATTN_11DB is an optional setting of ESP32, which expands the voltage range to 0-3.6V. ADC of ESP32 supports different attenuation settings:

    • ADC.ATTN_0DB:input voltage range 0-1V

    • ADC.ATTN_2_5DB:input voltage range 0-1.34V

    • ADC.ATTN_6DB:input voltage range 0-2V

    • ADC.ATTN_11DB:input voltage range 0-3.3V

    Setting attenuation helps you accurately read analog signals across different voltage ranges.

  3. adc.width(ADC.WIDTH_12BIT):

    Set ADC resolution to 12 bit. ADC of ESP32 can be configured different resolutions:

    • ADC.WIDTH_9BIT:resolution of 9 bit

    • ADC.WIDTH_10BIT:resolution of 10 bit

    • ADC.WIDTH_11BIT:resolution of 11 bit

    • ADC.WIDTH_12BIT:resolution of 12 bit

    The 12-bit resolution means that the ADC can subdivide the input voltage into 2^12^ = 4096 levels, which can provide finer measurements.

  4. print("Sonoro ADC Val:",adcVal) prints characters in double quotes and the value of the variable adcVal. Line wrap after printing.