Build Project

Home Automation

ESP32 Sound-Triggered Relay Demo

Build an ESP32 sound-triggered relay demo with an INMP441 I2S microphone, peak detection, and a low-voltage relay load. Not speech recognition.

IntermediateAges 13+90-120 minUnder 30 USDParent Safe
Project Mission

Build a Sound-Triggered Relay Demo

The Story

The old title says voice control, but the beginner implementation is clap or loud-sound detection. This page names that honestly and keeps relay use low-voltage. This Golden version keeps the promise narrow: the parts list, code constants, GPIO table, expected output, safety notes, and troubleshooting all describe the same low-voltage educational build.

Explain Like I'm 12

The microphone sends sound samples to the ESP32. The code looks for a big sound peak; when it sees one, it toggles the relay input and LED. The ESP32 is the decision maker: it reads one signal, compares it with simple rules, then changes an output you can see or hear.

Learning Support

  • Recommended ageAges 13+
  • Adult supervisionAdult supervision required for relay modules. Use only low-voltage demo loads on the bench.
  • Classroom useUse to discuss sound thresholds, false triggers, and relay safety boundaries.
  • Parent promptAsk whether the project recognizes words or only detects a loud sound peak.
  • Screen-free activityDraw the signal path from microphone samples to relay state.
  • Next challengeAdd a low-voltage LED strip demo load and a physical override button.
  • Skills practiced
    • I2S microphone input
    • Peak detection
    • Relay module safety
    • Active-low logic
  • Learning outcomes
    • Read an INMP441 microphone with I2S.
    • Detect a loud sound peak instead of speech commands.
    • Toggle an active-low relay module input.
    • Explain why relay isolation is not breadboard mains safety.
  • Mini experiments
    • Print room-noise peaks and choose a threshold.
    • Clap once and watch the relay toggle.
    • Change the debounce delay and observe double triggers.

Safety Standards

  • Unplug USB before changing jumper wires. Recheck 3.3 V, 5 V, and GND before reconnecting power.
  • Do not power motors, pumps, LED strips, or servos from the ESP32 3.3 V pin. Use a suitable external supply and common ground.
  • Breadboards are for low-current prototypes. Move high-current or unattended builds to proper terminals, enclosure, strain relief, and fusing.
  • Relay and mains-voltage projects require isolation, correct relay ratings, enclosed wiring, and qualified adult supervision.

What You Will Build

A sound-triggered relay demo using an INMP441 I2S microphone, a low-voltage relay module input, and an LED state indicator.

Learning Objectives

  • Read an INMP441 microphone with I2S.
  • Detect a loud sound peak instead of speech commands.
  • Toggle an active-low relay module input.
  • Explain why relay isolation is not breadboard mains safety.

Components List

  • ESP32 DevKit boardUSB-programmable ESP32 board used as the controller.
  • INMP441 I2S microphone module3.3 V digital microphone.
  • Single-channel relay moduleUse only the low-voltage input side for this bench demo.
  • LED with 220 ohm resistorRelay state indicator on GPIO25.
  • Low-voltage demo loadUse an LED module or other safe bench load.

Bill of Materials

PartQtyEstimated CostNotes
ESP32 DevKit board1VariesUSB-programmable ESP32 board used as the controller.
INMP441 I2S microphone module1Varies3.3 V digital microphone.
Single-channel relay module1VariesUse only the low-voltage input side for this bench demo.
LED with 220 ohm resistor1VariesRelay state indicator on GPIO25.
Low-voltage demo load1VariesUse an LED module or other safe bench load.

Wiring

INMP441 uses I2S pins GPIO14, GPIO15, and GPIO32. Relay IN is GPIO26 and LED indicator is GPIO25.

Wiring Diagram ESP32 Voice-Controlled Relay wiring diagram
  1. 1

    Unplug USB and relay supply before rewiring.

  2. 2

    Connect INMP441 SCK to GPIO14, WS to GPIO15, and SD to GPIO32.

  3. 3

    Connect INMP441 VCC to 3.3 V and GND to GND; connect L/R to GND for left channel.

  4. 4

    Connect relay IN to GPIO26 and relay input-side GND to ESP32 GND.

  5. 5

    Connect LED through a 220 ohm resistor to GPIO25 and GND.

  6. 6

    Use only a low-voltage demo load for relay output testing.

GPIO Mapping

SignalESP32 PinDirectionNotes
INMP441 SCK / WS / SDGPIO14 / GPIO15 / GPIO32I/OI2S microphone signals.
Relay INGPIO26OutputActive-low relay input.
LED indicatorGPIO25OutputShows relay state.

Circuit Explanation

The INMP441 sends digital microphone samples over I2S. The code calculates a peak value and toggles GPIO26 when the peak crosses the threshold.

