LM393 Sound Sensor with ESP32: Build a WiFi Noise Monitor
Wire an LM393 sound sensor to ESP32, detect noise events, and send alerts over WiFi. Includes complete Arduino and MicroPython code, plus Home Assistant integration via ESPHome.
What You Need
- ✓ ESP32 DevKit (any version)
- ✓ LM393 Sound Sensor Module(~$1.20)
- ✓ 3 jumper wires
- ✓ USB cable + Arduino IDE or Thonny
Wiring Diagram
LM393 Sound Sensor ESP32 ────────────────── ───── VCC ────────────── 3.3V GND ────────────── GND AO ────────────── GPIO 34 (ADC1_CH6) DO ────────────── GPIO 4
⚠️ Use GPIO 34 or 35 for analog input. These are ADC1 channels that work reliably even when WiFi is active. Avoid GPIO 2 (has pull-down resistor).
Arduino Code (ESP32)
// LM393 Sound Sensor with ESP32
// Detects sound events and prints to Serial Monitor
// Tutorial: https://comparator.online/tutorials/lm393-esp32-sound-sensor/
const int analogPin = 34; // AO to GPIO 34
const int digitalPin = 4; // DO to GPIO 4
const int ledPin = 2; // Built-in LED
// Sound detection threshold (tune to your environment)
const int SOUND_THRESHOLD = 1500;
void setup() {
Serial.begin(115200);
pinMode(digitalPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.println("ESP32 LM393 Sound Sensor Ready");
}
void loop() {
int analogValue = analogRead(analogPin);
int digitalValue = digitalRead(digitalPin);
// Detect sound event (analog above threshold)
if (analogValue > SOUND_THRESHOLD) {
Serial.print("🔊 Sound detected! Level: ");
Serial.println(analogValue);
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
// Print digital state
if (digitalValue == HIGH) {
Serial.println("⚠️ Threshold triggered (DO=HIGH)");
}
delay(50);
}MicroPython Code (ESP32)
# LM393 Sound Sensor with ESP32 - MicroPython
from machine import Pin, ADC
import time
analog_pin = ADC(Pin(34)) # AO on GPIO 34
analog_pin.atten(ADC.ATTN_11DB) # Full range 0-3.3V
digital_pin = Pin(4, Pin.IN) # DO on GPIO 4
led = Pin(2, Pin.OUT) # Built-in LED
SOUND_THRESHOLD = 1500
print("ESP32 Sound Sensor Ready")
while True:
level = analog_pin.read()
triggered = digital_pin.value() == 1
if level > SOUND_THRESHOLD or triggered:
print(f"Sound! Level={level}, Trigger={triggered}")
led.on()
else:
led.off()
time.sleep_ms(50)Home Assistant Integration (ESPHome)
Use ESPHome to expose sound events to Home Assistant via MQTT:
# ESPHome configuration for LM393 sound sensor
esphome:
name: esp32-sound-sensor
platform: ESP32
board: esp32dev
wifi:
ssid: "YourWiFi"
password: "YourPassword"
api:
encryption:
key: "your-api-key"
binary_sensor:
- platform: gpio
pin:
number: GPIO4
inverted: true
name: "Sound Detected"
device_class: sound
sensor:
- platform: adc
pin: GPIO34
name: "Sound Level"
update_interval: 1s
filters:
- sliding_window_moving_average:
window_size: 15
send_every: 153 Project Ideas
Baby Monitor
Get push notifications when baby cries. Combine with ESP32 WiFi for phone alerts.
Workshop Noise Alarm
Alert when power tools run too long. Helps protect hearing in loud environments.
Noise Logger
Log sound levels over time. Send to InfluxDB + Grafana for beautiful dashboards.
Frequently Asked Questions
What sound level can the LM393 sound sensor detect?
The LM393 sound sensor detects sounds above a threshold you set (typically 50-70 dB). It outputs a digital HIGH when sound exceeds the threshold, and an analog signal proportional to amplitude.
Can ESP32 read the analog output from the sound sensor?
Yes. ESP32 has a 12-bit ADC (0-4095). Connect AO to GPIO 34 or 35 (ADC1 channels, which work even when WiFi is active). Avoid GPIO 2 because it has a pull-down resistor.
How accurate is the LM393 sound sensor for measuring dB?
It is not a calibrated decibel meter. For accurate SPL measurement, use an INMP441 or SPH0645 I2S microphone. The LM393 sensor is good for threshold detection (loud/quiet) but not precise dB readings.
Can I integrate this with Home Assistant?
Yes. Use ESPHome firmware on the ESP32 to expose the sound level as a binary sensor to Home Assistant via MQTT. The LM393 DO pin triggers when sound exceeds your threshold.
Design Smarter Circuits
Use our free Hysteresis Calculator to design the comparator circuit itself. Calculate threshold voltages and resistor values for your LM393-based designs.
Try the Calculator →