Tuesday, March 6, 2012

Using Condenser Mics with Arduino + OPAMP

Whether for a DIY clapper or an autonomous robot, condenser mics are great for use with a Arduino. But, they don't output high enough voltage for the Arduino to detect (the loudness of the sound the mix picks up varies the voltage it outputs). So, i used use a LM386 OPAMP to boost the output. Just for fun i used it to trigger an LED, which could be replaced with something fancier, say a MOC/TRIAC and a lamp etc.
Scheme
Code:

/*
 * Monitor for sound sensor
 */

int micPin = 2;    // select the input pin
int ledPin = 13;   // select the pin for the LED
int val = 0;
int amp = 0;

void setup() {
  pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
  Serial.begin(9600);
}

void loop() {
  val = analogRead(micPin);
  amp = (val >= 512) ? val - 512 : 512 - val; //it is a ternary operator. X = A ? B : C; if A is true X = B, otherwise C
  if (amp > 100)
  {
    digitalWrite(ledPin, HIGH);
    delay(10);
  }
  else
  {
    digitalWrite(ledPin, LOW);
  }
  Serial.println(amp);
}

No comments:

Post a Comment