The microphone module allows you to detect sound.
The module has the following pins:
The threshold-sensitivity can be adjusted via a potentiometer on the board
To wire your Microphone module to your Arduino, connect the following pins:
int led= 13; // 'led' is the Arduino onboard LED int mic= 3; // 'mic' is the Arduino pin 3 = the digital output pin of the Microphone board (D0) int val = 0; // 'val' is used to store the digital microphone value void setup () { pinMode (led, OUTPUT) ; // configure 'led' as output pin pinMode (mic, INPUT) ; // configure 'mic' as input pin } void loop () { val = digitalRead(mic); // read value if (val == HIGH) // if he value is high then light the LED or else do not light the LED { digitalWrite (led, HIGH); } else { digitalWrite (led, LOW); } }
Example 1
const int sensorPin = A0; int sensorValue = 0; void setup() { Serial.begin(9600); pinMode(sensorPin, INPUT); } void loop() { sensorValue = analogRead(sensorPin); Serial.println(sensorValue); }
Example 2
int led= 13; // 'led' is the Arduino onboard LED int mic= A0; // 'mic' is the Arduino pin A0 = the analog output pin of the Microphone board (A0) int val = 0; // variable to store the analog microphone value void setup () { pinMode (led, OUTPUT); Serial.begin (9600); } void loop () { sensorValue = analogRead (mic); digitalWrite (led, HIGH); delay (500); digitalWrite (led, LOW); delay (500); Serial.println (val, DEC); }
Example 3
Measure the volume for 50 milliseconds. Values can range from 0 to 1023. Calculate the difference between the minimum and maximum volume level. Convert this to a voltage (0Vdc – 5Vdc). This represents the difference between minimal and maximum voltage on pin A0 as it occurred in the sample window.
/**************************************** Example Sound Level Sketch for the Adafruit Microphone Amplifier ****************************************/ const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz) unsigned int sample; void setup() { Serial.begin(9600); } void loop() { unsigned long startMillis= millis(); // Start of sample window unsigned int peakToPeak = 0; // peak-to-peak level unsigned int signalMax = 0; unsigned int signalMin = 1024; // collect data for 50 mS while (millis() - startMillis < sampleWindow) { sample = analogRead(0); if (sample < 1024) // toss out spurious readings { if (sample > signalMax) { signalMax = sample; // save just the max levels } else if (sample < signalMin) { signalMin = sample; // save just the min levels } } } peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude double volts = (peakToPeak * 5.0) / 1024; // convert to volts Serial.println(volts); }
The clap detector listens for a clap (and lights up a LED), then waits for a second clap (and lights up a second LED). If the second clap is within a set amount of time it will turn off the previous LEDs and light a third LED to indicate a valid clap sequence was detected.
You can expand the circuit to listen for additional claps for more complex clap sequences.
The program outputs all data to the serial port and adjusts input levels by measuring the ambient noise levels first.
/* * Clapper project * Author: Manoj Kunthu * Update: 2/2/13 */ /*----------------------------- * Method Prototypes *-----------------------------*/ void initialize(); void runDetector(); boolean clapDetected(); int detectClaps(int numClaps); void indicateClaps(); void readMic(); void printStats(); /*----------------------------- * Variable Declarations *-----------------------------*/ int TOTAL_CLAPS_TO_DETECT = 2; //The number of claps detected before output is toggled int offset = 80; // The point above average that the clap is detected int CLAP_TIME=4000; // The time allowed between each clap int sensorValue = 0; //the value read through mic int toggleOutput = -1; int SIZE = 3; int buffer[3]; int loopIteration = 0; int average = 0; int total = 0; //BOARD INPUT MIC const int inPin0 = A0; //BOARD OUTPUT SIGNALS const int clapLed1 = 12, clapLed2 = 11, out = 10, readyPin = 13; //CLAP STATE CONSTANTS const int FINAL_DETECTED = 0, LOST_CONTINUITY = 1, CLAP_NOT_DETECTED = 2; void setup() { Serial.begin(9600); //direct representation of the light bulb that toggles on/off pinMode(out, OUTPUT); //once initialize() runs the ready pin turns on pinMode(readyPin, OUTPUT); //respective clap LEDs, more can be added pinMode(clapLed1, OUTPUT); pinMode(clapLed2, OUTPUT); } void loop() { initialize(); runDetector(); } /** * Purpose: Prepares the buffer to recognize ambient noise levels in room. */ void initialize() { loopIteration = 0; total = 0; average =0; digitalWrite(clapLed1, LOW); digitalWrite(clapLed2, LOW); digitalWrite(out, LOW); for(int i = 0; i < SIZE; i++) { readMic(); buffer[i] = sensorValue; total = total + sensorValue; average = (total/(i+1)); Serial.print("INIT - AVE: "); Serial.print(average); Serial.print(" Total: "); Serial.print(total); Serial.print(" Sensor: "); Serial.print(sensorValue); Serial.print(" Change: "); Serial.println(sensorValue-average); delay(50); } digitalWrite(readyPin, HIGH); } /** * Purpose: Runs the detector algorithm. Developers can change the number of claps by * adjusting TOTAL_CLAPS_TO_DETECT variable up at the top. */ void runDetector() { while(true) { int clapState = detectClaps(TOTAL_CLAPS_TO_DETECT); if(clapState == FINAL_DETECTED || clapState == LOST_CONTINUITY) { Serial.println("--done--"); indicateClap(0);//turn off any clap indicating lights } } } /** * Purpose: Detects the number of claps specified. This method is recursive */ int detectClaps(int numClaps) { int clapNum = numClaps; //Base Case - if clapNum is 0, then all claps have been accounted. if(clapNum == 0) { //the output can now be toggled. toggleOutput *= -1; indicateClap(clapNum); Serial.println("----- Clap Limit Reached - Output Toggled -----"); return FINAL_DETECTED; } //Read from mic and update ambient noise levels. readMic(); total = (total - buffer[loopIteration]) + sensorValue; average = (total/SIZE); buffer[loopIteration] = sensorValue; loopIteration = (loopIteration+1)%SIZE; if(clapDetected()) { Serial.print("detectClaps - Claps:"); Serial.println(TOTAL_CLAPS_TO_DETECT + 1 - numClaps); printStats(); indicateClap(clapNum); delay(100); for(int i = 0; i < CLAP_TIME; i++) { int clapState = detectClaps(clapNum - 1); if(clapState == FINAL_DETECTED || clapState == LOST_CONTINUITY) { return clapState; } } return LOST_CONTINUITY; } return CLAP_NOT_DETECTED; } /** * Purpose: Turns the LED on appropriately to signal a clap detection. */ void indicateClap(int clapNum) { if(clapNum == 0) { if(toggleOutput == 1) { digitalWrite(out, HIGH); } else { digitalWrite(out, LOW); } digitalWrite(clapLed1, LOW); digitalWrite(clapLed2, LOW); } else if(clapNum == 1) { digitalWrite(clapLed1, HIGH); } else if(clapNum == 2) { digitalWrite(clapLed2, HIGH); } delay(110); } /** * Purpose: Prints basic statistics data for more info with sensor readouts and data points. */ void printStats() { Serial.print("--- AVE: "); Serial.print(average); Serial.print(" Total: "); Serial.print(total); //Serial.print(" iterNum: "); //Serial.print(loopIteration); Serial.print(" Sensor: "); Serial.print(sensorValue); Serial.print(" Change: "); Serial.println(sensorValue-average); //This is what I used to determine the 'offset' value } /** * Purpose: A clap is detected when the sensor value is greater than the average plus * an offset. The offset might need to be fine tuned for different sound sensors. */ boolean clapDetected() { return sensorValue > average + offset; } /** * Purpose: Reads mic input and stores it in a global variable. */ void readMic() { sensorValue = analogRead(inPin0); }