Is there a way to detect the volume of music from the line (not a microphone)?
Simply putting the wires on GND and an analog pin wouldn't work.
I don't need an exact measurement in standard units, just one that increases as the volume increases.
I would also like to have my Arduino not be fried when the volume is too high (use a transistor?)
I know I'm a newbie and probably am missing something obvious.
I really appreciate any help you can provide. @anon44338819
you can measure the amplitude the same way you measure the amplitude with ADC and analogRead. You will need a capacitor and another capacitor a resistor and a diode.
Really, all the old VU meters were wired into the preamp/driver section which typically is like 1V-PP if I recall. You can easily change the Arduino AD to use the lower bandgap reference and then simply use an RC network with a diode to get a DC varying voltage.
The Arduino can handle zero to +5V. It can be damaged by negative voltages or voltages over 5V so the main thing is to protect it from the negative half of the AC audio signal. With higher-power speaker signals you have to also protect it from excess positive voltages.
I normally use an op-amp peak detector. I use positive & negative power supplies so it can go all the way down to zero and I normally use + & - 12V so I can go all the way to 5V, and then I add an over-voltage protection circuit. (A resistor and diode.)
A peak detector puts-out a varying DC voltage and it holds the peaks and then decays depending on the RC time constant so it's straightforward to read it and depending on what you're doing you can read it 10 times per second (or so) instead of reading the waveform thousands of times per second. That frees-up the processor to do other things.
But there are simpler alternatives. The "standard solution" is a bias circuit that adds 2.5V to the signal (on the Arduino side) so silence reads about 512 on the ADC, and the audio creates readings above and below that. If you wish, you can subtract-out the bias in software to get the negative & positive values.
Or you can use a negative voltage protection circuit:
Note that with either of these the raw readings will "look random" because you are sampling an AC waveform. So, you'll have to find the peaks or the average. With the bias circuit the average is always about 2.5V so you'll have to find the average of the absolute values. Or you can "get fancy" and calculate RMS. With the protection circuit half of the readings (all of the negative readings) will be zero.
The protection circuit has a potential advantage that you can use the optional 1.1V ADC reference to increase sensitivity. That can be helpful, especially if your line-level signal is volume-controlled. You can do the same thing with a peak detector. But with the bias circuit at 2.5V you obviously can't use the 1.1V reference.
...I make sound activated lighting effects and my code switches automatically between the two ADC references depending on the signal level.
The the voltage level will do that. But that is not how "volume" or loudness is measured. That is measured in deciBells or DB. 3DB is twice as loud as before.
No software except to read the analogue pin the output is connected to.
Note your audio voltage needs to be over 0.7V before it will register anything.
Thanks! I will look into this (and test it)
How do I find the values of the capacitor and the resistor?
And how do I prevent blowing my pin with too much voltage?
Experiment, the capacitor will control how quickly the numbers respond to a peak, the smaller the sooner. The resistor controls how long the decay is after the peak is reached, the smaller the sooner.
Start off with 10 uF and 10K and go from there.
You would need a 10 volt peak to peak audio signal to exceed the capacity of the analogue input voltage ( assuming a 5V operating Arduino ) you will be hard pressed to get one of those that big.