Read 3.5mm audio jack volume values with Arduino UNO.

Alright, I got it to work. I only receive values 0-255 it seems. Definitely doesn’t work well enough. When there’s base but the volume is low (in the song) it sits well below 150s. I will now try the first schematic.

If it doesn’t work you are doing something wrong. Post you code and a photograph of the wiring for help in seeing why.

Imgur: The magic of the Internet Here are 3 pictures for convenience, I hope you can "read" them. The schematic is the second one you posted, the envelope follower (or so I am convinced). With max volume on the PC I get as high as 230s. At 50% volume I get 4-15 as values.
There is no code, I'm just reading. Maybe that's where I'm mistaken ?
Thanks for your help, I really appreciate your help.

int analogPin = A5; 
int val = 0;  

void setup() {
  Serial.begin(9600);        
}

void loop() {
  val = analogRead(analogPin); 
 // digitalWrite(8, LOW);
 
// if (val>175){
 // digitalWrite(8, HIGH);
 //}
 
  Serial.println(val);         
}

When there's base but the volume is low (in the song) it sits well below 150s.

Bass tends to dominate the volume/power level but if you filter-out everything except the bass you are throwing-away most of the signal and you'll usually get lower readings.

So far, I don't think you're doing any bass filtering. (The envelope follower isn't a frequency filter.)

Readings of 150 are not THAT bad. And of course the volume/voltage will vary A LOT depending on the loudness of the recording, the particular phone you're using, and volume control setting. It would be unusual to get a 5V peak-to-peak signal (for the full 0-1023 ADC range) from a headphone or line-level output. (You could easily get that from a "moderate power" audio power amplifier but then you could end-up with too much voltage.)

My "World's Simplest Effect" adjusts automatically to volume changes and my real lighting effects automatically switch to the optional 1.1V ADC reference with lower signals. (But, you can't use the 1.1V reference with the 2.5V bias circuit.)

You will get better/higher readings if you get rid of the diode. If you get rid of the envelope follower you'll be reading (sampling) a waveform and the raw readings will "look random".* So, you'll need to find the peaks, or take a moving-average of the positive values or absolute values** depending on what works for you.

  • If you don't understand why the samples "look random", the you are sampling a constantly-changing waveform that crosses-through zero twice per cycle. The Audacity website has a [u]little tutorial[/u] about how digital audio works.

** The AC audio signal is positive half the time and negative half the time and the true-average is always zero. With the added 2.5V bias, the average is always 2.5V (about 512).

With max volume on the PC I get as high as 230s. At 50% volume I get 4-15 as values.

Like I said, the diode is non-linear... The voltage drop (loss) across the diode is about 0.6V and when your input voltage falls below that you can't read anything.

Are you sure that you have a 0.22uF capacitor there? It looks to me like an electrolytic capacitor, maybe 22uF?

Oh man! It's 22 microfarads, not .22 microfarads! At the store I said 22 microfarads, not .22 microfarads :S Does that mean I'm filtering out the low frequencies and losing the ability to detect the bass ??
Guys really thanks for the information, I'm trying to process it all.

It looks to me like an electrolytic capacitor, maybe 22uF?

:smiley: :smiley: That actually WOULD create a low-pass filter with the cutoff frequency depending on the output impedance of the phone. But, NOT a good thing to do because you are “shorting-out” the output at mid & high frequencies.

DVDdoug:
Like I said, the diode is non-linear... The voltage drop (loss) across the diode is about 0.6V and when your input voltage falls below that you can't read anything.

Then why do we need the diode (yes, without the diode it's much more "precise" and works way better than with it) ? I thought it had to prevent the negative voltage damaging the pin ?

I thought it had to prevent the negative voltage damaging the pin ?

That is correct! Although, the diode serves two purposes as it is also part of the envelope follower (it allows the capacitor to charge-quickly through the diode, but discharge slowly through only the resistor).

The bias circuit is another way of doing it. The AC audio signal is literally added to (summed with) the bias. For example, let's say you have a 2V peak-to-peak audio signal (that goes from -1V to +1V). With the +2.5V bias, it now goes between +1.5V (ADC = 307) and +3.5V (ADC = 716).

Typically, you'd want to subtract-out the 2.5V (512) bias in software. So that 2V peak-to-peak signal would read between (about) -205 and +205, indicating the actual positive and negative audio values and your bias-corrected range is -512 to +511.

Of course, with a high-enough signal (like a speaker output from your home stereo amplifier) you can still go negative or over 5V, but that's not going to happen with the headphone output from a phone or computer. ...If you get readings of zero or 1023 with the bias circuit in place, you are going negative and over +5V. (It would be very-unlikely that you'd hit exactly zero-volts or exactly 5V, so you can assume you are going negative and going over.)

...You probably won't kill the Arduino with a direct line-level or headphone signal, but you'd be "violating the specs" if you go more than 0.5V negative so you'd be taking a risk and you can't complain if you do kill it.

And, you also risk "damaging" (distorting) the audio because there are small (low current) protection diodes that prevent the input signal from going more than 0.5V negative. With enough current you can potentially fry the protection diodes and the rest of the Arduino. So my advice is, don't feed-in a negative voltage.

I am wondering if your expectations are a bit high, both from what you can expect from a simple circuit and what you can expect from a bass visualisation?

First off understand that to get a full value signal from the analogue to digital converter you need a signal that is 5V ( full scale ) + 0.7V ( voltage to overcome the diode’s forward voltage = 5.7V. And that is just the positive part of the waveform so in terms of peak to peak voltage you need 11.4Vptp.

Now a standard personal music player’s line signal is likely to be about 2Vptp so to get a full signal you need about a times 10 amplifier, a bit more if you don’t want to run it at full blast. Also it is not always the base frequencies in any tune that defines the beat.

Have a look at this video, it uses a chip that splits the audio spectrum into 7 bands and displays them as bars. In fact there are two chips one for each stereo channel. The circuit was for the Raspberry Pi but there is no reason why you could not use an Arduino and strip LEDs.

Later on I used this setup to let music control the movement of dancing skeletons. The two base bands you saw in the previous video define the vertical and horizontal movement of the whole skeleton, where as the higher frequencies define the angle of rotation of the limbs.

My wife did say “those limb movements are a bit unrealistic”, to which I replied, “we are talking about dancing skeletons here!”.

Pay attention to the base frequencies and how that does not always define the beat.

Also with your code try displaying the values in the serial plot window rather than the serial monitor window, you might make more sense of the values you are getting than just looking at the numbers.