Some perhaps overly detailed background:

I have a Very Expensive Black Box™ piece of lab equipment that I can control via analog voltages in the range [0, 10] V.

It has two inputs that are relevant to this: a "DC" input, and an "AC" input. Both must be supplied voltages (which I call \$V_{DC}\$ and \$V_{AC}\$ respectively). They are effectively summed together to produce an input to the system that can vary over time (think \$V_{DC} + V_{AC}\sin(\omega t)\$), and they are additionally subject to the constraint that their sum must be 10 V or less.

At the moment, \$V_{AC}=0\$ and \$V_{DC}\$ is controlled by a separate microcontroller device. It is typically a few volts. I have the ability to spit out an additional arbitrary waveform in the range [-5, 5] VAC relatively easily through this second box, in a way that can play nicely with the UI on the rest of the equipment. This waveform is likely to be up to about 30 kHz and to a first approximation a sine wave is fine.

Ultimately, I would like to build a circuit that:

  1. takes \$V_{DC}\$ and \$V_{AC}\$ as inputs;
  2. computes the constant value \$V_{DC}-|V_{AC}|\$ and sets its output, \$V_{DC}'\$ to it (i.e. \$V_{DC} = V_{DC}-\max(V_{AC})\$);
  3. level-shifts the [-5, 5] \$V_{AC}\$ signal to be [0, 10] V, and scales it so that its minimum is set to zero (rather than 5 V, say) and provides this as its AC output, \$V_{AC}'\$.

That is quite a lot of complexity for a stack overflow post (!) but I have been trying to attack this problem as a set of chained-sub-problems: shift \$V_{AC}\$, obtain its envelop maximum, and then use that value (call it \$A\$) to either subtract from \$V_{DC}\$ or further shift the now-shifted waveform.

The problem

Here's how I've done this part so far – with two op-amps, a classic level shifter, and an envelope detector proposed previously on this site:

The first half

And, so far, it works great! Yay. You can see that the envelope is detected (with the addition of +5 V), and the levels are shifted compared to the original input:


Now, I'd like to subtract away the 5 V bias, and ultimately \$V_{DC}\$. This seems like a classic option for a subtracting op-amp, right? Just have a potential divider to split my 10 V supply in half, and provide these as inputs into a subtracting op amp, and produce a number that should be \$V_{sub}=6.5-5=1.5\$ volts with the example numbers I've chosen here:


Yet, it doesn't work: \$V_{sub}\$ is steadfastly zero (no matter what op-amp I chose or how long I wait) despite the inputs remaining at the right levels of 5 V and 6.5 V (±a little bit):

enter image description here

Why is this the case? What have I done wrong? Is it the case that effectively the output impedance of the envelope detector stage is too low – if I put a large resistor in series with it, the voltage reached by the last op-amp is no longer zero.

I was planning to carry on chaining stages like this to ultimately produce the other "features" remaining to be implemented in this circuit, but if anyone has a better idea, I would be very welcome to hear it.

  • \$\begingroup\$ Instead of randomly trying out opamps, did you pick one with an output range that you require? \$\endgroup\$
    – DKNguyen
    Jul 22 at 21:39
  • \$\begingroup\$ Even on the positive side, how close can this open amp get to zero, both on the inputs and outputs? \$\endgroup\$ Jul 22 at 21:42
  • \$\begingroup\$ @ScottSeidman – I believe the AD8032 is rated as being rail to rail up to 12V and can work single sided too? \$\endgroup\$
    – Landak
    Jul 22 at 21:47
  • 4
    \$\begingroup\$ You have the inputs to your 'subtractor' U2 wired the wrong way around. The R10/R11 divider should be connected to the inverting input and your signal 'A' to the non-inverting input. If you gave your U2 a negative supply (instead of ground) you'd see it trying to swing down to about -1.5V. \$\endgroup\$
    – brhans
    Jul 22 at 22:29
  • \$\begingroup\$ I separate my answers to two questions. why did it fail and (my question) why is the design spec missing or wrong and exceeding the device max differential limits (do not exceed) Any questions? \$\endgroup\$ Jul 23 at 1:38

4 Answers 4


Here is a way to do it. As you can see, it subtracts 5.00V from the 6.5V-biased 1.0V signal.

I would not use 100K resistors with the AD8031- the bias current is quite heavy, as much as 2uA, and it's a relatively high frequency op-amp (80MHz) so prone to oscillation or reduced phase margin with high-value feedback resistors.

enter image description here

It might be better to use a 5.0V reference rather than resistors from the power supply, but I'll let you work out your noise and error budgets.


subtractor is not subtracting is the question, why not?

Rail-to-rail input and output
No phase reversal with input 0.5 V beyond supplies

Failure analysis:

With subtractor inverting input A higher than non-inverting input , the output MUST saturate at 0V. This means you have positive feedback instead of negative in miswiring.

You should have a 2.5V reference on Vin+ not 5V.

Design Goal:

Input: 5V +/-2.5V
Output: 0.0 to 5.0V

both I/O are 5Vpp so gain =1 but offset = -2.5V

