Improving the sound detection circuit, part 2 - Op Amps

JezWeston

New Member
Okay, I built the circuit that Boriz suggested, thanks Boriz:
http://img223.imageshack.us/img223/5739/audiosensorob2.gif

And it works, it works better than the version with transistors. Its getting there but the sensitivity is still poor. It doesn't quite have to be be inside the speakers, but its still far from what I am after.

For the gain, I started with setting R5 to be 40 times R2. The sensitivity got better as I raised the ratio. Currently its 3000 times as much. Raising it further doesn't seem to help a great deal.

So obviously, I'm doing something wrong here. The op-amp seems to be behaving as I think it should. Is the problem with the microphone?

Component Values are:
R1 47k (giving 2.2V over the elecret)
R2 15
R3 47k
R4 set to 2k4
R5 Set to pretty much zero
R6 220k
C1 470n
C2 120n
And I'm using one op-amp from a 358.

(My +5V supply is only 3.8V, but that shouldn't make a difference?)

And my test code is:
SYMBOL counter1 = b1
SYMBOL temp_var = b2
SYMBOL temp_var2 = b3

Main:
temp_var2 = 0

'High point latching to catch transients
for counter1 = 1 to 5
readadc 0, temp_var
if temp_var > temp_var2 then
temp_var2 = temp_var
endif
next

if temp_var2 > 128 then
pins = %11111111
elseif temp_var2 > 64 then
pins = %01111111
elseif temp_var2 > 32 then
pins = %00111111
elseif temp_var2 > 16 then
pins = %00011111
elseif temp_var2 > 8 then
pins = %00001111
elseif temp_var2 > 4 then
pins = %00000111
elseif temp_var2 > 2 then
pins = %00000011
elseif temp_var2 > 1 then
pins = %00000001
elseif temp_var2 < 1 then
pins = %00000000
endif

Goto Main

(Obviously, if I can get a decent range out of the sound input, then these thresholds can be calculated on the fly for AGC.)

Any hints gratefully accepted.
 

JezWeston

New Member
A bog standard electret insert microphone.
http://www.jaycar.co.nz/productView.asp?ID=AM4011

Costing all of four dollars.
 

Michael 2727

Senior Member
Electret microphones can be dammaged easily, they have a small MOSFET inside
and static can kill them or drastically
reduce their performance, buy a few of them.
To get really good gain from an OP amp they
use 2 stages rather than a single. The 358
has 2 amps in it, you could try linking up the other stage.
Sound can be tricky to work with, I imagine that a rave party SPL Level would not be
below 85 to 90 dB and may go to 110 to 120 dB close to the sound source. I guarantee
that if you produce 90 dB inside your house
your wife, girlfriend or mother will kill you.
What may seem very poor performance at home may be exactly what you need at a rave party.
Experiment.
 

boriz

Senior Member
First thing I notice is that R1 should be much smaller than 47K!

Try 3.3k or 2.2k

And when you say R5 is zero, that means 100% negative feedback. That’s no gain at all!

The gain of a simple inverting op-amp circuit like this is determined by the ratio of R2 to R5. If R5 is 10 times larger than R2 then the gain is roughly 10. (eg: R2=1k and R5=10K). And if R5 is 100 times larger, then the gain is roughly 100. (eg:R2=1k and R5=100k)

So for larger gain, make R2 smaller and R5 larger.

Also make C1 larger if you can, for better low frequency response. Try 10uF. If using electrolytic, put positive terminal to mic output and negative terminal to resistor.

C2/R6 will decay from a 5v peak down to 2.5v in around 20mS. Is that long enough? I would go for a larger cap here too.

P.S.
I would raise the voltage to 5v if possible. The op-amp will work better.


Edited by - boriz on 24/06/2007 06:58:26
 

JezWeston

New Member
Thanks for the detailed replies.

Sorry Boriz, I measured the wrong end of the variable resistor - R5 is 50K, not zero.

I've swapped R1 for a 2k2, didn't make a noticeable difference, although I should point out that in the light of my lack of an osciliscope, small improvements may not be being picked up.

I shall pop down the shop tomorrow and get myself a battery box to feed the thing off 4 AA's, not 3, and see what that does. I'll also get a different electret, in case I have damaged this one with all my pfaffing.
 

Bob Elton

New Member
I just tried Boriz'ss circuit but using an MCP602 (I didn't have a 358).

