Advertisement
kowalyshyn_o

Microphone Sensor

Feb 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1.  /*
  2.  * The Circuit:
  3.  * Connect AUD to analog A4 (as input)
  4.  * Connect LED to analog A5 (as output)
  5.  * Connect GND to GND
  6.  * Connect VCC to 3.3V or 5.0V (3.3V yields the best results)
  7.  * 3.3V -- quiet sensor signal approx 310
  8.  * 5.0V -- quiet sensor signal approx 520
  9.  *  
  10.  * To adjust when the LED turns on based on audio input:
  11.  * Open up the serial com port (Top right hand corner of the Arduino IDE)
  12.  * It looks like a magnifying glass. Perform several experiments
  13.  * clapping, snapping, blowing, door slamming, knocking etc and see where the
  14.  * resting noise level is and where the loud noises are. Adjust the if statement
  15.  * according to your findings.
  16.  *  
  17.  * You can also adjust how long you take samples for by updating the "SampleWindow"
  18.  *
  19.  * This code has been adapted from the
  20.  * Example Sound Level Sketch for the
  21.  * Adafruit Microphone Amplifier
  22.  */
  23.  
  24. const int sampleWindow = 250;         //Sample window width in mS (250 mS = 4Hz)
  25. const float referenceVoltage = 3.28;  //Set board operating voltage
  26. const int threshold = 1.00;           //Sound level to trigger LED (in volts)
  27.  
  28. int knock;                            //Variable to store sensor value
  29. int ledPin = A5;                      //Define LED pin
  30. int micPin = A4;                      //Define Microphone pin
  31.  
  32. void setup()
  33. {
  34.   Serial.begin(9600);
  35.   pinMode(ledPin, OUTPUT);
  36.   Serial.println(F("\nGetting Ready..."));
  37.   delay(3000);
  38. }
  39.  
  40. void loop()
  41. {
  42.   unsigned long start= millis();       // Start of sample window
  43.   unsigned int peakToPeak = 0;         // peak-to-peak level
  44.  
  45.   unsigned int signalMax = 0;          // Define maximum signal as 0 to start
  46.   unsigned int signalMin = 1024;       // Define minimum signal as 1024 to start
  47.  
  48.   //collect audio signal data for 250 miliseconds
  49.  
  50.   while (millis() - start < sampleWindow)
  51.   {
  52.        knock = analogRead(micPin);
  53.      
  54.          if (knock > signalMax)
  55.           {
  56.           signalMax = knock;          // save just the max levels
  57.           }
  58.        
  59.          else if (knock < signalMin)
  60.           {
  61.           signalMin = knock;  // save just the min levels
  62.           }
  63.          
  64.   } //end while
  65.  
  66.   peakToPeak = signalMax - signalMin;                         // calculate max - min = peak-to-peak amplitude
  67.   float volts = (peakToPeak * referenceVoltage) / 1024;       // convert to volts
  68.   Serial.println("Signal: "+String(knock)+"   Peak Voltage: "+String(volts));
  69.  
  70.   if (volts >= threshold)
  71.     {
  72.       //turn on LED for 500ms
  73.       digitalWrite(ledPin, HIGH);
  74.       delay(500);
  75.       Serial.println(F("\n\nKnock Knock"));
  76.     }
  77.    
  78.     else
  79.     {
  80.       //turn LED off
  81.       digitalWrite(ledPin, LOW);
  82.     }
  83. }// end loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement