Hi,

Im using a microphone sensor and want the readings to range from 0 - 1023

But it only goes up to 672

What can i change in the code to get 1023?

``````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 = 1024;

// collect data for 50 mS
while (millis() - startMillis < sampleWindow)
{
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 * 3.3) / 1024;  // convert to volts

Serial.println(peakToPeak);
}
``````

Reference page to the rescue:
https://www.arduino.cc/en/Reference/Map
--Michael

mrears92:
Hi,

Im using a microphone sensor and want the readings to range from 0 - 1023

But it only goes up to 672

What can i change in the code to get 1023?

What is special about 1024? Once you have your readings, the scale is unimportant. But to answer your question, you can simply scale it with a different constant:

``````double sig = (peakToPeak * 3.3) / 672;  // convert to calibrated level
``````

mrears92:
But it only goes up to 672

The question is, why? Do you mean that is the maximum level, which the mike module hits long before the maximum volume has been reached? If that is the case, converting to 1023 or whatever won't improve things. There should be some kind of intelligent scaling and saturation detection happening after you convert to volts. Otherwise you are just engaging in a pretense of accuracy that you don't really have. You should also be thinking ahead to a calibration method, because there isn't any predictable gain in any part of your audio, electronic or software path.

I don’t think that a 10-bit ADC is likely to need this either:-

``````sample = analogRead(0);
if (sample < 1024)  // toss out spurious readings
``````