ESP32 S3 UNO - Sound Sensor

This ESP32 S3 Uno sound sensor tutorial shows you how to detect sound, such as a clap or a loud noise, with the ESP32 S3 Uno form board and Arduino code. You use a simple sound detector module and see a message on the Serial Monitor each time a sound starts and stops.

In this tutorial, you will:

  1. Compare the digital and analog sound detector modules
  2. Wire a sound sensor to the ESP32 S3 Uno safely at 3.3V
  3. Write Arduino code that detects when a sound starts and stops
  4. Adjust the sound sensor sensitivity with its potentiometer
ESP32 S3 Uno sound sensor

Once it works, you can change the code to switch on an LED, turn on a lamp through a relay, or move a servo motor when a sound is heard.

Hardware Preparation

1×ESP32 S3 Uno-form Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Digital Sound Sensor
1×Analog Sound Sensor
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Sound Sensor

A sound sensor listens to the noise around it and tells your board when the sound gets loud enough. Knowing which type of module you have helps you pick the right pin and the right code.

You can buy two kinds of sound sensor modules. The digital module only gives an ON or OFF signal. The analog module gives the same ON or OFF signal, and it also gives a changing voltage that follows the sound level. On both modules, a small potentiometer sets how loud a sound must be before the digital output changes.

The Digital Sound Sensor Pinout

The digital module is the simplest one to use. It has only three pins.

VCC pin

Connect it to a supply between 3.3V and 5V. With the ESP32 S3 Uno, use the 3.3V pin.

GND pin

Connect it to GND (0V).

OUT pin

This is the output pin. It stays HIGH when the room is quiet and goes LOW when the sensor hears a sound. Connect it to a digital input pin on the ESP32 S3 Uno.

Sound Sensor Pinout
image source: diyables.io

The board also has a potentiometer to set the sensitivity, and two small LEDs. One LED shows that the module has power. The other LED turns on while a sound is present and turns off when it is quiet.

The Analog Sound Sensor Pinout

The analog module has one extra pin, so you can read how loud the sound is, not only whether there is a sound.

+ pin

The power pin. The module works from 5V, but with the ESP32 S3 Uno it is safer to power it from 3.3V so its outputs never go above 3.3V.

G pin

Connect it to GND (0V).

DO pin

The digital output. It is HIGH when it is quiet and LOW when a sound is detected. Connect it to a digital input pin on the ESP32 S3 Uno.

AO pin

The analog output. Its voltage follows the sound level. Connect it to an analog input pin on the ESP32 S3 Uno, such as A0 (GPIO2). The ESP32 S3 Uno reads it with a 12-bit ADC, so analogRead() returns a value from 0 to 4095.

analog sound sensor Pinout
image source: diyables.io

Like the digital module, it has a potentiometer that sets the sensitivity of the digital output. It also has a power LED and a sound LED that lights up while a sound is heard.

How It Works

The logic of the sensor is inverted, which surprises many beginners. Once you know this, the code is easy to read.

The potentiometer sets a threshold. When the sound is louder than this threshold, the output pin goes LOW. When the sound is quieter than the threshold, the output pin stays HIGH. So in your code, LOW means "sound" and HIGH means "quiet".

ESP32 S3 Uno Pinout

The image below shows the pinout diagram of the ESP32 S3 Uno form board. Use it to find the Uno header pins (D2, A0, SDA…) and their GPIO numbers when you wire the circuit.

ESP32 S3 Uno pinout diagram

Wiring Diagram

Connect the sound sensor's OUT pin to the D8 header of the ESP32 S3 Uno, and power the module from the 3.3V pin.

The wiring diagram between ESP32 S3 Uno Sound Sensor

This image is created using Fritzing. Click to enlarge image

Sound Sensor Pin ESP32 S3 Uno Pin
VCC 3.3V
GND GND
OUT D8 (GPIO21)

WARNING

The ESP32 S3 Uno GPIO pins are NOT 5V tolerant. If you power the sound sensor from 5V, its OUT pin also outputs 5V, which can damage GPIO21. Power the sensor from 3.3V, or put a voltage divider (for example 10kΩ and 20kΩ) between the OUT pin and the ESP32 S3 Uno pin.

How To Program For Sound Sensor

Reading a digital sound sensor takes only two functions. You set the pin as an input once, then read it again and again.

  1. In setup(), set the ESP32 S3 Uno pin as a digital input with pinMode(). Here the pin is GPIO21, which is the D8 header.
pinMode(21, INPUT);
  1. In loop(), read the pin state with digitalRead(). A LOW value means a sound is heard.
int soundState = digitalRead(21);

ESP32 S3 Uno Code - Detecting the sound

The sketch below reads the sensor on GPIO21 all the time and compares the new state with the last one. It prints a message only when the state changes, so you see one line when a sound starts and one line when it stops, instead of a flood of messages.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-sound-sensor */ #define SENSOR_PIN 21 // The ESP32 S3 Uno pin GPIO21 (D8) connected to OUT pin of the sound sensor int prev_sound_state = HIGH; // the previous state from the input pin int sound_state; // the current reading from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the ESP32 S3 Uno's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: sound_state = digitalRead(SENSOR_PIN); if (prev_sound_state == HIGH && sound_state == LOW) Serial.println("The sound has been detected"); else if (prev_sound_state == LOW && sound_state == HIGH) Serial.println("The sound has disappeared"); // save the the last state prev_sound_state = sound_state; }

Detailed Instructions

Follow these steps in order:

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Copy the code above and paste it into Arduino IDE.
  6. Upload the code by clicking the Upload button.
  7. Open the Serial Monitor and set the baud rate to 9600.
  8. Clap your hands close to the sound sensor.
  9. Check the result on the Serial Monitor.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
The sound has been detected The sound has disappeared The sound has been detected The sound has disappeared
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Tip: If the sound LED on the module is always on, or never turns on even when you clap, turn the potentiometer slowly with a small screwdriver until the LED is off in silence and turns on when you clap.

Tips to Improve Sound Detection

A sound sensor is a small analog circuit, so it is sensitive to its surroundings. These tips help you get steady and reliable results.

Adjust the sensitivity

Turn the potentiometer to set how loud a sound must be to trigger the output. Small turns make a big difference.

Reduce vibrations

The microphone also picks up vibration and wind. Fix the module to a solid surface so that bumps on the table do not count as sounds.

Stay within the sensing range

This sensor only hears sounds up to about 10 inches (25 cm) away. Make your test sounds close to it.

Use a clean power supply

Noise on the power line can trigger the sensor by mistake. Use a stable supply and short wires.

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

The demo in the video uses the code below. It counts your claps and plays a different light effect on eight LEDs: one clap blinks all LEDs, two claps run a water wave, three claps light from the inside out, and four claps make a random dance. The LEDs are active-LOW, so each LED turns on when its pin goes LOW.

Part ESP32 S3 Uno Pin
Sound sensor OUT D10 (GPIO10)
LED 1 to LED 4 D2 (GPIO18), D3 (GPIO17), D4 (GPIO19), D5 (GPIO20)
LED 5 to LED 8 D6 (GPIO3), D7 (GPIO14), D8 (GPIO21), D9 (GPIO46)

WARNING

