Hello all,
I’m trying to use a MAX9814 microphone with the Photon to record ambient noise levels (in dB), however I’m having trouble getting an accurate reading from it. I have my microphone wired up as follows:
- Out -> A0 pin
- Gain -> 3.3v rail
- Vdd -> 3.3v rail
- GND -> Ground
Here is my code:
#include "math.h"
void setup() {
Serial.begin(9600);
Serial.println("setup");
}
void loop() {
Serial.println(noiseValue());
delay(1000);
}
float noiseValue() {
int sampleWindow = 50; // Sample window width. 50ms = 20Hz
int signalMax = 0;
int signalMin = 1024;
unsigned long startMillis = millis();
unsigned int sample;
unsigned int peakToPeak;
while (millis() - startMillis < sampleWindow) {
sample = analogRead(A0);
if (sample < 1024) {
if (sample > signalMax) {
signalMax = sample;
} else if (sample < signalMin) {
signalMin = sample;
}
}
}
peakToPeak = signalMax - signalMin;
return 20 * log(peakToPeak);
}
With that I just get a constant value of 443.61
. If I don’t use noiseValue()
and just do a straight Serial.println(analogRead(A0))
I get a constant value of 13841199.90
. I triple checked my wiring and can confirm that the microphone works on other microcontrollers. Is there anything I’m missing?