Introduction: DIY Snapper - Snap Activated Light - With ESP32

This turns on and off an LED by listening to a snap sound. The sound board is calibrated to listen to a snap, and toggles an LED on and off.

The Sparkfun Sound Detector board is quite special and different from all the other sound detector boards available in the market since it possesses three different output pins, where the user can customize the type of signal they want to receive, through three options: Gate, Envelope, and Audio.

This snapper uses the Gate pin to process audio signals.

Step 1: Tools and Materials

  • ESP32 Development Board
  • Sound Detector Board
  • 5mm LED
  • 100Ω resistor
  • 5 pieces of jumper wires

Step 2: Circuitry

  1. Connect the "Gate" pin from the Sound Detector Board to the pin D2 or IO24 on the ESP32.

The gate pin outputs a digital binary signal whether a relatively loud sound has been detected. We are using this pin as opposed to the envelope or audio pin so that programming will be simpler and that we will receive a cleaner signal from the sound board.

2. Connect the VCC pin from the sound detector board to the "3V3" pin on the ESP32 board.

3. Connect the GND pin from the sound detector board to the "GND" on the ESP32 board.

Step 3: Coding

/**
 * Snapper by Tech Martian
 */

const int soundGate = 2;        //declare the binary gate pin of the sound detector
const int ledPin = 5;           // declare the LED pin.
int toggle = 0;                 // initialize a toggle variable to be not-toggled
void setup() {
 
  pinMode (soundGate, INPUT);  // declare the soundGate pin to be a digital output pin
  pinMode (ledPin, OUTPUT);     // declare the ledPin to be a digital output pin
}
void loop() {
  if (digitalRead(soundGate) == HIGH) { // if a clap is detected
      if (toggle == 0) {                // check if the LED has already been toggled off
          digitalWrite (ledPin, HIGH);  // turn on LED
          toggle = 1;                   // toggle to turn off next clap
      } else {                          // otherwise turn off LED
          digitalWrite (ledPin, LOW);
          toggle = 0;                   // toggle to turn on next clap
      }
  }
}

Step 4: Video Demonstration

Snap Snap! [Light turn on]

Snap Snap! [Light turn off]

Oddly enough, there's a lot of satisfaction that can be gained by watching this circuit work!