Consider this circuit, which is a standard non-inverting amplifier with an amplification of A = 1+R1/R2.

Standard non-inverting amplifier with A=1+R1/R2

I now want to be able to change this amplification value dynamically, using a microcontroller pin. I came up with this solution, which basically modifies the value of the feedback resistor by inserting another resistor in parallel:

Non-inverting amplifier with changeable amplificaiton

I think that the new amplification (with the opto-isolator turned on) is

A = 1 + (R1||R3)/R2
  = 1 + (R1 R3)/(R2(R1+R3))

Would this solution actually work the way I intended? I'm especially worried that the phototransistor's saturation voltage may influence the op-amp in some way. If so, is there an alternative solution to this problem?

  • 1
    \$\begingroup\$ Interesting question and I'm curious for the answer myself. But you'll learn most by building the circuit and testing the result, then discuss the results in a question if you don't understand them or want to improve response. \$\endgroup\$ – jippie Oct 31 '13 at 6:37
  • 2
    \$\begingroup\$ Is there a particular reason you need to isolate the MCU from the opamp? I ask because my normal answer would be to use a digital pot, or a digital switch and some resistors, to achieve the same result. \$\endgroup\$ – markt Oct 31 '13 at 6:43
  • \$\begingroup\$ Take a look at this datasheet, there are some interesting applications in it. It is based on a optocoupler FET and the characteristics are more AC friendly than a bipolar type. Do you really need the isolation BTW, there may be other options too. \$\endgroup\$ – jippie Oct 31 '13 at 6:43
  • \$\begingroup\$ @markt: The µC is actually on another board, and the board with the op-amp only has a 24 V power supply. Also, I want the circuit to be a simple as possible, so it would be best to avoid having additional wires for power supply etc. But thanks for the suggestion anyway, maybe I'll bite the bullet and use your solution ;) \$\endgroup\$ – Geier Oct 31 '13 at 6:45
  • \$\begingroup\$ @jippie: See my answer to markt's comment. Isolation would be nice, but I'm definately curious for other solution. I would add the isolation someplace else, then. \$\endgroup\$ – Geier Oct 31 '13 at 6:47

Assumption: There is some need for optical isolation between gain control (uC output) and amplification module.

Here is a simplification of the approach in the question, that removes any transistors / FETs from the feedback path, and provides an analog (continuous) range of gains, while retaining the opto-isolation - Use an LDR optocoupler as used in some classic and DIY audio amplifiers:

LDR opto

For a one-off or DIY alternative, use a cheap and ubiquitous CdS light dependent resistor instead, coupled with a regular LED:


The schematic is thus:


simulate this circuit – Schematic created using CircuitLab

The gain controlling resistance is the parallel combination of R1 and (R2 + R_LDR).

By varying either the duty cycle of a PWM signal, or the voltage of a DAC output pin of the microcontroller, the LED's light intensity is varied. As this increases, the LED resistance drops, from a very high value (i.e. little effect on gain calculation) when the LED is off, to a low value when the LED is at nearly 100% duty cycle.

Note: If using PWM, the PWM frequency needs to be significantly higher than the frequency band of interest of the signal. Otherwise the PWM will couple into the signal path, as pointed out by @pjc50.

  • \$\begingroup\$ Wouldn't the PWM frequency couple into the output? \$\endgroup\$ – pjc50 Oct 31 '13 at 10:38
  • \$\begingroup\$ It would not matter unless PWM frequency is well within audio frequency. LDRs have a very slow response, 5 to 10 nS rise time is typical, so they would act as low-pass filters. \$\endgroup\$ – Anindo Ghosh Oct 31 '13 at 11:05
  • \$\begingroup\$ @pjc50 Actually, let me correct that: OP has not stated what frequency range the signal for amplification is in. Thus yes, if the PWM frequency were within or close to the desired band, and yet not high enough for the LDR's low pass response to kick in, then there would be coupling of the PWM into the signal path. \$\endgroup\$ – Anindo Ghosh Oct 31 '13 at 11:08

All provided answers are more or less workable, but have some disadvantages:

  1. All, but Anindo Ghosh answers will work with pretty low voltages only or have small regulation range (well or very high nonlinear distortions).

  2. The solution with the photo resistor will work, but resistor optocouplers are some kind of exotic elements.

  3. It is almost impossible to provide some exact gain and this gain will vary with the temperature.

