I used the circuits below (they're the same) to make an LED blink to music. Cool, but I'd like to be able to read the "level" of audio coming out of my PC by attaching it to the arduino instead of an LED. However, I can't get it to read the input using basic analog in code. Please help!
int value;
void setup()
{
Serial.begin(9600);
}
void loop()
{
value = analogRead(0);
Serial.println(value);
delay(10);
}
Audio is an AC waveform centered around zero, so it won't work to just feed it directly to a pin. You need to move the signal up so that the lowest value you read will be 0V. An easy way to do this is to feed the audio signal into a capacitor and bias the Arduino side up to 2.5V using a voltage divider. You may also want to amplify the signal to get full scale use of the ADC using an op amp.
I at first got values from about 2 to 35 or so when I replaced the LED with a wire going to analog in.
Thanks for the info about audio wave forms - So which type/size of capacitance do I need and how should I hook it up? Just between the audio signal and the input pin?
Also, I'm not quite understanding what you mean by:
"bias the Arduino side up to 2.5V using a voltage divider"
I thought about using an op amp but I was trying to "cheat" by doing it this way with just the tip31. Thanks for your help and answering my questions.
Thanks for the link. According to the diagram provided, it looks like all I need to do is attach a 10uf cap in between the audio signal and analog in, correct? And another cap going to ground?
There is also the 100K pull up connected to the 10K pot followed by the 100K pull down. This allows you to adjust the DC level to stop cliping in one direction.
Sorry, it seems I wasn't getting email response notifications. Yes, that circuit Grumpy_Mike linked to is what I was referring to ("Two resistors and a trimmpot are adding an DC offset to the audiosignal.") That pulls the Arduino side of the capacitor up towards the middle of the voltage range by adding a DC offset. The capacitor value is not too important; different values will affect the bass response differently (higher capacitance = less bass response), but the Arduino is not an audiophile device, so you're not going to have to worry about that. Anything from .1uF to 10uF should be fine. You don't technically need the cap to ground, that is there to filter out any high frequency noise on the input.
Those circuits are intended to get actual audio into the Arduino for sound effect processing. Sure, you could then do some math to software-rectify and integrate the received audio, but it won't leave much time for other things.
The following circuit requires a few more parts, but does a very good job of simply getting the current value of audio power. It is already rectifying too, so you regain half your ADC resolution, unlike the solutions requiring you to bias the waveform to half the reference voltage.
I have used this circuit a number of times for VU-style applications. Here's an example:
I was trying to use a similar configuration a while ago to do high resolution timing of peaks, but it didn't work out for me because it ignores negative going impulses.
Note that it doesn't rectify the incoming audio signal. The datasheet for the LM224 specifies a maximum negative input level of -.3V, and it looks from your graph that the loudest peaks exceed -1V, so you may want to be careful about that. A diode after C2 would rectify it so you don't have to worry about frying your op amp.
Thanks for the circuit macegr. What are V1 and V2 in the schematic? Also, shouldn't reading an analog signal be far more simple than this? Just seems like a lot of trouble...
I did deal with that later on, though I never updated the simulation screenshot. Thanks for reminding me...I had a reverse biased diode and resistor before the capacitor, and then a 4:1 voltage divider to ensure the maximum negative voltage set by the diode was always less than the op-amp maximum negative input. Then upped the gain to compensate. Works fine...but then again, the circuits I built without the protection circuit haven't failed yet. I probably just haven't given them enough of an excuse.
I am sure macegr can speak for him self but in the interest of speeding things up:-
shouldn't reading an analog signal be far more simple than this
Actually no, the apparent ease of modern digital circuits lull people into a false sense of security about how simple things should be. This is analogue electronics, that operational amplifier contains probably the equivalent of 60 transistors.
What does D1 do in your circuit
They are a peak detector circuit. The green waveform is the audio and the blue is the output of the circuit.
Note that it doesn't rectify the incoming audio signal.
The input is AC coupled to the op amp. This means that the input will see a DC voltage of what ever level it is biased to with the audio a variation on that.
However I must say that the -ve on the LM224 is shown as going to earth and I would have expected this to go to the -ve supply rail. Either that or take it to ground and create a mid rail point with two resistors.
I know it's a peak detector circuit. I know what a peak detector circuit is and how they're generally constructed. I'm wondering what is the function of the diode D1 is, since I haven't seen one used in that place before.
Also, I was commenting on macegr's circuit, not your schematic above. You'll note that it does not have any post-capacitor level biasing, and the op-amp is being run single-ended. That's not uncommon if you're just interested in the audio signal amplitude, but some care has to be taken not to fry the op amp with negative voltage.
macegr, I'm not sure I understood your explanation of where you put the diode and what direction it was pointing in. Can you elaborate? I'm genuinely curious, as I've been playing around with peak detection lately.
The red trace is the output signal, the green trace is the input signal, and the blue trace is the signal at the op-amp non-inverting input.
I'm using the diode to clamp the negative voltage to a manageable level and then dividing the signal to ensure it never reaches -300mV on the op-amp input. It wouldn't be a great idea if someone were concerned about noise, but actual audio doesn't pass through this circuit anyway.
BAT54 is a Schottky diode, you could google the part number. V1 is the supply for the op-amp. V2 is an audio file read from the computer and used as a changing voltage source. That's where you would connect the line in. You might want a 10K resistor to ground, since a lot of audio outputs seem to expect it.