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
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.
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
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
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.
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);
}
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.
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.
I actually think it works now - I changed the wires
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);