# Convert Single OPAMP PID to Software implementation

I have a number of analog PID implementations that I need to convert to software. In an typical example (given below), the summing junction is used to get setpoint-measured error. In decoding the P and I coefficients for software implementation, I am confused as to what input resistance should I consider, R1+R2 or R1 parallel with R2.

All help is welcome :)

simulate this circuit – Schematic created using CircuitLab

• The Vfdbk and Vref are opposite in sign, as they should be for difference to be achieved. Does that have any effect on how the PID calculation should act. Commented Jan 6, 2015 at 13:24

The "command" for this circuit is Vref/2, not Vref. The output is only zero when $$\frac{V_{ref}}{200K} = \frac{-V_{fdbk}}{100K},$$ meaning the output is zero when Vref/2=Vfdbk -- This of course is messing up attempts at analysis.

So, work out your circuit using Vref/2 as your input and 100K as its associated resistor. Then you know that the gain of the circuit with respect to (Vref/2+Vfdbk) is

$$\frac{-[R3 + 1/sC1]}{100K}.$$

Thus, using Vref/2 as your input, the P gain is R3/100K, and the I gain is 1/(100K*C1)

• Thanks for this insight. I particularly liked your explanation of why 100K should be used as the input weight. It is indeed true that in these implementations the Vref range is twice that of Vfdbk. This is done to make sure that "really" high feedback values can be accomodated in the control scheme. Commented Jan 7, 2015 at 12:27

I take it the resistor values (especially R5/R6? 200 ohms is an awfully heavy load on an op-amp) and maybe the op-amp number are just for illustration.

The 'P' gain is 0.1

The output slews at a rate of Vfb/(100K * 470nF) volts/second for the integral gain so the I time constant is 47ms.

There is no 'D' term.

The diodes and R5/R6 are to clamp the output asymmetrically (mainly to prevent integral windup). Presumably the output 0-100% requires an asymmetric swing like 0.5 to -7.5V or something like that. It's important in analog circuits to prevent integration from continuing once the output is saturated. It's infinitely more important in firmware implementations since there's almost no limit to how high the integration term could get (especially if you're using floating point). It's also important to limit it or at least initialize it. The controller might otherwise take longer than the age of the universe to recover if the integral term got set to something weird, which many customers might find unacceptable.

• Thanks for the answer. Also thanks for the advice regarding integral windup. The output of this circuit does require an asymmetric swing, from 0.5 to -7.5volts. That is because all the interesting action that the output can do is between these values :). You are also true that R5/R6 values here would cause a lot of load on the opamp. The values used here are only illustrative. Commented Jan 7, 2015 at 12:24

## My first thought:

Forget the analog circuit, pick one of many digital PID implementations, and re-tune from scratch.

You could accelerate the process by making educated guesses for the initial tuning parameters based on the system that it's controlling, but that's the basic idea. Start with "P" only, then add "I" and "D" while balancing stability and responsiveness.

## My second thought:

Simulate the analog circuit in realtime. When you first start the simulation, all voltages and currents are zero. Use:

• Kirchoff's voltage and current laws
• Ohm's law with complex impedances
• An ideal opamp model (output has infinite differential-mode gain and zero common-mode gain)
• All of the previous cycle's voltages and currents

to find the current cycle's voltages and currents.

This approach is probably not useful unless you have a very specialized application that takes advantage of a funky circuit to compensate for funky behavior. Even then, my third thought is probably easier.

## My third thought:

Take the analog controller out of its application, give it some test inputs, and measure its response. Then make a digital filter (doesn't have to be PID) that provides the same response.

## Common theme:

Analyzing the circuit for a first-shot, convert-and-run solution is well into the diminishing returns area. You can do it, but the break-even point between that and starting over is very soon. And you'll probably make a mistake somewhere anyway, which wipes out the first-shot part.

Given the specialized appearance of the analog circuit, I think your digital version will probably be specialized also, to some extent. That is, you'll:

• Start out with a more-or-less standard attempt at a solution
• Find that you can tune it very well if you exclude certain situations
• Modify it to handle those situations with less de-tuning.

Depending on how much rube-goldberg is between your actuator and its desired effect, you may even end up with multiple PID's, one feeding another, each corresponding to its own step in the rube-goldberg machine.

I am confused as to what input resistance should I consider, R1+R2 or R1 parallel with R2.

Input resistance for Vfdbk is R2 (100kohm) and for Vref it is R1 (200k). This op-amp configuration is a virtual earth type circuit and, for a very reasonable level of accuracy, the -Vin node of the op-amp can be considered to be at the same potential as the +Vin node. As the +Vin node is connected to 0V (via a 47k), the -Vin node will largely be at 0V.

This means the input impedances are the input resistor values.

BTW, the TL081 is a FET input amplifier and does not require the 47k on the +Vin input.

• Thanks for that insight. Does that mean, I can consider R1+R2 as the input impedance. If yes, then kp would be R3/(R1 + R2) and Ti would be C1*(R1 + R2). Does this sound OK? Commented Jan 6, 2015 at 13:25
• It does not sound right, because Kp = 0.033 and Ti = 141ms. Thats too loo a Kp. Kp should be somewhere in the range of 0.1 to 0.5. An autotuned PID for a known application, where this circuit has been used, put the value of Kp as 0.24, and Ti as 88ms. Commented Jan 6, 2015 at 13:29
• I am unable to reach close to those values...logically. I am looking for that logic !!! Commented Jan 6, 2015 at 13:29
• The input resistance is R1 for Vref as an input and R2 for Vfdbk as an input. These are two separate inputs. Commented Jan 6, 2015 at 13:40

Hmm, Well I'm not sure what's going on with R5,6 and the diodes... I'd have to think about it or maybe simulate it. Concerning your values. Kp is the gain.. or one over the gain.. or whatever.. Anyway this is also set by the rest of your circuit which is not shown. In practice it's hard to know exactly what the gain is sometimes.

Re: the time constant. Well to my eye that circuit has two different time constants.
For Vref the TC is ~200k * 470 nF ~ 94 ms and for the Vfeedback it's about 1/2 that. (I think R3 should be added to R1 or R2.. but that a 10% change at most.)

• That's an old analog hack, D1 will clip the positive excursion of the output, while D2 will clip the negative excursion of the output to a voltage given by the divider formed by R5 and R6. Commented Jan 6, 2015 at 15:10