Difference between revisions of "Microphone Sound Detection Sensor Module for Arduino"

From air
Jump to navigation Jump to search
 
 
Line 10: Line 10:
 
Test of Noise Level Sensor SKU 135533
 
Test of Noise Level Sensor SKU 135533
 
http://dx.com/p/arduino-microphone-sound-detection-sensor-module-red-135533
 
http://dx.com/p/arduino-microphone-sound-detection-sensor-module-red-135533
*/
 
   
  +
Author : Remi Pincent
#define TIME 100
 
 
*/
   
  +
// Do not remove the include below
#define APIN A0
 
  +
#include "SoundSensorExampleCode.h"
#define DPIN 9
 
  +
#include <limits.h>
   
  +
/* SKU15333 Analog output -> connected to analog input 0 */
int counter = 0;
 
  +
const int soundSensorAnalogPin = A0;
boolean flag = false;
 
int sensorMaxValue = 0;
 
int sensorAvgValue = 0;
 
   
  +
/* SKU 135533 Digital output -> connected to D2 PIN */
void setup() {
 
  +
const int soundSensorDigitalInputPin = 2;
pinMode(DPIN, INPUT);
 
  +
/* 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);
 
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 */
void loop() {
 
  +
for(int loc_nbSamples = 0; loc_nbSamples < nbSamples; loc_nbSamples++)
+
{
int sensorValue= analogRead(APIN);
 
  +
loc_sample = analogRead(soundSensorAnalogPin);
sensorAvgValue+=sensorValue;
 
  +
if(loc_maxSample < loc_sample) loc_maxSample = loc_sample;
if(digitalRead(DPIN)==HIGH) flag=true;
 
  +
if(loc_minSample > loc_sample) loc_minSample = loc_sample;
if(sensorValue>sensorMaxValue) sensorMaxValue=sensorValue;
 
  +
loc_avg += loc_sample;
+
}
if(counter++>TIME) {
 
Serial.print(sensorAvgValue/TIME);
+
Serial.print("Sound level measures :\n Min sample = ");
Serial.print(" ");
+
Serial.println(loc_minSample);
Serial.print(sensorMaxValue);
+
Serial.print("Max sample = ");
Serial.print(" ");
+
Serial.println(loc_maxSample);
if(flag) Serial.print(flag);
+
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();
 
Serial.println();
  +
}
   
  +
static void soundDetected(void)
flag = false;
 
  +
{
counter = 0;
 
  +
nbRisingEdges++;
sensorMaxValue = 0;
 
sensorAvgValue = 0;
 
}
 
delay(1);
 
 
}
 
}
  +
 
</pre>
 
</pre>

Latest revision as of 21:51, 6 December 2013

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++;
}