Created
September 2, 2013 08:49
-
-
Save madeintaiwan/6410697 to your computer and use it in GitHub Desktop.
Sample Adafruit code for Sound to Volts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************** | |
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 * 3.3) / 1024; // convert to volts | |
Serial.println(volts); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://learn.adafruit.com/adafruit-microphone-amplifier-breakout/measuring-sound-levels
from there.