I could easily get greater than 50x voltage gain, though i was using a sig gen.

With a suitable C2 I got a pretty good 'rectified' signal which followed the amplitude of the overall input level.

BUT... without AGC (or maybe doing a fancy bit of ADC input processing) I can see it having problems with guaranteeing peak detection. I think someone mentioned it before, that true peak detectors 'track' as well as detect maximums.

i.e. if you have a fixed threshold reference on your READADC you will (under certain circumstances) sense it as max for long periods because the 'averaged' input is over your threshold. So, over a range of levels it'll probably be fine, but if it gets low (unlikely) you'll get nothing, and if its mega loud then it'll be on all the time.

The other thing is that without a 'scope the component values guess-work may take ages.
 

boriz

Senior Member
With R2 at 15K and R5 at 50K, the gain will be a little over 3! Not nearly enough.

Try R2=1K and R5=47K (gain of around 50).

Or R2=1K and R5=100K (gain of around 100).
 

moxhamj

New Member
If you want AGC there is a circuit using 2 diodes. All op amps need a 1/2 rail supply and it needs to be stable and once you get more than a couple of op amps in a circuit it is worth building a stable 2.5V supply (assuming a 5V supply). Use two 10k resistors as a voltage divider then into the + of an op amp and connect the - and output together. This will generate at stable 2.5V reference. Use a 324 or similar quad op amp. The circuit you have is an inverting amp with variable gain and so the + goes to the 2.5V ref and set the gain at about 50. Then run this into a second op amp. + to 2.5V. 10k between output of the first op amp and the - of the second one. Then 914 diode from the - to the output. Then a second 914 from the output to the -, ie the other way round. This produces an output signal of 2.5V +/-about 0.6V over a large range of input voltage variations.

The above circuit is using 3 op amps. You could do it with 2 if you had to by using two 1k resistors as the voltage divider but there might be some crosstalk between the two stages. The other way is to use a +5V and -5V supply but I find the virtual earth at 2.5V is easiest.

The output of the AGC goes into the peak level detector. There probably isn't much point amplifying the output further 2.5V+/-0.6V will peak at 3.1V. You could amplify by 2 to get a peak of 3.7V but this is getting close to the max output volts (usually 1V from the supply voltage).

I can't seem to find this circuit anywhere on the internet. It is very useful and a lot simpler than using transconductance amps with variable gain.

Edited by - Dr_Acula on 25/06/2007 04:03:17
 

boriz

Senior Member
15 OHMS?

C1/R2 form a high pass filter. With the values C1=470nF and R2=15 OHM then the -3dB cutoff for this circuit is over 22Khz! That’s beyond the audio spectrum!

Not only that, but the total impedance of C1/R2 is less than 100 OHM and since the output of the previous stage (the mic) has an impedance approximately = R1, only a fraction of the signal coming from the mic is actually getting to the op-amp.

Try this:

R1 2k2
C1 10uF (+ve to the mic)
R2 2k2
R5 220K

This will give you a gain of 100 with a -3dB frequency cutoff just under 10Hz. You should get plenty of output from music.
 

premelec

Senior Member
A slight [ha!] mismatch in impedance between the mic and amp input - I think you've nailed it Boriz!
 

JezWeston

New Member
Hmm, tried all those changes, didn't really make a huge difference. Still poor sensitivity and the Picaxe seems to miss picking up lots of the beats, despite spending the majority of its time in a readadc loop.

I think I'm going to have to borrow a scope and see:
a) What the elecret is actually producing
b) What the gain is across audio frequencies
c) What the hold time is on the outputs

So I can do a better job of understanding what's going on here. The other option is to try a different op-amp, if there's a problem with that.

One odd point, to get a 0V output, R4 has to be set to zero ohms. R3 is 4k7, so the non-inverting input is at 0V. Should that be the case?

And with R4 set to ~300 Ohms, the output oscillates at about 2 Hz. This makes me go huh?
 

Bob Elton

New Member
I don't really know how you can set up a circuit WITHOUT a 'scope.

And your gain of 3000 is slightly unlikely. I take it your op-amp experience is not-a-lot. Apart from calculating theoretical gain, I take it you haven't considered gain-bandwidth product / unity gain figures?
 

