Go Down

Topic: Read 3.5mm audio jack volume values with Arduino UNO. (Read 1 time) previous topic - next topic

zwieblum

Well, you have a lowass filter that lets pass all frequencies lower then 7 Hz. Higher frequencies are smoothed out and you see only the envelop. Please read some texts on lowpass and highpass filters, that will answer a lot of questions :)

Stoil

7Hz! That's what the formula indicates but how is that applicable ? I mean, isn't the signal something like 20-20kHz ? What good does 7Hz do me ?  :o Now I got very confused :D

zwieblum

Yes, but it's the envelope you get for frequencies > 7 Hz

Grumpy_Mike

Quote
by using capacitors and resistors with this formula
That is just the formula for capacitive reactance.

Basically an envelope follower is a filter because you are removing high frequencies, the audio signal, just leaving the peak amplitude. Make that capacitor bigger and you are lowering the frequency of the filter again. It is only a first order filter, if you want sharper cutoff then use a higher order filter like a fourth or sixth order one at the frequency of beat you want to trigger off.

Stoil

Hello friends,
Just came home and tried it out. There's just 2.1V after the diode, however Arduino is reading crap. On top if it, it stopped displaying what it reads on the Serial, weird. No matter how loud is the volume or if a song is playing or not, the values received are the same.
What is going on here

Stoil

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.

Grumpy_Mike

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

Stoil

https://imgur.com/a/01EcxnU 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.

Code: [Select]
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);         
}

DVDdoug

Quote
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 little tutorial 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).





DVDdoug

Quote
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.

Grumpy_Mike

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

Stoil

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.

DVDdoug

Quote
It looks to me like an electrolytic capacitor, maybe 22uF?
:D :D 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.

Stoil

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 ?

DVDdoug

Quote
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.

   

Go Up