Index
Introduction
The sound sensor module is a great tool for detecting sound levels and is perfect for projects like sound-activated lights or alarms.
It features a microphone and provides an analog output that reflects the ambient sound intensity.
This guide will show you how to connect the sound sensor to an ESP32 and read sound level values accurately using the ADC (analog-to-digital converter).
Required Components
- ESP32 Board
- Sound Sensor
- LED
- Jumper Wires
- Breadboard (optional)
Pinout

Circuit Diagram / Wiring
- SOUND SENSOR
- SOUND SENSOR VCC → VIN, 3.3V (ESP32)
- SOUND SENSOR GND → GND (ESP32)
- SOUND SENSOR AO (Analog Output) → Pin 34 (ESP32)
- LED
- Connect the longer leg (anode) of the LED to digital pin 13 through a 220-ohm resistor.
- Connect the shorter leg (cathode) to the GND pin.

Code / Programming
const int LED = 13; // Use any valid GPIO pin for the LED (e.g., 13)
const int Sensor = 34; // Use an ADC-capable pin on ESP32 (e.g., GPIO 34)
int level;
const int threshold = 640; // Adjust based on your sound sensor's response
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(115200);
}
void loop() {
level = analogRead(Sensor); // Read sound level
Serial.println(level); // Print value for debugging
if (level > threshold) {
digitalWrite(LED, HIGH); // Turn on LED if sound level is high
delay(1000);
digitalWrite(LED, LOW);
} else {
digitalWrite(LED, LOW);
}
}
Explanation
- The sound sensor detects ambient sound levels and sends an analog voltage to the ESP32.
- The ESP32 reads this value using its ADC pin (e.g., GPIO 34) and compares it to a threshold.
- If the sound level exceeds the threshold, the LED turns ON as an alert or response.
Troubleshooting
- Make sure the sensor is connected to an ADC-capable pin (e.g., GPIO 34, 35, 32, or 33).
- Use Serial Monitor to observe real-time analog values and adjust the threshold accordingly.
- If the LED doesn’t respond, verify the sensor’s VCC and GND connections and confirm the LED is wired correctly.
Project 1:
Introduction
This project uses an ESP32 and a sound sensor (microphone) to visualize real-time sound waveforms on a web interface.
The ESP32 reads analog sound data from the microphone, samples it, and sends it to a browser via WiFi.
A built-in web server displays the audio signal as a live waveform on a canvas using HTML and JavaScript.
This is ideal for learning about audio sampling, analog sensors, and web-based IoT visualization.
It’s a creative and educational project combining sound sensing and real-time web visualization.
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
#define MIC_PIN 34 // Analog input pin for microphone
#define SAMPLE_COUNT 200
int samples[SAMPLE_COUNT];
WebServer server(80);
void handleRoot() {
String html = R"rawliteral(
<!DOCTYPE html><html><head><meta charset="UTF-8">
<title>ESP32 Audio Waveform</title>
<style>
body { background:#111; color:#eee; text-align:center; font-family:sans-serif; }
canvas { border:1px solid #444; margin-top:20px; background:#222; }
</style></head><body>
<h2>ESP32 Audio Waveform</h2>
<canvas id="wave" width="400" height="150"></canvas>
<script>
const canvas = document.getElementById("wave");
const ctx = canvas.getContext("2d");
async function fetchWave() {
const res = await fetch("/data");
const data = await res.json();
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
for (let i = 0; i < data.length; i++) {
let x = i * (canvas.width / data.length);
let y = canvas.height - (data[i] / 4095) * canvas.height;
if (i == 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.strokeStyle = "#00ff88";
ctx.stroke();
}
setInterval(fetchWave, 500); // update every 500ms
</script>
</body></html>
)rawliteral";
server.send(200, "text/html", html);
}
void handleData() {
// Sample audio values
for (int i = 0; i < SAMPLE_COUNT; i++) {
samples[i] = analogRead(MIC_PIN);
delayMicroseconds(300); // ~3.3 kHz sampling
}
// Send as JSON array
String json = "[";
for (int i = 0; i < SAMPLE_COUNT; i++) {
json += String(samples[i]);
if (i < SAMPLE_COUNT - 1) json += ",";
}
json += "]";
server.send(200, "application/json", json);
}
void setup() {
Serial.begin(115200);
pinMode(MIC_PIN, INPUT);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500); Serial.print(".");
}
Serial.println("\nConnected! IP: " + WiFi.localIP().toString());
server.on("/", handleRoot);
server.on("/data", handleData);
server.begin();
}
void loop() {
server.handleClient();
}
Explanation
- The ESP32 reads analog values from a microphone using
analogRead()
and samples audio in real-time. - It hosts a web server that sends sampled audio data to a browser in JSON format.
- The browser displays the waveform on an HTML canvas using JavaScript for visualization.
Troubleshooting
- No waveform on browser: Ensure ESP32 is connected to WiFi and use the correct IP shown in Serial Monitor.
- Flat or noisy line: Check MIC pin wiring, and try a different analog pin (like GPIO 34 or 36).
- Laggy updates: Reduce
SAMPLE_COUNT
or increasedelayMicroseconds()
to balance performance.