Reading data from headphone jack

Hello,

I'am trying to use a headphone jack to read the values and control some RGB lights with it. And it kinda works.
My current setup is:

  • A cable from the left channel of the headphone jack to A0 on the arduino.
  • A cable from the ground of the headphone jack to the ground on the arduino.
  • And an rgb strip which we just skip right now to make it easier.

If I print the A0 i get values between 0 and 300.
The problem is that i will get values of 0 around every 2 seconds for about 2 seconds. And than it will continue with the normal values. While trying to find a solution i found out that when I put a multi meter against both wires the signal changes between positive and negative. And when the signal is negative the values on the arduino are 0. Now I have read that arduino's can read negative voltage.

So my first question is how can I convert the negative voltage to positive voltage?
The second question I got is how can I boost the signal because now I have to put every thing on 100% with an amplified speaker, but this is not the output jack I want to use. But if I use a different output jack the signal is not strong enough.
The third question is what does an headphone jack output? Is it frequency or is it the amplitude?

Negative voltages can damage an Arduino. A Google search for "arduino measure negative voltage" will show how to bias the voltage so that it wil not go negative.

TheLazyTurtle:
Hello,

I'am trying to use a headphone jack to read the values and control some RGB lights with it. And it kinda works.
My current setup is:

  • A cable from the left channel of the headphone jack to A0 on the arduino.
  • A cable from the ground of the headphone jack to the ground on the arduino.
  • And an rgb strip which we just skip right now to make it easier.

If I print the A0 i get values between 0 and 300.
The problem is that i will get values of 0 around every 2 seconds for about 2 seconds. And than it will continue with the normal values. While trying to find a solution i found out that when I put a multi meter against both wires the signal changes between positive and negative. And when the signal is negative the values on the arduino are 0. Now I have read that arduino's can read negative voltage.

So my first question is how can I convert the negative voltage to positive voltage?
The second question I got is how can I boost the signal because now I have to put every thing on 100% with an amplified speaker, but this is not the output jack I want to use. But if I use a different output jack the signal is not strong enough.
The third question is what does an headphone jack output? Is it frequency or is it the amplitude?

Congratulations! You discovered that audio signals are AC.

To convert to DC, build a bridge rectifier using germanium diodes. Google will help. That will convert the AC to pulsing DC by inverting the negative voltage to positive and inserting it between the original positive impulses.

Now you can measure the sum of positive voltages. Watch for the values that will exceed the limits the ARduino pin can handle.

Paul

Just to get you started, take a look at my [u]Wold's Simplest Lighting Effect[/u]. It has automatic sensitivity adjustment and there is an attached schematic for the standard bias circuit. (You can subtract-out the bias in software to get the negative readings.)

Do you need frequency information? If not, you can throw-away the negative half of the waveform. The bottom-half is nearly identical to the top-half so you don't need the negative half for amplitude. But, half-wave or full-wave rectification is severe distortion so the frequency information will be damaged.

I don't recommend passive-diode rectification because there is a forward-voltage drop and weak signals won't get through at all.

If you just want amplitude information you can use a [u]peak detector[/u] (AKA envelope follower) with an op-amp. You can use an RC time constant of about 1/10th of a second and you can read the "loudness" about 10 times per second instead of reading an audio waveform thousands of times per second. (This how I make my real lighting effects... I just use the simple bias circuit for that simple example.)

You can optionally add gain to the peak detector, or you add another op-amp gain stage.

Since the peak detector output is not biased, you can use the optional 1.1V ADC reference to increase sensitivity. In my effects, I switch automatically between the 5V and 1.1V bias depending on the signal level (in addition to a variable software reference).

If you want to go all the way down to zero volts, your op-amp circuit should be powered by positive & negative voltages. I use + & - 12V and I add an [u]over-voltage protection circuit[/u] between the peak detector and Arduino.

The problem is that i will get values of 0 around every 2 seconds for about 2 seconds.

I don't know why you're getting 2 seconds of zeros... Maybe those negative voltages are temporarily killing your Arduino? But, half of your values are negative and these will read usually zero (if the Arduino survives). The positive readings will "look random" within some range because you are sampling a waveform. You'll generally want to find the peaks or take an (moving) average of the positive values. (Since it's negative half the time, the true-average is zero.).

