Go Down

Topic: [solved] Problem with reading out Adafruit max9814  (Read 1 time) previous topic - next topic

seebaer

Hey, I bought a adafruit max9814 mic, but I can't readout the data I get from it. I wired the mic like it is shown in picture below, but when I upload my code I always get the same data of around 500 in the serial monitor. Can you guys help me?


wiring plan:

https://cdn.instructables.com/FA6/2ZDM/IZ6D4PX9/FA62ZDMIZ6D4PX9.LARGE.jpg



my code:

[const int soundPin = A0;

void setup() {

Serial.begin (9600);
pinMode (soundPin, INPUT);

}


void loop() {

int din = analogRead (soundPin);
Serial.println(din);
delay (200);
}]



Wawa

The sketch does exactly what you have told it to do, take a single snapshot five times a second.
To measure the forever changing voltage of sound your must measure continuously (thousands of times a second), and store/update min and max values.
Adafruit has a link to example code on the main page of this sensor.
Leo..

seebaer

Thank you! I got it working now, but still there is the problem, that the differences between the values I get from the mic are all about 0.03V. I added a LED that should glow when there is a sound, but I really have to scream into the mic to see the value changing.
I just modified your code a little bit:


[/****************************************
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);
   pinMode (7, OUTPUT);
}


void loop()
{
  digitalWrite (3, 0);
   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 * 5.0) / 1024;  // convert to volts

  if (volts >= 0.03)
   {
      digitalWrite (7,1);

   }

  else
  {
    digitalWrite (7,0);
  }

 
   Serial.println(volts);

 
  }




 ]

Wawa

digitalWrite (3, 0); // module power pin ? gain pin ?  No pinMode ?

You should have posted according to the forum guidelines.
Code with code tags, and a picture of the setup.

Sensitivity of the Arduino (assuming Uno) can be increased by setting Aref to EXTERNAL, and connecting the Aref pin to the 3.3volt pin. Do BOTH, otherwise you could fry the Aref circuitry of the MCU.
Code: [Select]
void setup() {
  Serial.begin(9600);
  analogReference(EXTERNAL); // connect Aref to 3.3volt
  pinMode (3, OUTPUT);
  pinMode (7, OUTPUT);
}
Leo..


seebaer

I forgot to delete the "digitalWrite (3,0)". Here is the wiring plan and the code, but if I enter " analogReference(EXTERNAL); // connect Aref to 3.3volt" i just get a lot of zeros in the serial monitor... Thanks for your help!

Code: [Select]
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;


void setup()
{
 
   Serial.begin(9600);
   analogReference(EXTERNAL);
   pinMode (7, OUTPUT);
   digitalWrite (7,0);

}


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 * 5.0) / 1024;  // convert to volts
 
    if (volts > 0.03)
     {
     digitalWrite (7,1);
     }
 
    else
    {
      digitalWrite (7,0);
    }
 
     
     Serial.println(volts);

 
  }

Wawa

Sketch seems ok.
Your "wiring plan" shows the output connected to A5, but your code uses A0.
Ethernet shield? Which Arduino are you actually using, and does it have that shield?.
Fritzing toddler art never seems to represent what's actually used, also the LED is connected in reverse.
Post a 'REAL' picture of the setup.
Leo..

seebaer

It was just tge output connected to the wrong pin xD.

Now it works fine. Thank you!

Go Up