Microphone Sound Detection Sensor Module for Arduino

From air
Revision as of 21:51, 6 December 2013 by Donsez (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Microphone Sound Detection Sensor Module for Arduino 135533

Microphone Sound Detection Sensor Module for Arduino

http://dx.com/p/arduino-microphone-sound-detection-sensor-module-red-135533


/*
Test of Noise Level Sensor SKU 135533
http://dx.com/p/arduino-microphone-sound-detection-sensor-module-red-135533

Author : Remi Pincent
*/

// Do not remove the include below
#include "SoundSensorExampleCode.h"
#include <limits.h>

/* SKU15333 Analog output -> connected to analog input 0 */
const int soundSensorAnalogPin = A0;

/* SKU 135533 Digital output -> connected to D2 PIN */
const int soundSensorDigitalInputPin = 2;
/* SKU 135533 digital output -> connected to D2 PIN used as external interrupt */
const int soundSensorExtIntNumber = 0;

/* Sensor analog signal measures */
const int nbSamples = 128;

/* Number of rising edges during capture */
volatile unsigned int nbRisingEdges = 0;
/* Rising edge capture duration in ms*/
const int captureDuration = 1000;

/* Private functions */
static void soundDetected(void);

//The setup function is called once at startup of the sketch
void setup()
{
  Serial.begin(9600);
}

// The loop function is called in an endless loop
void loop()
{
	unsigned int loc_sample = 0;
	unsigned long loc_avg = 0;
	unsigned int loc_maxSample = 0;
	unsigned int loc_minSample = INT_MAX;

	/* Get many reading and average them */
	for(int loc_nbSamples = 0; loc_nbSamples < nbSamples; loc_nbSamples++)
  {
	  loc_sample = analogRead(soundSensorAnalogPin);
	  if(loc_maxSample < loc_sample) loc_maxSample = loc_sample;
	  if(loc_minSample > loc_sample) loc_minSample = loc_sample;
	  loc_avg += loc_sample;
  }
	Serial.print("Sound level measures :\n Min sample = ");
	Serial.println(loc_minSample);
	Serial.print("Max sample = ");
	Serial.println(loc_maxSample);
	Serial.print("Average = ");
	Serial.println(loc_avg / nbSamples);
	Serial.println();

	/* Now look at sensor digital output */
	nbRisingEdges = 0;
	attachInterrupt(soundSensorExtIntNumber, soundDetected, RISING);
	delay(captureDuration);
	detachInterrupt(soundSensorExtIntNumber);
	Serial.print("Number of rising edges detected in ");
    Serial.print(captureDuration);
    Serial.print(" ms = ");
    Serial.println(nbRisingEdges);
    Serial.println();
}

static void soundDetected(void)
{
	nbRisingEdges++;
}