ESP32 - Sound Sensor - LED

In this tutorial, we'll explore how to utilize the sound sensor for LED control. Specifically, we'll delve into two exciting applications:

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Sound Sensor
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to LED and Sound Sensor

If you do not know about led and sound sensor (pinout, how it works, how to program ...), learn about them in the following tutorials:

Wiring Diagram

ESP32 Sound Sensor LED Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code - Sound Switch toggles LED

The below code toggles the state of LED each time the sound is detected.

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-sound-sensor-led */ #define SENSOR_PIN 25 // The ESP32 pin GPIO25 connected to the sound sensor #define LED_PIN 26 // The ESP32 pin GPIO26 connected to the LED int prev_sound_state; // the previous state of sound sensor int sound_state; // the current state of sound sensor int led_state = LOW; // the current state of LED void setup() { Serial.begin(9600); // initialize serial pinMode(SENSOR_PIN, INPUT); // set ESP32 pin to input mode pinMode(LED_PIN, OUTPUT); // set ESP32 pin to output mode sound_state = digitalRead(SENSOR_PIN); } void loop() { prev_sound_state = sound_state; // save the last state sound_state = digitalRead(SENSOR_PIN); // read new state if (prev_sound_state == HIGH && sound_state == LOW) { // state change: HIGH -> LOW Serial.println("The sound has been detected"); // toggle state of LED led_state = !led_state; // control LED arccoding to the toggled state digitalWrite(LED_PIN, led_state); } }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Connect ESP32 to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
Arduino IDE Upload Code
  • Clap your hand in front of the sound sensor
  • See the change of LED's state

Code Explanation

Read the line-by-line explanation in comment lines of source code!

ESP32 Code - Sound-activated LED for a period of time

The below code turns on LED for a period of time when the sound is detected. After the period of time, LED is turned off.

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-sound-sensor-led */ #define SENSOR_PIN 25 // The ESP32 pin GPIO25 connected to the sound sensor #define LED_PIN 26 // The ESP32 pin GPIO26 connected to the LED #define TIME_PERIOD 5000 // in milliseconds int prev_sound_state; // the previous state of sound sensor int sound_state; // the current state of sound sensor void setup() { Serial.begin(9600); // initialize serial pinMode(SENSOR_PIN, INPUT); // set ESP32 pin to input mode pinMode(LED_PIN, OUTPUT); // set ESP32 pin to output mode sound_state = digitalRead(SENSOR_PIN); } void loop() { prev_sound_state = sound_state; // save the last state sound_state = digitalRead(SENSOR_PIN); // read new state if (prev_sound_state == HIGH && sound_state == LOW) { // state change: HIGH -> LOW Serial.println("The sound has been detected"); // turn on LED digitalWrite(LED_PIN, HIGH); delay(TIME_PERIOD); // turn off LED digitalWrite(LED_PIN, LOW); } }

Please take note that the previous code utilizes the delay() function, which is straightforward to understand. However, when additional code is added, the delay() function can cause blocking issues during the delay period. To overcome this, the following code implements a non-blocking approach by utilizing the ezLED library. The ezLED library, working behind the scenes, utilizes the millis() function instead of delay to prevent blocking.

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-sound-sensor-led */ #include <ezLED.h> // ezLED library #define SENSOR_PIN 25 // The ESP32 pin GPIO25 connected to the sound sensor #define LED_PIN 26 // The ESP32 pin GPIO26 connected to the LED #define TIME_PERIOD 5000 // in milliseconds ezLED led(LED_PIN); // create a LED object that attach to pin LED_PIN int prev_sound_state; // the previous state of sound sensor int sound_state; // the current state of sound sensor void setup() { Serial.begin(9600); // initialize serial pinMode(SENSOR_PIN, INPUT); // set ESP32 pin to input mode sound_state = digitalRead(SENSOR_PIN); } void loop() { led.loop(); // MUST call the led.loop() function in loop() prev_sound_state = sound_state; // save the last state sound_state = digitalRead(SENSOR_PIN); // read new state if (prev_sound_state == HIGH && sound_state == LOW) { // state change: HIGH -> LOW Serial.println("The sound has been detected"); led.turnON(); // turn on immediately led.turnOFF(TIME_PERIOD); // turn off after TIME_PERIOD } }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Connect ESP32 to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search “ezLED”, then find the led library by ArduinoGetStarted
  • Click Install button to install ezLED library.
ESP32 led library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
Arduino IDE Upload Code
  • Clap your hand in front of the sound sensor
  • See the change of LED's state

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES