Skip to content

Instantly share code, notes, and snippets.

@madeintaiwan
Created September 2, 2013 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madeintaiwan/6410697 to your computer and use it in GitHub Desktop.
Save madeintaiwan/6410697 to your computer and use it in GitHub Desktop.
Sample Adafruit code for Sound to Volts
/****************************************
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