Controlling MOSFET's resistance using negative feedback

I wanted to use LM317 in a power supply project but wanted to control voltage digitally or with voltage. One possible way was to change one of the voltage divider resistors on the schematic below, with some device used as voltage controlled resistor.

I though of using a MOSFET as voltage controlled resistor, however knowing that the region where it is linear and of suitable "resistance" is quite narrow I thought of using a feedback loop that would automatically control the gate voltage so to provide the resistance needed. I came up with the following configuration:

U2 acts like a buffer while U3 compares the voltage at the adjust pin node with the voltage set by V1, and outputs its own control voltage on the FET's gate. The simulation works perfectly. Here's output of the lm317 probed while sweeping V1 from 0 to 15 volts. Here's transient response when V1 is set to 10 volts. However, predictably it didn't go as smoothly irl. When I built the schematic on a breadboard the circuit oscillated quite a bit. It produced a triangular waveform with frequency of around 15 KHz with Vpp of around 5 volts with approximate offset voltage of the value set by V1.

I understand that there may be a ton of things causing a feedback circuit to oscillate however I'd like to know what can be done to reduce this effect or how to go about solving such a problem.

Even if I don't end up using this circuit in a power supply I still like the idea of controlling non-linear element with a feedback to behave exactly how you want them to.

Edit: The circuit works now! However, for reasons I don't completely understand. I removed the unity gain amp and I changed a MOSFET with a BJT (2n2222), and added a 10K resistor to its base. The circuit changed behavior a little bit but still oscillated significantly. I then tried to introduce a capacitor somewhere in the loop. The circuit worked perfectly (500mV ripple at 10 Ohm load) with the capacitor placed between base and collector of the BJT as shown here:

Now, I am happy that the circuit works, however I find it hard to find an explanation as to why? Can anyone provide some insight?

• For just controlling LM317 output voltage, you could leave out the resistors and feed your control voltage directly or through opamp buffer to the ADJ pin. The LM317 will keep output at 1.25 volts above this voltage. – jpa Jul 24 '18 at 19:29
• I actually tried that but the LM317 need to have a minimal current flowing from ADJ pin to operate correctly. That's why I decided to stay with some sort of voltage divider, to draw some current. – Anthropomorphous Dodecahedron Jul 24 '18 at 20:26
• Since you're willing to add one or more opamps and transitors, plus change, why not implement your own LDO with discretes, where you can control the reference voltage. Or use a more "tameable" JFET, not MOSFET. Also, unless you meant it, you pressed A in LTspice, that's why it's showing the text anchors. – a concerned citizen Jul 25 '18 at 19:21
• Hah! Thanks for tip, I was wondering what these dots were. Doing my own LDO was not something I thought about in the beginning however thinking back right now I might as well do it that way. I just have a bunch of LM317s and wanted to put them to good use. – Anthropomorphous Dodecahedron Jul 25 '18 at 20:46

Your feedback system is two op-amps in cascade followed by a MOSFET. The first op-amp is unity gain and the 2nd op-amp is used as a comparator. The MOSFET will have high voltage gain because the source is at 0 volts and Vgain = Rd/Rs so it's high!. Additionally the input capacitance of the MOSFET (about 1 nF) will likely turn the driving op-amp unstable too.

This will inevitably cause stability issues because it's quite easy to make an op-amp go unstable by making a complex feedback loop involving an extra comparator and a high gain transistor.

You need to lower the gain of the loop and this might be acheived by making the comparator into a non-inverting integrator. You might also try putting a resistor in the source of the MOSFET - as high a value as you can tolerate.

Unfortunately the LM358 (old school device) doesn't have a proper graph of its phase margin so it's tricky to calculate values but for the integrator 10 kohm and 1 nF mightd work so I'd be tempted to ditch the unity gain op-amp and feed "ADJ" straight into an inverting integrator.

• Thanks, haven't thought about MOSFET's gain. I'll try to fiddle with the integrator. – Anthropomorphous Dodecahedron Jul 24 '18 at 14:31
• It'll need to be simulated - I'm not 100% sure about the circuit but there are definitely examples on google images of using an integrator to drive a MOSFET to prevent oscillaton/instability. – Andy aka Jul 24 '18 at 14:33

The LM317 regulates using a high gain loop and a bandgap reference diode of 1.25v such that Vo-Vadj=1.25x.

The good news is your 10V step result is correct 11.25x V within tolerances.

The bad news is you gm or inverse RdsOn is too sensitive .

A better way is to use an NPN and Rb chosen to give a range of Ic to reduce the output to near 2.5V with some headroom on Op Amp Voh. This reduces the loop gain . Then use a fixed Radj to ground to limit the output voltage (opt.)max.

To get more phase margin a small C >Ciss eg 1nF in series with another 240 Ohm in parallel with feedback R improves phase lead margin stability.

Alternatively change Op Amp , U3 from a comparator to a lower gain linear inverting OpAmp by adding 2 resistors eg 10k:1M

Reason : the comparator design of an Op Amp is actually an integrator due to internal compensation in linear zone, reducing phase margin of 60deg (ideal LM317) by 90 deg or -30 deg forcing oscillation.

U2 is redundant.

To eliminate the 1.25V output offset , this must may left as is or corrected by design.

• Thanks I'll try changing the comparator to a low gain amp, and maybe try to use the NPN. I don't care about the offset much though. – Anthropomorphous Dodecahedron Jul 24 '18 at 14:28