[SOLVED] Trouble using MAX9814 with Photon

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?

Try Serial.write?

Try serial.print(val,DEC)

You need to know that the Particle ADC feature 12bit precision (max 4095) and not 10bit (max 1024).
So this is one thing you should alter in your code.

Also add a pinMode(A0, INPUT); in your setup().

And use Serial.print(val, 2) which will give you the float with two decimal places (DEC, HEX, BIN are for integer types ;-)).

Thanks for the help, that fixes the formatting of the value but my real issue is that I’m not getting an accurate reading from the microphone. I’m always getting a value of 443.61 regardless of the noise level.

Have you seen the first part of my post?

You are checking if (sample < 1024) but quite possiblly your reading will be higher than this.
And pinMode()?

1 Like

Ah! My apologies, I added the pinMode call and used Serial.print(val, 2) but didn’t update my code to use 4095. It works! Thanks so much.

1 Like