This demo uses D6 (GPIO3) and D9 (GPIO46). Both are boot strapping pins on the ESP32 S3 Uno. The board reads them at power-up to choose its boot mode. If an LED circuit pulls one of them to the wrong level at startup, the board may not boot or may fail to upload code. If that happens, unplug the LED wires from D6 and D9, upload the code, then plug them back in.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-sound-sensor */ //===================================================== // SOUND CONTROLLED LED EFFECTS // 1 Clap -> Blink All // 2 Claps -> Water Wave // 3 Claps -> Inside Out // 4 Claps -> Random Dance //===================================================== #define SOUND_PIN 10 // GPIO10 (D10) - OUT pin of the sound sensor // ESP32 S3 Uno GPIOs for Uno headers D2, D3, D4, D5, D6, D7, D8, D9 const byte LED_PINS[] = {18,17,19,20,3,14,21,46}; const byte NUM_LEDS = sizeof(LED_PINS); const unsigned long CLAP_DEBOUNCE = 80; const unsigned long CLAP_TIMEOUT = 700; //----------------------------------------------------- // Clap detection //----------------------------------------------------- bool lastSound = HIGH; unsigned long lastClapTime = 0; byte clapCount = 0; bool waitingSequence = false; //----------------------------------------------------- // Effect control //----------------------------------------------------- byte currentEffect = 0; //----------------------------------------------------- // Effect 1 variables //----------------------------------------------------- unsigned long blinkTimer = 0; bool blinkState = false; //----------------------------------------------------- // Effect 2 variables //----------------------------------------------------- unsigned long waveTimer = 0; int waveIndex = 0; //----------------------------------------------------- // Effect 3 variables //----------------------------------------------------- unsigned long insideTimer = 0; int insideStep = 0; //----------------------------------------------------- // Effect 4 variables //----------------------------------------------------- unsigned long danceTimer = 0; //===================================================== void setup() { Serial.begin(115200); pinMode(SOUND_PIN, INPUT); for(int i=0;i<NUM_LEDS;i++) { pinMode(LED_PINS[i], OUTPUT); digitalWrite(LED_PINS[i], HIGH); // OFF } randomSeed(analogRead(2)); // GPIO2 (A0), left unconnected Serial.println("System Ready"); } //===================================================== void loop() { detectClaps(); runEffect(); } //===================================================== // CLAP DETECTION //===================================================== void detectClaps() { bool soundValue = digitalRead(SOUND_PIN); unsigned long now = millis(); // Falling edge detection if(lastSound == HIGH && soundValue == LOW) { if(now - lastClapTime > CLAP_DEBOUNCE) { clapCount++; lastClapTime = now; waitingSequence = true; Serial.print("Clap: "); Serial.println(clapCount); } } lastSound = soundValue; // Sequence complete if(waitingSequence && (now - lastClapTime > CLAP_TIMEOUT)) { if(clapCount >= 1 && clapCount <= 4) { currentEffect = clapCount; Serial.print("Effect = "); Serial.println(currentEffect); resetEffectState(); } clapCount = 0; waitingSequence = false; } } //===================================================== void resetEffectState() { turnOffAll(); blinkState = false; waveIndex = 0; insideStep = 0; blinkTimer = waveTimer = insideTimer = danceTimer = millis(); } //===================================================== void turnOffAll() { for(int i=0;i<NUM_LEDS;i++) { digitalWrite(LED_PINS[i], HIGH); } } //===================================================== void runEffect() { switch(currentEffect) { case 1: effectBlinkAll(); break; case 2: effectWaterWave(); break; case 3: effectInsideOut(); break; case 4: effectDance(); break; default: break; } } //===================================================== // EFFECT 1 //===================================================== void effectBlinkAll() { if(millis() - blinkTimer >= 200) { blinkTimer = millis(); blinkState = !blinkState; for(int i=0;i<NUM_LEDS;i++) { digitalWrite( LED_PINS[i], blinkState ? LOW : HIGH ); } } } //===================================================== // EFFECT 2 //===================================================== void effectWaterWave() { if(millis() - waveTimer >= 70) { waveTimer = millis(); turnOffAll(); digitalWrite( LED_PINS[waveIndex], LOW ); waveIndex++; if(waveIndex >= NUM_LEDS) { waveIndex = 0; } } } //===================================================== // EFFECT 3 //===================================================== void effectInsideOut() { if(millis() - insideTimer >= 120) { insideTimer = millis(); turnOffAll(); switch(insideStep) { case 0: digitalWrite(LED_PINS[3], LOW); digitalWrite(LED_PINS[4], LOW); break; case 1: digitalWrite(LED_PINS[2], LOW); digitalWrite(LED_PINS[5], LOW); break; case 2: digitalWrite(LED_PINS[1], LOW); digitalWrite(LED_PINS[6], LOW); break; case 3: digitalWrite(LED_PINS[0], LOW); digitalWrite(LED_PINS[7], LOW); break; } insideStep++; if(insideStep > 3) { insideStep = 0; } } } //===================================================== // EFFECT 4 //===================================================== void effectDance() { if(millis() - danceTimer >= 80) { danceTimer = millis(); turnOffAll(); int a = random(NUM_LEDS); int b = random(NUM_LEDS); int c = random(NUM_LEDS); digitalWrite(LED_PINS[a], LOW); digitalWrite(LED_PINS[b], LOW); digitalWrite(LED_PINS[c], LOW); } }

