Arduino Forum

Using Arduino => Audio => Topic started by: Stabbler on May 13, 2018, 11:02 am

Title: Getting sound level from audio jack
Post by: Stabbler on May 13, 2018, 11:02 am
Hi all,

I am currently in the process of making a hacked billy bass. I am using the following tutorial:
http://www.instructables.com/id/Animate-a-Billy-Bass-Mouth-With-Any-Audio-Source/ (http://www.instructables.com/id/Animate-a-Billy-Bass-Mouth-With-Any-Audio-Source/)

The part that is confusing is about getting the audio sound levels as an input.
In this tutorial the maker just hooks up the audio jack to the analog input port.

I have done some research. Instead of making it clearer, it got more confusing.
From this thread (https://forum.arduino.cc/index.php?topic=476900.0) I understand you should not hook the audio jack directly to the anolog pin of the arduino.

Note that I am a complete newbie to electronics, instead of just following instructions I like to understand what I am doing.

In the thread the following schematic was posted:
(http://forum.arduino.cc/index.php?action=dlattach;topic=121540.0;attach=34854)


I am unable to comprehend what that circuit does and how it functions.
Things I do not understand/basic electronic lessons needed:



Please be gentle, as I am very new to this.

Thanks in advance!
Title: Re: Getting sound level from audio jack
Post by: vffgaston on May 13, 2018, 11:19 am
Hi,

Well, it is not an easy circuit to fully understand it just by "wikying" ...

To help you we will need to know what your background is: basic electronics, programming, electrical circuits, ...

Regards
Title: Re: Getting sound level from audio jack
Post by: Stabbler on May 13, 2018, 12:22 pm
Hi Gaston,

Thank you for your reply. I am a software developer so my programming skills are ok.
I have always had an interest in the hardware side of things. I have played a bit with a raspberry pi, and recently purchased some Esp chips.

I have never really made complicated circuits.I have just plug some sensors/buttons in.
The only knowledge I have regarding electronics is from Highschool Physics class. Which is not much I am afraid.

The difference (how I perceive it, I could be wrong) between the 'sensor' I am trying to build now and the one I used is that the other sensors were 'powered' by the ESP/Raspberry pi. Thus the voltage is always between suitable ranges of the RaspberryPi/ESP. (Input of the sensor is also the max voltage out.)

Thanks for taking the time to reply.

Title: Re: Getting sound level from audio jack
Post by: MikeLittle on May 13, 2018, 04:07 pm
You have 2 INPUTs tied together. Where is the audio OUT?
Title: Re: Getting sound level from audio jack
Post by: DVDdoug on May 13, 2018, 05:33 pm
Quote
I am unable to comprehend what that circuit does and how it functions....


...Why does the A0 pin has to be connected to the 5V pin? (with a resistor) It is some sort of voltage divider.
When there is no input from the audio source. The voltage to A0 will be halved: So it will be 2.5V at rest.
What happens when there is coming input from the audio source?
How will that interact with the signal to A0?
I assume when there is no incoming signal: Current on A0 will be 2.5v -> A0 input will read 512.
I read that such a audio jack produces an AC Current. To measure how loud the sound is should I take the absolute value of 512 - [actual input from A0 pin]?
Right!!!

Audio signals are AC.   They swing positive & negative.   The Arduino can't read negative voltages.  In fact it can be damaged by negative voltages (or voltages greater than +5V) and/or the audio signal can be "damaged" (distorted).

With the voltage divider in place, the DC voltage is added to the audio signal.     With low level/quiet signals you'll get ADC readings that "swing' slightly above and below 512 and high/loud signals can read between 0 and 1023.    You can subtract the bias out of the reading in software if it helps in your application.

Quote
What is the capacitor used for.
A series  capacitor "blocks" DC, but allows AC through.   The capacitor along with the resistors form a high-pass filter (https://www.google.com/search?q=high-pass+RC&newwindow=1&tbm=isch&source=iu&ictx=1&fir=zPT0uVn-vO3cXM%253A%252C_f4h38pKEpcF_M%252C_&usg=__xHAlzaq4-56AIg95Qs56POGRPf4%3D&sa=X&ved=0ahUKEwjzwKOC_4LbAhURC6wKHWVFDGEQ9QEIPDAD#imgrc=zPT0uVn-vO3cXM:).    Since DC is zero Hz, it gets blocked.   (If the capacitor value is too low, it will also block the bass, etc.)

Without the capacitor, the connected device could potentially mess-up the 2.5V bias and/or the DC bias could get-into the connected device and cause unknown/undefined problems.
Title: Re: Getting sound level from audio jack
Post by: Stabbler on May 13, 2018, 10:10 pm
You have 2 INPUTs tied together. Where is the audio OUT?
The audio output will come from an external source. Which will be used as an input for the arduino. The Arduino will not have an Audio Out. (The plan is to use a splitter to send the audio to a speaker.

Quote from: DVDdoug
With the voltage divider in place, the DC voltage is added to the audio signal.     With low level/quiet signals you'll get ADC readings that "swing' slightly above and below 512 and high/loud signals can read between 0 and 1023.    You can subtract the bias out of the reading in software if it helps in your application.
That is a neat trick. One thing that I am missing is that there is no protection for when the current from the audio source is to big. In theory when the volume is high enough it might still reach >5V?
Or will this value never be reached? (Because of guidelines for audio output devices perhaps?)


Quote from: DVDdoug
A series  capacitor "blocks" DC, but allows AC through.   The capacitor along with the resistors form a high-pass filter.    Since DC is zero Hz, it gets blocked.   (If the capacitor value is too low, it will also block the bass, etc.)

Without the capacitor, the connected device could potentially mess-up the 2.5V bias and/or the DC bias could get-into the connected device and cause unknown/undefined problems.
Thank you for the explanation, tomorrow after work I will look for more information about capacitors and their uses. (And try to understand the math and behaviours behind it.)


I came across another schematic as a possible solution for what I am trying to do. I will try to understand that schematic myself first (and then probably post it here to check if my understanding of it is correct).

Thank you very much! Your help is appreciated.

Thanks you!
Title: Re: Getting sound level from audio jack
Post by: Grumpy_Mike on May 14, 2018, 06:18 am
Quote
In theory when the volume is high enough it might still reach >5V?
It is unlikely from an audio jack.

However that circuit will not help you find the audio sound levels as an input, it will allow you to read in the audio waveform.

You must learn that most Instructables electronics articles are crap, written by people who think they know more than they do. What you need to do this is a peak envelope detector circuit. Like this one:- https://en.wikipedia.org/wiki/Envelope_detector (https://en.wikipedia.org/wiki/Envelope_detector)
Title: Re: Getting sound level from audio jack
Post by: MarkT on May 14, 2018, 12:52 pm
Though you can do the peak detection in software of course, if you are sampling the input
waveform.  Minor quibble - that circuit lacks any over-voltage protection - adding 10k between
the voltage divider and the analog pin will make it pretty-bomb-proof.  Make R1 100k, R2 10k,
C can be around 1uF - if its polarised the negative goes to the audio input jack.

Adding an opamp for hardware peak detection increases the hardware complexity, software
approach avoids that.
Title: Re: Getting sound level from audio jack
Post by: Grumpy_Mike on May 14, 2018, 02:08 pm
Quote
Minor quibble - that circuit lacks any over-voltage protection
But you are not likely to get over 5V peak from an audio jack. You also have the forward volts drop of the diode and the capacitor to absorb brief spikes.
A 10K series resistor will reduce the attack speed of the detector and make it a lot more sluggish in its response.
Title: Re: Getting sound level from audio jack
Post by: Stabbler on May 25, 2018, 02:39 pm
Hi guys,

Thanks for your input. As Grumpy Mike suggests, I should make an envelope detector.
I thought I would use the schematic posted here (https://www.hobbyist.co.nz/?q=making-led-lights-dance-to-your-music).

I am running into a question.
What kind of capacitor should I use? I see different kinds: Ceramic, aluminum, .....
Does it matter which one I buy? As long as I have the right value? 10uf?
Title: Re: Getting sound level from audio jack
Post by: Grumpy_Mike on May 25, 2018, 04:01 pm
Quote
I thought I would use the schematic posted here.
Bad idea, it is crap, no diode.

Quote
Does it matter which one I buy?
No.
Title: Re: Getting sound level from audio jack
Post by: Stabbler on May 27, 2018, 01:08 pm
So I searched a bit on the internet, all I need to do to adjust the schematic, is insert a diode before the resistor?
Title: Re: Getting sound level from audio jack
Post by: Grumpy_Mike on May 27, 2018, 04:32 pm
what is wrong with the circuit in the link https://en.wikipedia.org/wiki/Envelope_detector (https://en.wikipedia.org/wiki/Envelope_detector)
Title: Re: Getting sound level from audio jack
Post by: MarkT on May 27, 2018, 06:07 pm
But you are not likely to get over 5V peak from an audio jack. You also have the forward volts drop of the diode and the capacitor to absorb brief spikes.
A 10K series resistor will reduce the attack speed of the detector and make it a lot more sluggish in its response.
I'm thinking nylon carpets and static for the source of spikes really, and the 10k on the analog pin doesn't
affect the attack speed surely?
Title: Re: Getting sound level from audio jack
Post by: Stabbler on Jun 19, 2018, 04:28 pm
Hi All,

Been a while as I did not have time to play with this project. Yesterday evening I fiddled a bit with the project.
I tried to make the envelope follower linked to by Grumpy Mike. Unfortunatly it did not seem to work.
(I have probably attached something wrong.)
I used a potentio meter for the Resistor, to dynamicly change it. Put a diode on it and a 2.2Uf capacitor, as well a 10uf capacitor.
I got no inputreadings on the A0 pin whatsoever.
I still don't understand how the schematic works. Where in the schematic should I measure the voltage (i.e. connect to A0). As I think I might have that wrong.

I did try the other schematic I posted (This one (https://www.hobbyist.co.nz/?q=making-led-lights-dance-to-your-music)). I realise this schematic is bad, and it can damage the arduino. (So anyone reading this, don't try this..)
I was able to get the desired effect. (Although it is very jumpy.)
When inserting a diode to this schematic (between the potentiometer and the Capacitor) I can't detect a signal anymore.
(I am pretty sure I inserted the Diode the correct way(Such that the positive current can flow though it.)

Any ideas on what I am doing wrong?

My 'ideal' solution is to use the Envelope Follower provided by Grumpy Mike. Can it be that I used an incorrect Capacitor what value of Capacitor would you guys suggest? Also, does any diode work? On the packaging of the diode I am using it sais: 1N4001 
Title: Re: Getting sound level from audio jack
Post by: Grumpy_Mike on Jun 19, 2018, 06:10 pm
Quote
Any ideas on what I am doing wrong?
Not without you posting a good picture of your wiring.

Quote
On the packaging of the diode I am using it sais: 1N4001
That is a rectifier diode and while it will work the response is not so fast, so try a small signal diode like a 1N4148.

Also with a diode your audio signal needs to be above 0.7V for the diode to conduct.
Title: Re: Getting sound level from audio jack
Post by: Stabbler on Jun 20, 2018, 03:37 pm
Not without you posting a good picture of your wiring.
That is a rectifier diode and while it will work the response is not so fast, so try a small signal diode like a 1N4148.

Also with a diode your audio signal needs to be above 0.7V for the diode to conduct.
Thank you Mike.

I understand it is dificult to give an answer without a picture of the wiring. (It is also hard to produce a clear picture of the wiring.)

I will probably drive to the store to get the 1N4148. And see if that works. If it fails I will take another attempt at making a clear picture.

Does the signal also has to be above 0.7V for the 1N4148?
Title: Re: Getting sound level from audio jack
Post by: Grumpy_Mike on Jun 20, 2018, 04:12 pm
Quote
Does the signal also has to be above 0.7V for the 1N4148?
Yes it is the forward voltage drop of a silicon diode.
Title: Re: Getting sound level from audio jack
Post by: Stabbler on Jun 20, 2018, 08:20 pm


(https://preview.ibb.co/bQcDud/IMG_20180620_200540.jpg) (https://ibb.co/kdSrfJ)
(https://preview.ibb.co/fm0Yud/IMG_20180620_200552.jpg) (https://ibb.co/dM8xLJ)
(https://preview.ibb.co/kAAcLJ/IMG_20180620_200616.jpg) (https://ibb.co/didmEd)

This is my current set up.

Resistor = 100k Ohm
Capacitor = 0.22UF
Diode = 1n4148

I did not have the right colours jumper wires laying around, so this will have to do.
The blue wire is connected to A0 of the nodemcu, the yellow wire is connected to the ground.

With very load volumes I am getting a reading, otherwise I get no reading at all.

Can this be due to the fact that the diode only conducts when the voltage is above 0.7V?

Is there another Diode I can try whos forward voltage drop is lower?
Or should I find a way to amplify the signal?
Title: Re: Getting sound level from audio jack
Post by: Grumpy_Mike on Jun 21, 2018, 06:30 am
Thanks for the pictures.

Quote
With very load volumes I am getting a reading, otherwise I get no reading at all.
Well that shows something is working at least.
Try dropping that capacitor to 22nF or so and see if it improves.

Quote
Can this be due to the fact that the diode only conducts when the voltage is above 0.7V?
It can in part but with such a small signal it is not going to be much good if you get a lower forward volts drop diode. You could either use a Schottky diode or a germanium diode but the volts drop is still in the order of 0.2V.

Quote
Or should I find a way to amplify the signal?
Yes, look at getting an op-amp that you can run off 3V3 so you don't have to worry about sending too much voltage to the processor.