ESP32 - Sound Sensor - Relay

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

By connecting relay to light bulb, led strip, motor or actuator... We can use the sound sensor to control light bulb, led strip, motor or actuator...

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Sound Sensor
1×Relay
1×Jumper Wires
1×(Optional) Solenoid Lock
1×(Optional) 12V Power Adapter
1×(Optional) DC Power Jack
1×(Recommended) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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 Relay and Sound Sensor

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

Wiring Diagram

ESP32 Sound Sensor Relay 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 Relay

The below code toggles the state of relay 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-relay */ #define SENSOR_PIN 25 // The ESP32 pin GPIO25 connected to the sound sensor #define RELAY_PIN 26 // The ESP32 pin GPIO26 connected to the relay int prev_sound_state; // the previous state of sound sensor int sound_state; // the current state of sound sensor int relay_state = LOW; // the current state of relay void setup() { Serial.begin(9600); // initialize serial pinMode(SENSOR_PIN, INPUT); // set ESP32 pin to input mode pinMode(RELAY_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 relay relay_state = !relay_state; // control relay arccoding to the toggrelay state digitalWrite(RELAY_PIN, relay_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 the relay's state

Code Explanation

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

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

The below code turns on the relay for a period of time when the sound is detected. After the period of time, the relay 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-relay */ #define SENSOR_PIN 25 // The ESP32 pin GPIO25 connected to the sound sensor #define RELAY_PIN 26 // The ESP32 pin GPIO26 connected to the relay #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(RELAY_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 relay digitalWrite(RELAY_PIN, HIGH); delay(TIME_PERIOD); // turn off relay digitalWrite(RELAY_PIN, LOW); } }

Please take note that the code mentioned above utilizes the delay() function for simplicity. However, if you incorporate additional code, it may get blocked during the delay time. To address this, the following code implements a non-blocking approach using the ezLED library. Behind the scenes, the ezLED library employs 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-relay */ #include <ezLED.h> // ezLED library #define SENSOR_PIN 25 // The ESP32 pin GPIO25 connected to the sound sensor #define RELAY_PIN 26 // The ESP32 pin GPIO26 connected to the relay #define TIME_PERIOD 5000 // in milliseconds ezLED relay(RELAY_PIN); // create a relay object that attach to pin RELAY_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() { relay.loop(); // MUST call the relay.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"); relay.turnON(); // turn on relay immediately relay.turnOFF(TIME_PERIOD); // turn off relay 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 ESP32GetStarted
  • 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 the relay'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