So if I just want to get the loudness (the amplitude) all I have to get is an peak detector and i can program my lightning effects based on that info? And can I buy a peak detector or do I have to build one because when I google "peak detector" I get some circuit diagrams that i don't understand.
And does an peak detector also work with negative voltage or do i still have to build the thing that prevents/converts the negative voltage to positive?

So if I just want to get the loudness (the amplitude) all I have to get is an peak detector and i can program my lightning effects based on that info?

Yes. The peaks are one indication of "loudness". For example, with digital recording peak meters are the most useful and probably the most common type of "VU meter".*

And can I buy a peak detector or do I have to build one because when I google "peak detector" I get some circuit diagrams that i don't understand.

You'd have to build one.

If you look at [u]this circuit[/u] R2 is zero ohms, so you can leave out the resistor and replace it with a wire. And, without R2, you can leave-out R1. So, that leaves one op-amp, one diode, one resistor, and one capacitor. The "hardest part" is that you should have positive and negative power supplies. (And if the power supply is above +5V you should add the resistor & diode protection circuit, or at least add a series resistor to take advantage of the protection diodes built-into the chip.)

Or, forget about the peak detector and make the bias circuit (2 equal-value resistors and a capacitor) and find the peaks in software like in my "Worlds Simplest..." example. This is the most common way of handling audio input.

With the bias method you are sampling the waveform so it's also possible to take a moving average of the positive or absolute values and that's actually a better indication of "loudness" than the peaks.

And does an peak detector also work with negative voltage or do i still have to build the thing that prevents/converts the negative voltage to positive?

A standard peak detector ignores the negative voltages so it's safe for the Arduino.

With a couple more op-amps you can make a precision rectifier to "flip" the negative voltages positive but that's really not necessary.

With the bias circuit it's also easy to take the absolute values (after subtracting the bias), which I do in my "Worlds Simplest..." example.

  • A true VU meter shows a short-term average and they are "outdated" in the digital age..

So if i understand correctly the bias circuit form your example is easier and better to use.
And i took a look at the circuit diagram you left in the post but i don't fully understand it. I understand the audio in and the pin that go to the arduino, but where do both of the 10k resistors go? And does the +5v mean you add 5 volt or is that the positive side of the circuit?

So if i understand correctly the bias circuit form your example is easier and better to use.

It's a trade-off. The bias circuit is easier to build.

Like I said, the peak detector makes your software a little easer, plus you can use the 1.1V ADC reference for more sensitivity. (The bias is 2.5V so with a 1.1V reference you'd max-out the ADC and you'd always read 1023.)

And i took a look at the circuit diagram you left in the post but i don't fully understand it. I understand the audio in and the pin that go to the arduino, but where do both of the 10k resistors go? And does the +5v mean you add 5 volt or is that the positive side of the circuit?

You get 5V from the Arduino's 5V pin and the other resistor connects to the Arduino's ground.

The two resistors form a [u]voltage divider[/u] and equal resistors divide the voltage in half. That's 2.5V which reads ~512 on the 0-1023 ADC range.

If you were getting peaks of 300 before, you should get readings between 812 and 213 with the bias added. And of course, if you subtract-out the bias you'd get readings between +300 and -300.

The capacitor "blocks" the DC bias from getting into the connected audio circuit while allowing the AC audio signal through, and it prevents any resistance in the (unknown/undefined) audio circuit from messing-up your voltage divider calculations. (Sometimes there is already a capacitor on the amplifier output but another one doesn't hurt and it's better not to take a chance.)

DVDdoug:
Like I said, the peak detector makes your software a little easer, plus you can use the 1.1V ADC reference for more sensitivity. (The bias is 2.5V so with a 1.1V reference you'd max-out the ADC and you'd always read 1023.)

Does this mean that it will only detect a peak and will not say how high the peak is, for example if i had a peak of 1 (i don't know what) it would show 1023 and if i had a peak of 2 (i don't know what) it would also show 1023? Because if that is the case it is not the best solution because i want to change the color of the rgb lights based on how high the amplitude is.

edit/update
I just tried the circuit from world easiest... and I am still getting values like i used to get (which is great) but now it is from 20 - 200/300 and peaks of 450. The 0 values are now way less frequent (mostly only when the music is not very loud). I did not use the capacitor that you showed in the circuit because i dont have any at this moment. Does this matter?