Michael Cartwright
Published © LGPL

MAX9814 as Audio Switch

Simple binary on/off audio switch using an off-the-shelf microphone breakout board. Clapper?

BeginnerProtip1 hour472
MAX9814 as Audio Switch

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
Adafruit MAX9814 - Electret Microphone Amplifier with Auto Gain Control
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

MAX9814 as Audio Switch

Arduino
#define LED_PIN 5    // D1 on Wemos D1 Mini
#define KNOCK_PIN 17 // A0 on Wemos D1 Mini

// Start with a low value and test what works in your environment via the Serial Monitor
const int threshold = 805; 

// Comment this out once you've calibrated your threshold
//#define SERIAL_MONITOR

int inputFromMAX9814 = 0;
bool switchState = false;

void setup() 
{
  pinMode(LED_PIN, OUTPUT);
#ifdef SERIAL_MONITOR  
  Serial.begin(115200); 
#endif
}

void loop() {
  // read the sensor and store it in the variable sensorReading:
  inputFromMAX9814 = analogRead(KNOCK_PIN);

  if (inputFromMAX9814 >= threshold) 
  {
#ifdef SERIAL_MONITOR      
    Serial.println(inputFromMAX9814);
#endif
  switchState = !switchState;
    digitalWrite(LED_PIN, switchState ? HIGH : LOW);

    // this delay ensures that we only trigger our switch once per second ("debouncing")
    delay(1000);
  }
  delay(1);
}

Credits

Michael Cartwright

Michael Cartwright

21 projects • 13 followers

Comments