FAQ

Can I power the sound sensor from 5V on the ESP32 S3 Uno?

The module itself works at 5V, but then its output pins also give 5V. ESP32 S3 Uno GPIO pins are not 5V tolerant, so a 5V signal can damage them. Power the sensor from the 3.3V pin, or use a voltage divider on the output.

What range does analogRead() give for the analog sound sensor on the ESP32 S3 Uno?

The ESP32 S3 Uno has a 12-bit ADC, so analogRead() returns 0 to 4095, not 0 to 1023 like on many Arduino boards. Connect AO to an analog header such as A0 (GPIO2) and use analogRead(2) in your code. Also add analogSetAttenuation(ADC_11db); in setup(). It sets the ADC range to about 0–3.3V, so the full sensor signal fits in 0 to 4095.

Why does my code print "detected" when the pin is LOW?

The sensor output is inverted. It is HIGH in silence and LOW when it hears a sound. That is why the code looks for a change from HIGH to LOW to detect the start of a sound.

Can the sound sensor recognize words or voices?

No. It only tells you that a sound is louder than the threshold you set. To recognize speech, you need a microphone module and a voice recognition module or library.

Which ESP32 S3 Uno pin should I use for the sound sensor?

Any normal digital pin works for the OUT or DO pin, such as D8 (GPIO21) in this tutorial. Avoid D0 and D1, which are used for USB serial, and avoid D6 (GPIO3) and D9 (GPIO46) if you can, because they are boot strapping pins.

What if my project needs more pins than the Uno headers give?

The ESP32 S3 Uno has two extra rows of holes with more GPIOs. GPIO15 and GPIO16 support I/O, PWM and analog. GPIO45 and GPIO35 to GPIO42 support I/O and PWM. GPIO47 and GPIO48 are output only, with PWM. Solder pin headers to use them and write the GPIO number in your code. For an extra analog sound sensor, use GPIO15 or GPIO16. For more digital sound sensors, use GPIO35 to GPIO42, and never GPIO47 or GPIO48 because they cannot be inputs. Keep two warnings in mind. First, GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Troubleshooting

Problem Possible Cause Solution
No COM port shows up in Arduino IDE USB driver missing or board not in upload mode Install the CP210x or CH340 driver, use a data USB cable, or hold BOOT while pressing RESET
Nothing prints on the Serial Monitor Wrong baud rate Set the Serial Monitor to 9600 baud
"detected" never prints when you clap Sensitivity too low or OUT wire on the wrong pin Turn the potentiometer and check that OUT goes to D8 (GPIO21)
Messages print all the time in silence Sensitivity too high or power noise Turn the potentiometer the other way and use short wires and a stable supply
Sensor works for a while then the GPIO stops responding Sensor powered from 5V so OUT gives 5V to a 3.3V pin Power the sensor from 3.3V or add a voltage divider
Power LED on the module is off VCC or GND not connected Check the 3.3V and GND wires
Board does not boot or upload with the demo wired LEDs on strapping pins D6 (GPIO3) or D9 (GPIO46) Unplug D6 and D9 while uploading, then reconnect

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!