Op Amp Peak Detector

OK Signality, or anyone else (I am used to being rescued by Signality ... who should probably charge for his/her services!) I created a MIC OP AMP circuit with peak detection at the end. The peak detection is based on . My circuit is .

The reference circuit seems to work perfectly, while mine doesn't show peak as well. I've tried a number of different resistor values for RL and R19, but I can't seem to get it to behave correctly.

If you are curious, the point of the circuit is to create a peak voltage to be monitored by an Arduino microcontroller (in fact, two of them).

If you have any ideas, I really appreciate it.

by khauser
December 22, 2012

Hey! Charge for my services? Now that's an idea!

But I need to establish a bit of street cred first ...

:)

You're running the opamps off a single supply. My example was from split supplies.

You've pulled the peak detector non-inverting input down to below 2.5V (ground) so the output is pushed up towards the 5V rail.

You need to split R13 into 2 off 20k resistors one to ground the other to +5V to take OA15 non-inverting input to 2.5V.

If you have R13a/b (R13 split in to 2 equal valued parts) and R20 > 0 then you don't need RL.

But you only need R13a/b < infinite & R20 > 0 if you need a gain of peak detector gain > 1.

If you don't need gain then remove R13, set R20 = 0 and use RL to leak C3.

It also helps to reduce C3.

BTW, CL seems to struggle with getting these sims started. I had a quick go with ramping up the 5V supply but that didn't seem to help.

Too much time having to work around CL's oddities, not enough time running sims.

Ho hum.