So, such schematics are suitable only for AGC schematics where the second back feed will regulate the gain to the needed values.

If the exact and reliable gain have to be set, the only working method is to use MOSFETs controlled in switching mode (ON/OFF) and normal resistors:


simulate this circuit – Schematic created using CircuitLab

  • \$\begingroup\$ instead of discrete MOSFET you can use Quad analog CMOS Switch IC CD4066 \$\endgroup\$ – yogece Oct 31 '13 at 15:23
  • 1
    \$\begingroup\$ @yogece Yes, but it is not really necessary, because the switches have one end grounded. IMO, one can use some package of several low power MOSFETs. \$\endgroup\$ – johnfound Oct 31 '13 at 17:19
  • \$\begingroup\$ You're welcome. \$\endgroup\$ – markt Nov 1 '13 at 7:38

Why don't you use a gain control from an SPI bus from the MCU: -

enter image description here

There are other gain control chips that can be activated by hardware lines if you don't like SPI. I've used this device extensively and can vouch for its usefulness and accuracy.

The SPI stuff doesn't need to be high speed and can be isolated too if you really need it. I've run 2MHz SPI 10 metres with decent drivers but going at pretty slow speed won't be an issue.



simulate this circuit – Schematic created using CircuitLab

Assuming your op-amp signal ground and your MCU's ground are identical, this approach would work. If not, use an optocoupler to drive the MOSFET. You could also add multiple parallel MOSFET's (with seperate control lines) to get multiple gain options.

  • \$\begingroup\$ You swapped the op-amp's inputs ;). But other than that, that's an interesting approach. Does it have to be a MOSFET, or would a bipolar one work as well? \$\endgroup\$ – Geier Oct 31 '13 at 7:11
  • \$\begingroup\$ lol didn't even think about the inputs ;-) A MOSFET would be better, because it will present to the circuit (when active) as a small resistance to ground. I suspect a BJT would look like a current sink, i.e. it would actively drive the opamp feedback path and interfere with operation of the opamp. Worth a try on breadboard though. \$\endgroup\$ – markt Oct 31 '13 at 7:20
  • \$\begingroup\$ @pjc50: The way I see it, this solution does not depend on the FET input being a PWM. I don't want to use PWM anyway. \$\endgroup\$ – Geier Oct 31 '13 at 10:34
  • \$\begingroup\$ Oops, that comment was on the wrong answer! \$\endgroup\$ – pjc50 Oct 31 '13 at 10:38

I would say a better idea would be to use the optoisolator to control a CMOS switch, and use that to switch in the resistor. Putting a phototransistor in the loop like that may have strange results.


I'm answering my own question here, because I've taken up jippie's advice. I've built the circuit on a breadboard and performed the measurements.

  • Power supply: 5 V (7805)
  • Op-Amp: LM324
  • Opto-Isolator: SFH610A-3
  • R1: 21.7 k
  • R2: 9.83 k
  • R3: 21.8 k
  • Turned on the opto-isolator with a current of 7.7 mA

With these resistor values, the expected amplification is 2.11.

Here are the measurement results:

Vin     Vout measured   Vout Expected   Difference in %
0       0               0   
0.077   0.164           0.162           1.2
0.1     0.213           0.211           0.9
0.147   0.314           0.31            1.3
0.154   0.329           0.324           1.5
0.314   0.668           0.661           1.1
0.49    1.04            1.032           0.8
0.669   1.422           1.409           0.9
0.812   1.726           1.71            0.9
1       2.12            2.106           0.7
1.23    2.61            2.591           0.7
1.52    3.24            3.202           1.2
1.84    3.75            3.876           -3.3     |
2.1     3.75            4.423           -15.2    | (reached max output voltage)
2.54    3.75            5.35            -29.9    v


Additionally, I measured the voltage across R3 and the opto-transistor, allowing me to calculate a resistor value for the transistor. This fluctuated from 400 to 800 Ohm, most likely due to my multimeter having trouble measuring the small voltages. Compensating the expected amplification by adding 600 Ohm to R3, brings down the difference to 0.6 % max.

So my answer is: Yes, it'll work the way I expected, probably mostly due to the currents being so low that the transistor is used in a linear area. I wouldn't expect the same results if the resistors used had much less resistance.

Still, I changed my circuit to use the method suggested by markt and johnfound. Seems more correct.


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.