Particle photon measure sound level MAX4466

Hi Particle Community - thanks for all the help so far. I hope you can help me out with this one.
I have bought the Ardafruit MAX4466 with goal of measuring sound volume.

To get started I try to use the code snippet from here.

However, when I flash this to the photon the only value I can read from the cmd is 20971515.00

This is my setup:

What is wrong?

Try checking the accuracy you’re using. The Photon has 4096 levels it can measure, compared to the 1024 of the Arduino used. That ought to make a difference.

Hi Moors thanks for taking a look. I’m a new to electronics - how do I check the accuracy?

https://docs.particle.io/reference/firmware/photon/#analogread-adc-
The first paragraph over here should give you some ideas :wink:

Serial.println of analogRead(0) gives me readings around 3746-3747.

What can I conclude from that?

That the range of measurements is most certainly greater than 0-1024, and you should probably adjust the math in the code you’re using to reflect that :wink:

Edit:
Taking a second look, it seems that you’re using a 5V-out sensor, something that’s not tolerated on Analog pins. I’d disconnect and make sure you use the proper circuitry to prevent damage to the Photon, if it hasn’t occurred already.
https://docs.particle.io/datasheets/photon-datasheet/#peripherals-and-gpio

So when you said the Photon has 4096 levels it can measure, does it mean that the code should be changed to this:

 unsigned int signalMin = 4096;

// collect data for 50 mS
   while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(0);
      Serial.println(sample);
      if (sample < 4096)  // 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) / 4096;  // convert to volts

Is it that simple?

@Moors7 hmm - using that code and serial printing volts give just 0.01 no matter the sound?

have you seen my edit?

Did not see the edit. Is it a 5V-out sensor?

Here it says that VCC can be 2.4-5VDC, and the output 1.65V?

I just jumped over a few posts when I read this

  sample = analogRead(0);

That won’t work since 0 means D0 which is not an analogRead() pin.
You really should stick with the elaborate pin names analogRead(A0)

And not the output is 1.65V (Vcc/2) but the bias (mid point) when powered off 3.3V. The voltage will be that +/-1.65V (theoretic max amplitude when 3.3V)
If you power it off 5V, the bias will be Vcc/2 (=2.5V) with an amplitude of +/- 2.5V - possibly grilling your A-pin - so stick with Vcc on 3v3 (as you have it), and avoid using Vin.

1 Like

Ok I only use 3v3.

I changed to analogRead to

sample = analogRead(A0);

This gives me the following on the serial monitor:

With this code:

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 = 4096;
        
       // collect data for 50 mS
       while (millis() - startMillis < sampleWindow)
       {
          sample = analogRead(A0);
          if (sample < 4096)  // 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) / 4096;  // convert to volts
       Serial.println(volts);
    }

And this setup:

What could be wrong?

I’m guessing the Math. Could you printing the analog reads again to see if they make some more sense now? Make some noise, see if they change noticeably, preferably within the full range.
Once that works, you can start looking at the code/math further.

double volts = (peakToPeak * 5.0) / 4096;  // convert to volts

That stands out to me, since that 5V seems a bit random, or doesn’t it?

Thanks for helping!

I now do this:

sample = analogRead(A0);
Serial.println(sample);

But no matter how much sound I make - no difference…
Only the following values :joy:

This might be causing some issues:

I tried this - but with no success :frowning: It only shifts the value from 374something to 375something

Try adding some delayMicroseconds(5) in your sampling loop and do you know what the output resistance/impedance of the module is?
If it’s too high, you might not get good readings and you may need a voltage follower to reduce that.
A 10/100nF cap might do too.

Have you got an oscilloscope to check the output?

Hi @ScruffR

I actually think it works now - I changed the wires :smiley:

Thanks for the help!

I ended up with the following code:

    unsigned long startMillis= millis();  // Start of sample window
    unsigned int peakToPeak = 0;   // peak-to-peak level
    
    unsigned int signalMax = 0;
    unsigned int signalMin = 4095;
    
    // collect data for 50 mS
    while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(0);
      if (sample < 4095)  // 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) / 4095;  // convert to volts
   
   Serial.println(volts);
3 Likes