:(

Another peak detector is:

In that, if RL is omitted, then R2 can be used as the leak for C1 to the virtual ground formed by the inverting input.

Have a close look at the signals around the sim to see how it works.

by signality
December 23, 2012

Thanks! I'm only pretending to understand analog design (who could tell?)

I sure am learning though!

BTW, the original circuit was posted by mrobbins? Are you the same person? Or did he post your circuit??

If you have any thoughts on which peak detection mechanism is better for my purposes I'm all, well, eyes!

Again, the circuit amplifies a microphone and creates a voltage to be read by Arduino's analog input.

by khauser
December 23, 2012

"If I only had a clue!"

I tried two variants: and

The first one the peak outputs all gradually increase. (I wouldn't mind amplifying to get a 0-5V range, but the Arduino has a reference voltage so I can get good results at a non-amplified 2.5V top.)

The second attempt shows I still have a lot of learning to do because I am not sure why it's a flat 2.17V output..

I have tried the other peak detector in the past with similar results but I am going to remodel it now since I'd made the mistake of not using a split reference input.

by khauser
December 23, 2012

Ugh!

That one doesn't work well at all with my circuit ... it's pulling the signal way down.

You wouldn't happen to have a book in mind around this kind of circuit? I am a rank beginner with anything non-digital...

by khauser
December 23, 2012

I'm me, not mrobbins.

I don't have any connection to CL, I'm just helping out the community.

Do a search of CL for signality peak detector.

to see my peak detector postings.

This:

is a nice robust design that gives better performance at high audio frequencies because it limits how far the opamp output has to slew up from on each peak. It also prevents the delay is the opamp output moving because it has swung all the down to the opamp negative output limit and saturated in between each peak.

Note that the voltage at virtgnd follows the input signal so the leakage from the peak detector capacitor tends to be less than for a resistor RL to ground if there's a lot of input cycles that are close to the peak level and more if the input is at a low level. Tricky to get your head round but tends to reduce the time it takes the peak detector to recover from a large peak before it can detect a small one later.

Simplest way to use it is to set:

Rdummy = 0

R1 = open cct.

R2 = whatever you want for the leakage resistor (depends on the CL recovery time constant you need).

Using a full wave rectifier circuit in front of the peak detector reduces low frequency "pumping" in AGC and compressor circuits caused by asynchronously sampling an output that has drooped between the peaks of low frequency signals.

Search CL for signality rectifier to see some examples.

You'll also find in CL, a variant of the peak detector from an idea I developed about 30 years ago that gives an output pulse immediately after the peak has been detected. That may be handy for sampling exactly when the peak has occured.

Now, the mistake you've made is that you didn't split R13 into 2 off 20k resistors, one to ground, the other to +5V to take OA15 non-inverting input to 2.5V.

You've tried to use the existing R8, R9 2.5V resistor ladder. You need to use a separate pair of resistors.

The parallel combination of the two (half the individual values because they're equal valued) forms the ground end resistor Rgnd of the non-inverting opamp gain equation of:

Vout = Vin*(Rfeedback/Rgnd+1)

If you try to use the resistor ladder that sets 2.5V for the rest of the circuit, you have connected the rest of the circuit into the feedback loop for the peak detector opamp!

If you don't need a gain of greater than 1 then just remove any connection of the non-inverting input to anywhere and then replace the feedback resistor with a piece of wire.

It's up to you and the Arduino spec to see if you need gain or not.

Note that with this circuit, the peak detector output will always be centred on 2.5V.

If you use a 4th opamp to buffer the 2.5V reference point then you can connect all the points requiring to be biased at mid rail to the output of that opamp because it looks like a low impedance voltage source. A simple, single Rgnd for the peak detector can then be connected to this 2.5V source.

by signality
December 23, 2012

So I'm doing my best to puzzle through this exercise. I've created a separate resistor ladder.

Does "If you don't need a gain of greater than 1 then just remove any connection of the non-inverting input to anywhere and then replace the feedback resistor with a piece of wire." mean the non-inverting side has no connection at all?

Then comes the peak detector output being centered on 2.5V and the use of another opamp. I haven't yet figured out what that looks like.

I can be quite specific on my needs, if that helps. (Some of this is regurgitating stuff I've already said.) I have an electret condenser mic feeding in to two op-amps. That part of the circuit is working quite well, thanks in no small part to your help.

Since the ultimate goal is to feed a value in to the Arduino's ADC, I need to measure the peak of that audio waveform (and yes, I am only interested in measuring loudness). That's why I started looking in to peak detection. The Arduino's ADC has an external reference input, so I can "live" with just about any reasonable value between, say 1V and 5V and still get a full scale resolution.

The circuit so far: has caused me even more confusion (that's easy). This has affected the Stage 2 output so that the 1mv waveform centers on 2.5V while the 11 and 21mv signals are 0-based rectified signals. That I assume is because of the rectifier following it. Then (3rd graph) I find the peak values for the 11 and 21mv inputs are near identical. So yeah, I have no idea what I am doing. I do enjoy the learning, but I am also running out of time. I meant to use this week and next to finish the project (I'm on vacation) ... so I really appreciate any help you're willing to provide!

by khauser
December 26, 2012

Gnnnnffffggnnnaaaaargh!

No wonder you're confused. CL is just making such hard work of actually running the sim correctly.

It is so frustrating for users to be held up by CL failing to run sims that just work in other simulators!

It takes more time to find the workarounds for CL than it does to design and run the sim in the first place. And that leaves inexperienced users in a really confusing and discouraging place. Their circuits may be fine but because they don't run in CL they go round in circles because they think it's their fault, when in fact it's CL being awkward.

Sure this happens in other simulators but much less so with circuits at the level people are generally working at in CL.

Bah! Humbug!!

Maybe like all good open source/hardware developers, maybe I should start asking for donations ...

Anyhow, rant over ... here are several ways to do what you're trying to:

https://www.circuitlab.com/circuit/5at9u8/khausers-peak-det-x1-gain/

https://www.circuitlab.com/circuit/xu3z73/khausers-peak-det-x2-gain-01/

https://www.circuitlab.com/circuit/germ7k/khausers-peak-det-x2-gain-02/

https://www.circuitlab.com/circuit/9dmma9/khausers-peak-det-x2-gain-03/

Note that in:

https://www.circuitlab.com/circuit/germ7k/khausers-peak-det-x2-gain-02/

https://www.circuitlab.com/circuit/9dmma9/khausers-peak-det-x2-gain-03/

if you do not fit R22 then the peak detector gain collapses back to x1.

Help on designing with opamps:

www.ti.com/lit/an/slod006b/slod006b.pdf

www.ti.com/lit/an/sboa092a/sboa092a.pdf

http://www.analog.com/library/analogDialogue/archives/39-05/op_amp_applications_handbook.html

Also search CL for Learning Resources

by signality
December 27, 2012

OMG that was a boatload of GREAT information! I spent hours especially on the TI pages.

Your circult https://www.circuitlab.com/circuit/xu3z73/khausers-peak-det-x2-gain-01/ would be great if it was 0V based ... what would it take to do that?

Also look for a private email....

by khauser
December 27, 2012

Maybe this is another simulation issue? I added a simple blocking capacitor right before the stage2 output. I thought that would remove the bias from the waveform. But wow, that changed the results!

The circuit works way better without the capacitor, at least in the sim. (That's why I named it 'close'.) I may just breadboard it later today to see how close it is to reality (I do have an oscilliscope).

Thanks again!

by khauser
December 28, 2012

" ... would be great if it was 0V based ... "

Do you mean the output sits at 0V for no signal and goes positive for increasing signal level?

Easy with split +/-ve supplies.

It can be done on a single supply rail but is a bit tricky.

You either need an opamp that can tolerate input voltages that swing as far below ground as the positive peaks go above it or a circuit that sits the input at mid rail and can then offset it to ground.

The alternative is to keep the mid rail peak detector as shown in the examples so the output is based at mid rail but then to use a difference amplifier stage after the peak detector to subtract the mid rail voltage from the peak detected output.

" Also look for a private email.... "

Mine?

If yes then have a visit to my website; url in my CL profile.

There are two problems with:

https://www.circuitlab.com/circuit/9hgnuc/

i) C6 and C5 block any DC path for the non-inv opamp i/p bias current. That's why in:

https://www.circuitlab.com/circuit/9dmma9/khausers-peak-det-x2-gain-03/

there's R24 to provide the DC path for the non-inv i/p bias current.

ii) having a simple diode and a cap as the peak detector only works well for voltage swings greater than the forward drop of the diode (approx 0.6V for a silicon diode).

The beauty of the precision peak detector is that the opamp removes the diode drop and any temperature drift in the drop.

by signality
December 28, 2012

Yup, 0V for no signal, positive for increasing.

I've tried (in the past) using a full-wave rectifier before peak detector. That was before the latest set of circuits and I think suffered from inexperience with sims on my part, so maybe I will try that again.

I will replace the diode/cap with an op-amp based circuit ... I was being lazy for testing.

As for the private email, yes, I sent it to the email in the contact section of the web site. I could resend it through Linked In....It's not critical ... just didn't think it belonged in this thread. I could resend it ... I've discovered your name in the mean time so it would be less impersonal!

I was looking at the differential amplifier to solve my problem. I'll see if I can figure it out ....

Thanks! -Kevin

by khauser
December 29, 2012

I think I have success!

I was surprised I had to use an approx. 3.3V input to the differential amp instead of a 2.5V, I'm sure it is obvious to you!

On to the breadboard! Have I thanked you enough? I would never have made it this far without your help. I probably would have punted and created a software algorithm to take a bunch of samples in rapid sequence and keep the highest value. Software I can do! But that seemed like such cheating...

by khauser
December 29, 2012

Well done!

I haven't looked in detail at your solution but I suspect that the 3.3V arises because the peak detector has a gain of 2.

You'd be as well to add the series AC coupling caps shown in my examples to remove as much DC offset from the x100 opamp gain chain as possible otherwise your low level peaks may be swamped by it. 1uF caps with 10k resistors give you a high pass filter cutoff frequency of about 16Hz so your low frequency response should still be OK.

A slightly simplified version here:

adds the coupling caps and simplifies the level shift. It also reduces the DC offset error introduced by any mismatch and tolerances by the various attenuators/resistor ladders.

Also, don't forget that if you offset the output all the way to ground, (a) if the DC bias is slightly out then you may lose low level signals by the being clamped to ground by an offset that is trying to force them below 0V and (b) you will have to use an opamp with an output that can swing down effectively to 0V. The LM324 will not do this, so you'll need low voltage rail to rail output (but not necessarily input) opamp.

Also, a not very well publicised or appreciated fact of rail to rail output amps is that the open loop gain rolls off like a stone as the output gets near to the rails which can create so very strange errors and effects until you realise that is what's happening.

If you can, you may actually be better off keeping the signal swing bias to mid swing for zero input.

Or use split supplies. The you have an accurate 0V reference point!

If you want to play with precision rectifiers, I have posted several examples in CL as I think I said earlier.

:)

"Software I can do! But that seemed like such cheating..."

Back in about 1990 I took my in depth knowledge of how TV and video cameras worked and rubbed it against my working knowledge of DSP to take a complete 625 line PAL colour CCD video camera/LCD monitor chain down from over £300 each in quantity to about £50 by doing everything from the jfet buffer on the CCD output through to the LCD panel RGB to LCD row/column scanning on-the-glass-ASICs digitally.

I didn't actually design the ASICs to do that but I defined the algorithms and wrote the pseudo code to describe them.

http://www.britishtelephones.com/rel2000.htm

Actually, I'd much rather do many things digitally! Nowadays it's so much easier to do that because powerful, fast processing is so cheap and writing s/w is getting easier all the time (speaking as someone who hasn't written any real code since a few lines of FORTRAN in 1977 ...)

by signality
December 29, 2012

(sigh) ... sim problems. If I add the blocking cap it changes the sim. Remove it, change the last stage to your spec and it doesn't sim right. I really don't understand why the blocking cap changed it ... maybe I need a separate 2.5V source on the second amp?

Precision isn't very important. There's two of each circuit (ie, two mics creating two input values). If the resulting values are anywhere near each other then nothing will happen (by design). If there's more of a difference, a servo motor will turn the mic platform towards the louder value and try again.

I saw from your bio and linked in profile that you are a very accomplished engineer. Software is easier (certainly for me!) but the art does keep changing and I like that part ... if I am learning I am happy. I don't think your career is much longer than mine (I'm searching for a polite way to suggest we are similar ages).

I am starting to take an interest in video work (codecs and such) .... not sure if I have enough remaining gray matter to absorb it but it looks fun.

Back to the circuit, I am kind of attached to LM324's as I've already built part of the circuit and they're in place. But again, precision isn't important. What I can do is keep it from going too close to the rails, so I will go in that direction.

I still have to find the best way to get 5 or 9V from a 3.3V LIPO battery ... but I know there are dedicated components to do that. That shouldn't be as much of a challenge (as I rapidly attack the nearest source of wood).

Thanks again! I'll be hanging around. If you don't get that email let me know... -Kevin

by khauser
December 29, 2012

Quick additional note: I took the spice model and replicated it in CL: And that models similarly in both. There are a few differences between this model and the one I had ... you took care of the DC offset earlier so no need for differential amp. I found it interesting that you were able to share the 2.5V virtual ground through the fullwave rectifier stage and then used another separate resistor ladder (with 6.8K) for the peak detector. I assume the value of the resistors doesn't matter (as long as they are equal), but I'm not positive...

by khauser
December 30, 2012

Sorry for jumping in with stupid questions, but this is my fate and the biggest part of my job. So don’t get me wrong, I should study Laplace instead of talking nonsense here.

Is the double peak detector / Arduino ADC + compare really what you are looking for? Or is it the average (loudness) difference what you are looking for?

I don’t know what you exactly want, but thinking about a robocat turning it’s head into the direction of a human speaker I’m afraid the signal differences (left / right) would be extremely small (and depend extremely on the acoustical focus of the microphones). Electronic noise, drift in gain, phase, attack (not sure if this is the right word in English) / decay of peak detector, resolution and the timing difference of ADC measurements will add additional problems.

Why not compare left / right channels directly (OpAmp) and feed just the difference into the Arduino (and / or, but much better, directly to the motor controller …) ?

Again, I apologize, but I can’t stop it. Trash my posting if irrelevant.

;-)

Regards, Sancho

by Sancho_P
December 30, 2012

Hi Sancho, No problem with you jumping in! As a point of fact there have been several different attempts at "sound localization" within the Arduino world, including: 1) a phase difference comparison, done with a specific Arduino microcontroller via direct registry access. This didn't fit my need because the microcontroller used is far larger (physically) than the microcontroller I can fit in my project space.

