Help with MEMS Microphone configuration

Hello!
I've recently acquired a MEMS mic sensor. I found this simple but nice program which made me understand the way the microphone works:

const int pinMic = A5; //analog input connected to mic AUD

const int pinLED0 = 9; //digital output connected to LED 0
const int pinLED1 = 10; //digital output connected to LED 1
const int pinLED2 = 11; //digital output connected to LED 2

//baseline level for my mic in a fairly quiet room
//determined by watching serial monitor on first run
const int valBaseline = 350;

void setup() {
    Serial.begin(115200);

    pinMode(pinLED0, OUTPUT);
    pinMode(pinLED1, OUTPUT);
    pinMode(pinLED2, OUTPUT);
}

void loop() {
    int valMic = analogRead(pinMic);

    Serial.println(valMic);

    if (valMic > valBaseline + 10) digitalWrite(pinLED0, HIGH);
    else digitalWrite(pinLED0, LOW);

    if (valMic > valBaseline + 15) digitalWrite(pinLED1, HIGH);
    else digitalWrite(pinLED1, LOW);

    if (valMic > valBaseline + 25) digitalWrite(pinLED2, HIGH);
    else digitalWrite(pinLED2, LOW);
}

I then thought I could make it work smoother if the baseline is taken from the first readings of the microphone, so that I don't have to worry about the certain ambiental noise of every place and change it in the program.
What I did was adding this sentence on the setup function: valBaseline = inicializarBaseline();
Which calls:

int inicializarBaseline(){
  
      int baseline[5];
      for (int i=0;i<4;i++){
      
          baseline[i]=analogRead(pinMic);
          delay(100);
        
      }
      int result = lecturaMedia(baseline[0],baseline[1],baseline[2],baseline[3],baseline[4]);
      return result;
         
      
  
  }

Taking five readings with 100 milliseconds between each one, and making the mean of them (that's the "lecturaMedia").

My problem comes with this baseline obtained on the setup. It seems that while the setup is running there's electrical noise on the analog pin, so that I get a much higher baseline than the real average reading of the microphone when the room is silent.

How can I solve this and get an accurate baseline?

Sorry if I made this a bit confusing for reading :cold_sweat:

My goal is similar, but my code is quite different. The noise in my hardware configuration comes from the USB power coming to the Uno. When I use batteries the noise is gone. I can literally detect a pin drop. I have noticed that it is much more sensitive to low frequency noise. A heater rumble in the basement or truck driving by miles away completely overpowers anyone talking in the next room. I know how to write a high pass filter, but I think it's pretty cool the way it is!

sbright33:
My goal is similar, but my code is quite different. The noise in my hardware configuration comes from the USB power coming to the Uno. When I use batteries the noise is gone. I can literally detect a pin drop. I have noticed that it is much more sensitive to low frequency noise. A heater rumble in the basement or truck driving by miles away completely overpowers anyone talking in the next room. I know how to write a high pass filter, but I think it's pretty cool the way it is!

Interesting. I remember I tried the same program with batteries, and noticed that it wasn't working correctly, so I suppose that in my case the noise comes from somewhere else.
Still, I don't have a solution for this noise... The program is fine by itself, but I feel it could be a bit more accurate.
And by the way, would you share your program? I find it interesting to see how other people program.
Thank you.

Your baseline function doesn't do what you say it does. It only samples four times. baseline[4] of the array will be garbage when it is passed to the lecturaMedia function.
Use this:

     for (int i=0;i<5;i++) {

Pete

I still think it's your PS or a cable near your microphone. Here's the loop of my code.

void loop(){
if(jj++%30==0){  //was overflow or first time 
for(li=0,i=0;i<2000;i++){
 li+=analogRead(3);
 delayMicroseconds(14);
 } 
//Serial.println("Cal");
avg=li/2000;
//Serial.println(avg);
}  //first time
nhit=nsil=mxp=np=0;
for(mx=0,li=0,lj=0,k=0;k<10000;k++){
i=analogRead(3);
delayMicroseconds(50);
j=analogRead(3);
delayMicroseconds(50-5);
//delay(2); Narcoleptic doesn't delay
ii=abs(((i+j)>>1)-avg);
li+=ii;

if(k==0)was=ii;
lj+=abs(was-ii);
was=ii;
if(k%100==0){
  if(mxp>=avg+30)np++;
  mxp=0;
  }
if(i>mx)mx=i;
if(j>mx)mx=j;
if(j>mxp)mxp=j;
if(ii>=30)nhit++;
if(ii<=5)nsil++;
//Serial.print(i);WR32
//Serial.print(j);WR32
}
//Narcoleptic.delay(4000);
li-=12000;
if(li<0)li=0;
mx-=avg;
mx-=4;
nmid=10000-nsil-nhit;
if(nmid>=100){  //100 or 1000 USB
//  if(!nrow)printDate();
printDate(); 
  fprn(++nrow,3);WR32
  fprn(li/1000,4);WR32  //total energy
  fprn(lj/1000,4);WR32  //high freq only near limit or >1/2, does not change with truck or heater or claps or snaps or clicks
  fprn(mx,3);WR32       //peak value
  fprn(np,2);WR32       //# periods out of 100/sec
  fprn(nhit,4);WR32     //# >=30
  fprn(nmid,4);WR32     //# <30,>5
  WR13
  } else nrow=0;
}

I am a bit lost with your code. Too many things and I don't know what does each one stand for...
Anyway, what do you mean by PS?
And how could a cable affect the analog pin?

PS= Power Supply. A cable with a magnetic field near the analog wire could cause noise because it's a low voltage when there is no signal.

I understand that, but what makes no sense is that this noise only appears on startup...