You have a choice:
Differential Amp If you solve for the Differential Amp you need

Since |Av-|=1, Av+=10 * 3/8 = 3.75V for Av+in (not 5V) thus 3.75*(1+1)= 7.5 - 5 (Av-)= 2.5V.

Proof of concept

Check ! =7.5V-5V = 2.5V subtractor but that inverts the peak level to 0V output so that no good.

You must use the non-inverting input for A.


With your difference amplifier as it is, you are adding 5V, not subtracting. You can easily tell which one is being subtracted; it's the signal feeding the op-amp's inverting ('−') input.

That also means you are inverting the sinusoid, since that's actually the signal being subtracted.

Another problem with your difference amplifier is that you think you are dividing 10V by two, but you're actually dividing by slightly more than that. That's because the two 100kΩ resistors R7 and R8 are a series pair (totalling 200kΩ) in parallel with the lower 1kΩ resistor R11. This is a classic mistake, and is technically described as a finite load (R7 + R8) on a non-zero impedance source (R10 + R11). Don't worry, it's easy to fix.

Here's my suggestion:


simulate this circuit – Schematic created using CircuitLab

The output here is:

$$ V_{SUB} = V_A - \frac{1}{2}V_B $$

This is the simulated output, given inputs of \$V_A = 5+1.5sin(\omega t)\$ and \$V_B = 10V\$:

enter image description here

The general relationship (for any resistances R1, R2, R3 and R4) is:

$$ V_{SUB} = V_A \left(1+\frac{R_2}{R_1}\right)\left(\frac{R_4}{R_3+R_4}\right) - V_B\frac{R_2}{R_1} $$

To arrive at the values I chose in my schematic above, my thinking went like this:

I know I have to subtract a half of \$V_B\$, so I must set \$\frac{R_2}{R_1}=\frac{1}{2}\$. Start with \$R_2=100k\Omega\$ and \$R_1=200k\Omega\$

I do not want any gain or attenuation of the signal at A, so I need to choose values for R3 and R4 such that:

$$ \begin{aligned} \left(1+\frac{R_2}{R_1}\right)\left(\frac{R_4}{R_3+R_4}\right) &= 1 \\ \\ \frac{3}{2}\left(\frac{R_4}{R_3+R_4}\right) &= 1 \\ \\ \frac{R_3+R_4}{R_4} &= \frac{3}{2} \\ \\ 1+\frac{R_3}{R_4} &= \frac{3}{2} \\ \\ \frac{R_3}{R_4} &= \frac{3}{2}-1 \\ \\ \frac{R_3}{R_4} &= \frac{1}{2} \\ \\ \end{aligned} $$

I'll settle with \$R_3=100k\Omega\$ and \$R_4=200k\Omega\$.

You'll notice that this alleviates the need for a potential divider (your R10 and R11) to divide the 10V by 2. This design accounts for that requirement as-is, and was established when we decided on values for R1 and R2.

That's all fine if you only ever want to subtract half of something, but I think the amount you wish to subtract will be the entirety of the output from your envelope detector, and the above static "dividing by 2" solution won't work for you.

Actually, you probably don't want to multiply B by anything, or A by anything. Rather, the goal is just to subtract, no scaling necessary. That's dead easy:


simulate this circuit

All resistances are equal, \$R_1=R_2=R_3=R_4=100k\$. Plugging those values into the above equation gives us this relationship between \$V_{SUB}\$, \$V_A\$ and \$V_B\$:

$$ V_{SUB} = V_A - V_B $$

One word of warning - in the graph above, the op-amp is able to output negative potentials, but that will only be possible if the op-amp's negative supply is below zero. The output can't fall below whatever lower potential you supply to the op-amp.


why does this op-amp subtractor not subtract?

Was the question, but why level shift twice?
But you have a bigger problem with > 1.5Vpk input.

Rail-to-rail input and output
No phase reversal with input 0.5 V beyond supplies
Input CMVR extends beyond rails by 200 mV
Output swing to within 20 mV of either rail (datasheet) enter image description here

Corrective actions

  1. protect the differential in the peak and hold detector from exceeding ABS MAX. +/-3.4V

    • instead. divide input by 2 then buffer with x2 2nd stage so as to reduce input.
  2. the series diode +R is redundant, remove the diode and use large RC=T decay time as desired.

  3. you do not need to level shift input if you can use a diode clamp, since you are only using a half-wave peak detector. thus all you need is a precision peak detector without a level shifter that can handle a >10V input swing.

There are other designs that work better.

enter image description here

But you start with good specs 1st.

  1. Input = +/-5V max, frequency range = ___
  2. Output = 50 mV (0) to 5V linear.
  3. Rise time < 20 ms (?)
  4. Decay time 0.5 to 1 s (?) (T=90% reduction)
  5. Differential input 10Vpp max
  6. Single supply 5 to 12V. <5% ripple
  7. peak error < 1%
  8. room temp
  9. Vcc current < 20 mA. on charge, < 1mA on idle or don't care. (?)

This one has better input max diff specs enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.