2) A circuit that sounds extremely similar to what you have suggested, except the author used three microphones, comparing left-center difference and right-center difference to create two digital inputs to the Arduino. It would move only if one or the other digital outputs (inputs to the Arduino) were high.

The latter probably could be adopted to my use, but I began (before I even found CL) using two analog inputs and moving accordingly, and got surprisingly decent results.

Additionally, a different 'feature' of my end-result will be reflecting the ambient volume difference via the color of a full-color led.

I am using directional mic's and in fact will probably build something to increase that directional sensitivity.

Anyway, that's the approach I am working on for this go round. If it is insufficient I can always go again!

-Kevin

by khauser
December 30, 2012

Any advice for a simple peak detector for a multi stage preamp that uses either phantom power of +48V or dual 9V in series to 18V? The rails are split with a voltage divider for virtual ground.

working voltages are switched between (roughly) 30V and 18V. I am very limited in the ammount of current draw with phantom power. The circuit max is 10mA.

would the applications above indicate audio AC riding on the 0V virtual ground signal to indicate saturation?

by Archangel
July 22, 2013

Pretty much all the info you need is in this thread but can you draw a public or unlisted sketch to clarify your power supply arrangements?

by signality
July 22, 2013

Absolutely! And thanks greatly for the help. https://www.circuitlab.com/circuit/q3f8x6/ps-example/

by Archangel
July 22, 2013

V2 is the wrong polarity.

You have spaces in net labels.

You mention monitoring VG in the circuit. It is not labelled.

If you correct these minor errors then you should be able to run a Time Domain simulation on your circuit.

You may have to run it for a long time to let the DC levels stabilise as it is AC coupled to a mid supply DC level. If that is the case then use a start time > 0.

You may also have to use one of the startup options described here:

https://www.circuitlab.com/circuit/b24363/ramped-and-glitched-signals-and-supplies-01/

Search CL for:

start up

and:

initial conditions

for more.

You can add one of the peak detector circuits shown in this thread - or linked to from it - to your circuit. Pay special attention to the DC biasing and any gain you wrap around it.

Sanity check against the original examples posted in CL to ensure your implementation works as you intend it to!

:)

by signality
July 22, 2013

Thanks! I fixed the errors and will run some sims this evening.

by Archangel
July 22, 2013

Post a Reply

Please sign in or create an account to comment.

Go Ad-Free. Activate your CircuitLab membership. No more ads. Save unlimited circuits. Run unlimited simulations.

About CircuitLab

CircuitLab is an in-browser schematic capture and circuit simulation software tool to help you rapidly design and analyze analog and digital electronics systems.