ESP32 Sound-Reactive RGB Light

by drahul1103 in Circuits > Arduino

109 Views, 1 Favorites, 0 Comments

ESP32 Sound-Reactive RGB Light

WIN_20260708_19_26_09_Pro.jpg

This project combines a sound sensor and RGB LED to create a color-changing light that responds to noise. Perfect for learning analog input, PWM output, and basic state management on the ESP32.

Supplies

What You'll Need:

  1. ESP32 (Microcontroller)
  2. Keyestudio Sound Sensor (KY-038) (Analog microphone with A0 output)
  3. KY-016 RGB LED (Common cathode, 5V tolerant)
  4. Breadboard (Solderless prototyping board)
  5. Jumper Wires (Male-to-male connectors)
  6. USB Cable (For programming the ESP32)

Wire the Circuit

Follow these connections carefully:

Sound Sensor → ESP32

  1. S (Signal) → GPIO 34 (ADC input)
  2. (Power) → 5V
  3. (Ground) → GND

RGB LED → ESP32

  1. R (Red) → GPIO 21 (PWM)
  2. G (Green) → GPIO 22 (PWM)
  3. B (Blue) → GPIO 23 (PWM)
  4. (Ground) → GND (Common cathode)


Upload the Code

Copy the code below into the Arduino IDE.

Select Tools → Board → ESP32 Dev Module.

Plug in the ESP32 via USB and select the COM port.

Click Upload and wait for the compile to finish.

Understanding the Code

Audio Input & Filtering

The code reads the analog microphone on GPIO34. Because the sensor outputs noisy values, a simple exponential moving average filter (SMOOTH = 0.4) smooths the signal. This prevents the LED from flickering on every tiny sound fluctuation.

Three-Zone Color Logic

  1. Green (calm): Sound level below 300
  2. Yellow (getting loud): Sound level 300–800
  3. Red (too loud): Sound level above 800

Color Hold Timer

When a sound level crosses a threshold, the corresponding color "latches" on for 2000 milliseconds (HOLD_MS). This prevents the color from jumping around constantly and gives a more satisfying visual effect. After 2 seconds of silence, it resets to green.

Testing & Tuning


  1. Open Tools → Serial Monitor (115200 baud) to see raw sensor values.
  2. Snap your fingers near the mic and watch the numbers jump.
  3. If colors aren't triggering at the right times, adjust YELLOW_LEVEL and RED_LEVEL in the code. Lower numbers = more sensitive.
  4. If the LED flickers too much, increase SMOOTH (0.4 → 0.6) for more filtering.

Example Tuning Guide:

  1. Too sensitive → Triggers at whispers → Raise YELLOW_LEVEL
  2. Not sensitive enough → Needs loud claps → Lower YELLOW_LEVEL
  3. Colors too fast → Flickers constantly → Increase SMOOTH
  4. Colors too slow → Sluggish response → Decrease SMOOTH


Video Demo

sound sensitive LED project