JezWeston

New Member
Haven't considered a what?

My electronics experience is pretty much limited to following the instructions. But hey, I can read instructions quite well, and the people here have been very helpful.

One thing that does confuse me (amongst many others) is that the output of this circuit seems to be very little, until a certain volume threshold is reached. Then it maxes out very quickly indeed. This is not quite what I'm after.
 

Bob Elton

New Member
Without going into a boring paragraph, op-amps gain is a function of frequency; Gain/Bandwidth product. Op-amps do not have an infinite gain at infinte frequencies. Tink of it like a frequnecy response.

If you get a 'scope and some spare time you can see that the amount of gain won't get bigger regardless of your R ratios. And the gain will be affected by the frequency you chuck in. In your circuit using the 358 I can't imagine it will prevent you getting a gain of over 50 at lower audio fs. So, put GBP to one side for now, but it simply explains why you can't get huge gains regardless of resistor ratio.

When I was trying it with a MCP602 I used R2=1k) and R4=100K and got good results from a sig gen. I haven't got a mic to try.

But, back to the plot: if you had a 'scope you would see that the R4 affects the DC offset of the output. Your o/p signal is superimposed on that offset (level).

Hence, if your settings or component values aren't carefully done then your results could be bonkers - it's scope time!

Hence also the lack of dynamic range without AGC. With a 'scope you will see that your 'rectified' output may be full on wrt your ADC reading/threshold.

Once you can see these things on a 'scope you will appreciate the 'challenge'.

Unless you have AGC or 'track' the previous average level within your programme your boomph/flash will be a bit limited. Don't get me wrong, get the settings in that circuit good, and you will get some results. But to make it really good you'll need to desing a bit more.

Step1: As you say, borrow a 'scope; preferably a storage type. And if you can borrow a sig/func generator you will learn SO much about op-amps while you're adjusting.
 

boriz

Senior Member
I suggested a potentiometer for R4 so that you could experiment. But I don’t recommend you use a value for R4 that gives you much less than 1v DC offset at the +ve input.

Use these components:

R1 2k2
R2 2k2
R3 47K
R4 10K
R5 220K
R6 22K
C1 10uF (+ve to the mic)
C2 10uF (+ve to diode)

If you need a longer decay time, use a larger C2.
 

boriz

Senior Member
On the subject of AGC. You could try using a digital potentiometer in place of R5 and control it from the PICAXE.
 

JezWeston

New Member
I could indeed use a digital pot for that, once I get the thing to behave itself. With your settings, and a dc offset of +1V, it does what I want it to, although it still requires a lot of volume before it starts to produce a signal.

Turning up the gain (i.e. R5 above about 15k) makes the output oscillate at about 2 Hz. At first I thought this might be due to changes to the supply voltage as the Picaxe was turning on a bunch of LEDs, but I can get this effect with the Picaxe set to turn on just two. This changes the supply voltage by less than 20 mV.

The effect also requires a transient to kick it off, almost as if the circuit was ringing like a bell. So, any idea what's going on and what I could do about it?
 

JezWeston

New Member
Reading around and looking at similar circuits, lots of them have a 10uF capacitor between the supply rails. I guess this would help stabilise the supply voltage, so I'll get one and give it a go tomorrow.
 

JezWeston

New Member
Ok, it turns out I am a muppet. Borrowed an osciloscope and turns out that this circuit is working just fine. Very responsive, even to very quiet sound.

So the problem lies at the ADC end, in the Picaxe. I shall continue fiddling, with the right tools this time.
 

boriz

Senior Member
Glad you are making progress. I don’t know what the input impedance is for a PICAXE ADC, but it’s probably much lower than the ‘scope. ‘Loading’ the peak detector might be effecting the circuit, so you might try using a buffer.

The other op-amp (call it op_B) in the 358 would be ideal. To make op_B a buffer, connect it’s output (pin 7) to it’s –ve input (pin 6). Now connect the signal coming from your peak detector to the +ve input of op_B (pin 5). The output of op_B (pin 7) will now track the signal voltage from the peak detector without effecting the signal by ‘loading’ it, which might be what the ADC is doing. Connect op_B’s output (pin 7) to the PICAXE ADC and that should eliminate impedance matching problems.
 
Top