Audio signal to 0-1023

hi,

I would like to feed an audio signal to an A/D input and create a value from 0 - 1023 from it.

However, it doesn't seem to work as I thought it would.

The audio signal is biased at 2.5 volts DC and feeds into an A/D input. I then read the analog input continuously.

input = analogRead(A0);

Is that all there is to it, or what else do i need to do?

thanks

Basic way of getting peak/peak value.
Leo..

int rawValue, minValue, maxValue, peakValue;

void setup() {
}

void loop() {
  minValue = 1023; // reset
  maxValue = 0; // reset
  for (int i = 0; i = 250; i++) { // x samples
    rawValue = analogRead(A0);
    if (maxValue < rawValue) maxValue = rawValue; // store max
    if (minValue > rawValue) minValue = rawValue; // store min
  }
  peakValue = maxValue - minValue; // calculate difference
  // code here to use peakValue
}

i see. thanks.

why i = 250; Could it instead just keep running inside main loop()?

Long enough to detect low frequencies and short enough to not block loop() too long.
Must stop the process at some stage to harvest and reset.
Leo..

Wawa:
Basic way of getting peak/peak value.
Leo..

int rawValue, minValue, maxValue, peakValue;

void setup() {
}

void loop() {
  minValue = 1023; // reset
  maxValue = 0; // reset
  for (int i = 0; i = 250; i++) { // x samples
    rawValue = analogRead(A0);
    if (maxValue < rawValue) maxValue = rawValue; // store max
    if (minValue > rawValue) minValue = rawValue; // store min
  }
  peakValue = maxValue - minValue; // calculate difference
  // code here to use peakValue
}

I am not sure that code makes sense. seems that peak value will always be zero.?

If you have properly biased your audio input mid-voltage, then the A/D will return 512 without sound.

'maxValue' will be 0 at the start of loop, and with the first A/D read it will be 512.
A positive peak in the sound (if there) will increase that value, say to 700.

'minValue starts at 1023, and is lowered to 512 with the first A/D read.
Any negative peaks will decrease that value, to say 300.

'peakValue' then calculates to 700-300=400.

It all starts over again with the next loop.
Leo..

ok thanks

for (int i = 0; i = 250; i++)

should that be i < 250

tim77777:
should that be i < 250

Duhh, yes. Good catch.
Need more coffee.

Do you actually want a peak value? If so then the simplest way would be to use an envelope follower before the analogue input.
Use the one with the resistor across the capacitor in his link

Not sure what OP need it for, but a simple diode/cap has a threshold (the diode Vf).
Ok for max volume detection, but low volume signals are simply cut off.
An active top detector (diode/opamp) could be needed.
You can find good examples in the LM3915 datasheet.
Leo..