Thanks Mike, never heard of a peak detector. Is it as simple as hooking up a diode and capacitor between my A0 pin and the left or right channel?
Maybe... There's an approximate 0.7V drop across the diode, so low-level AC audio signals might not get through.
I use an [u]active peak detector circuit[/u], but that requires an op-amp and a dual (positive and negative) power supply.
A peak detector charges-up a capacitor and holds it for a period of time depending on the RC time constant. (There is no resistor in the circuit Mike linked to, and you really need a resistor across the capacitor.) I use a time-constant of about 1/10th of a second for audio activated lighting effects.
A peak detector removes the negative part of the AC audio waveform (a full-wave peak detector will invert negative voltages) and since it holds the voltage you can read the "volume" about 10 times per second instead of reading the audio waveform thousands of times per second.
(The Arduino can be damaged by negative voltages, so you do need to deal with that somehow.)
but it just spits out a 250 average consistently regardless what the song is playing.
Does silence also read about 250? ...That could be a good thing.
With the default 5V reference, 250 is about 1.25V. That means the audio output is biased at ~1.25V (and the headphone output uses a virtual ground at the same voltage).
A normal audio waveform is positive half the time and negative half the time and the average is zero. If your signal is biased so that silence reads 250, the average will also be 250, no mater how loud the audio is.
But, you should get readings above and below that average depending on the volume, and you can pick-out the peaks in software, or make your software do something when the peak readings are above a certain threshold.* You can optionally ignore the negative values (readings below the 250 bias), and/or you can subtract-out the bias and/or you can take the absolute value of the readings.
You do not need a peak detector, but you should know that with an audio signal half your readings are negative (or below the bias) and most of your readings will be somewhere between zero and the positive or negative peak. i.e. Since you'll be getting lots of low readings, even with a loud audio signal, you may want to do something like peak detection in software, or every time the LED comes on, hold it on for a fraction of a second, etc.
Bias on the output will also take care of the diode voltage-drop if you use a passive peak detector. You'll get slightly lower readings through the diode, but the readings will go up when audio is present.
If silence is reading zero, you can add your own [u]DC bias circuit[/u] to take care of the negative voltage issue. (With two equal-value resistors the bias will be half the supply voltage, so your signal should be biased at an ADC reading of about 512.)
- One lighting effect I have keeps a 20 second moving average of the peak-detected value to use as a threshold. The light flashes on when the signal is above average and goes off when it's below average.