Engineering Explanation

This is sound-triggered control, not speech recognition. Background noise, echoes, and threshold choice affect false triggers.

Libraries

  • Arduino ESP32 coreUses the ESP32 I2S driver included with the core.

Code

Copy into Arduino IDE. Install any libraries noted in the component guides first.

esp32-voice-controlled-relay.ino
// ESP32 sound-triggered relay demo - not speech recognition
#include <driver/i2s.h>

const i2s_port_t I2S_PORT = I2S_NUM_0;
const int RELAY_PIN = 26;  // active LOW relay module input
const int LED_PIN = 25;
const int CLAP_THRESHOLD = 50000; // tune from Serial Monitor peak values
bool relayState = false;

void i2sInit() {
  i2s_config_t cfg = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
    .sample_rate = 16000,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
    .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
    .communication_format = I2S_COMM_FORMAT_STAND_I2S,
    .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
    .dma_buf_count = 4,
    .dma_buf_len = 256,
    .use_apll = false
  };
  i2s_pin_config_t pins = {
    .bck_io_num = 14,
    .ws_io_num = 15,
    .data_out_num = I2S_PIN_NO_CHANGE,
    .data_in_num = 32
  };
  i2s_driver_install(I2S_PORT, &cfg, 0, NULL);
  i2s_set_pin(I2S_PORT, &pins);
}

int32_t peak(int32_t *buf, size_t n) {
  int32_t mx = 0;
  for (size_t i = 0; i < n; i++) {
    int32_t v = abs(buf[i] >> 8);
    if (v > mx) mx = v;
  }
  return mx;
}

void setup() {
  Serial.begin(115200);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, HIGH); // active LOW relay off
  digitalWrite(LED_PIN, LOW);
  i2sInit();
  Serial.println("Sound-triggered relay demo ready. Use a low-voltage demo load.");
}

void loop() {
  int32_t samples[256];
  size_t bytesRead = 0;
  i2s_read(I2S_PORT, samples, sizeof(samples), &bytesRead, portMAX_DELAY);
  int32_t p = peak(samples, bytesRead / sizeof(int32_t));
  Serial.printf("Peak: %d\n", p);
  if (p > CLAP_THRESHOLD) {
    relayState = !relayState;
    digitalWrite(RELAY_PIN, relayState ? LOW : HIGH);
    digitalWrite(LED_PIN, relayState ? HIGH : LOW);
    Serial.printf("Relay: %s\n", relayState ? "ON" : "OFF");
    delay(500);
  }
}

Code Explanation

I2S uses GPIO14, GPIO15, and GPIO32. CLAP_THRESHOLD sets the sound peak needed to toggle the active-low relay input on GPIO26.

Expected Output

Serial Monitor prints Peak values. A loud clap above 50000 toggles Relay ON or OFF and changes the LED state.

Troubleshooting

  • Relay toggles randomly Raise CLAP_THRESHOLD after recording normal room peaks.
  • No sound detected Check INMP441 power and I2S pins.
  • Relay logic is backwards Confirm the module is active LOW and starts with GPIO26 HIGH.

Common Mistakes

  • Calling clap detection full speech recognition.
  • Putting mains voltage on a breadboard.
  • Forgetting active-low relay logic.
  • Setting the threshold before measuring room noise.

Testing Checklist

  • Boot with no relay load connected.
  • Record quiet-room peak values.
  • Clap once and confirm one toggle.
  • Test only a low-voltage demo load.

Engineering Tips

  • Call it sound-triggered unless real speech recognition is added.
  • Keep microphone wires short.
  • Use enclosures and professionals for mains work.

Upgrade Ideas

  • Add a physical override button.
  • Add a low-voltage LED strip demo load.
  • Move to a real speech-recognition module before claiming commands.

Real-World Applications

  • Sound-triggered demo
  • Relay safety lesson
  • I2S microphone threshold experiment

FAQs

Is this speech recognition?

No. The beginner version detects loud sound peaks.

Can I wire a lamp?

Not on a breadboard. Mains work needs rated parts, enclosure, and qualified supervision.

Why is the relay active LOW?

Many relay modules turn on when their IN pin is driven LOW.

Review, Testing, and References

Author: Abdul Mubeen and the ESP32 Engine editorial team. Last updated: 2026-06-27. Reviewed: wiring logic, Arduino code structure, beginner safety, and learning sequence.

Educational level: Intermediate. Estimated completion time: 90-120 min. This project is for learning and prototyping; production or unattended hardware needs additional engineering review.

Project Complete!

You completed ESP32 Voice-Controlled Relay as a trustworthy ESP32 learning build with matching wiring, code, testing, and safety boundaries.

  • I2S microphone input
  • Peak detection
  • Relay module safety