Op-amp circuits

Nine op-amp circuits on ±12 V: photodiode, hysteresis, integrator, differentiator, precision rectifier, voltage and current sources, PID and logarithm.

A printed circuit board with two black eight-legged chips, resistors, capacitors, a photodiode and a transistor, and above it the thin lines of graphs: a triangle, a square wave and a curve slowly climbing

The Operational amplifier page covers the basics: the symbol, the follower, the inverting and non-inverting amplifiers and the limits of a real chip. Here are nine circuits that use the same two rules:

  1. the inputs are at the same voltage, while the amplifier has negative feedback;
  2. no current flows into the inputs.

In nearly all of them the IN+ input is on GND, so IN− is a virtual ground: a point at 0 V where currents add up. You work out each circuit by following those currents.

All the circuits run on ±12 V, like the lab power supply, with an LM358 amplifier. Its output can be from −12 V to +10.5 V. If a circuit has to work with an Arduino, its output mustn’t go straight to a pin: an Arduino handles only voltages from 0 to 5 V (see the warning on the Operational amplifier page).

The page is for everyone aged 12 and up who already knows the basics from the first page. Each circuit has a schematic, how it works, what each part does and, where it shows something, a calculated graph. At the end of each circuit is what to watch for when you build it.

Why is it called operational?

The first op-amps were parts of analogue computers, before digital ones existed. Each amplifier did one mathematical operation: adding, multiplying by a fixed number, integrating. Connected together, they solved equations, for example how a rocket flies or how a bridge sways. The numbers were voltages, and the result was read on an oscilloscope. Several circuits on this page are exactly such operations.

Current to voltage

Schematic: photodiode D1, lit, with its anode on GND and its cathode on the IN− input of amplifier U1 (LM358). A current arrow I flows through the diode towards GND. R1, 100 kΩ, and C1, 10 pF, are between IN− and the output. IN+ is on GND, and the output voltage is U2

A photodiode gives a current in proportion to the light, a few microamps (see Infrared sensors). This circuit turns it into a voltage. The diode’s cathode is on the virtual ground, so the voltage across the diode is always 0 V. The light’s current flows from the node through the diode into GND. It can’t come from the amplifier’s input, so the output has to supply it, through R1:

\[ U_2 = I \cdot R_1 \]
With R1 at 100 kΩ, a current of 10 µA gives 1 V, and 50 µA gives 5 V.

  • R1 sets how many volts one microamp gives. For very weak light, 1 or 10 MΩ is used.
  • C1 (10 pF) calms the amplifier. Without it the diode’s capacitance and R1 could make the amplifier oscillate. Together with R1 it slows the circuit down, so it follows changes in the light up to about 160 kHz.
  • The photodiode stays at 0 V, so its capacitance doesn’t charge and discharge. That makes this circuit faster and more exact than a resistor connected straight to the diode, as in the lesson Detecting the ball.

What to watch for: the amplifier’s input current adds to the light’s current. The LM358 has about 20 nA, which is already 2 % of 1 µA. For very small currents, use an amplifier with FET transistors at its inputs (the TL072), which has less than a nanoamp.

Comparator with hysteresis

Schematic: the input voltage U1 goes to the IN− input of amplifier U1 (LM358). R1, 100 kΩ, leads from the output back to IN+, and R2, 10 kΩ, is between IN+ and GND. The output voltage is U2

An ordinary comparator changes its output as soon as the input crosses the level. When the input is noisy, it flips up and down many times around the level. This circuit has two levels: while the output is high the level is higher, and while it’s low the level is lower. This is called hysteresis.

R1 and R2 feed part of the output back to IN+. This is positive feedback: when the output jumps, it moves the level so that it pushes the output even further the same way. The level is the output voltage times R2 / (R1 + R2) = 1/11:

  • while the output is at +10.5 V, the level is at +0.95 V;
  • while the output is at −12 V, the level is at −1.09 V.

The input is on IN−, so the output is inverted: high when the input is low. The output drops only when the input passes +0.95 V, and goes back up only when it falls below −1.09 V. There are 2 V between the levels.

Three graphs over 2000 ms. Top: the input U1 is a slow sine wave from −3 to 3 V with noise, and two dashed lines are the levels at +0.95 and −1.09 V. Middle, red cross: the output of a comparator with one level at 0 V jumps up and down many times as the input passes through zero. Bottom, green tick: the output with hysteresis changes only once, from +10.5 to −12 V

In the graph the input has ±0.6 V of noise. A comparator with one level at 0 V changes its output 73 times, and one with hysteresis only once, because the gap between the levels is larger than the noise.

We did the same in the program for the photoresistor, with two levels in the code, and the Hall sensor has hysteresis in its chip. The pinball machine’s break-beams work this way too, with an LM339 comparator.

What to watch for: the hysteresis has to be larger than the largest noise, not the average noise. The levels can be moved away from zero: connect R2 to a fixed voltage from a divider instead of GND.

Integrator

Schematic: the input voltage U1 goes through R1, 10 kΩ, to the IN− input of amplifier U1 (LM358). C1, 100 nF, and R2, 1 MΩ, are connected side by side between IN− and the output. IN+ is on GND, and the output voltage is U2

The current U1 / R1 flows into the virtual ground and on into capacitor C1. While the current flows, the voltage on the capacitor keeps rising, so the output keeps falling. How fast the output changes is in proportion to the input:

\[ \frac{\Delta U_2}{\Delta t} = -\frac{U_1}{R_1 C_1} \]
With R1C1 = 1 ms, an input of 1 V changes the output by 1 V every millisecond. The output adds up the input over time. In maths that’s called an integral, so the circuit is called an integrator.

Graph over 20 ms: the input U1 is a square wave from −1 to 1 V with a 4 ms period. The output U2 is a triangle from −1 to 1 V: it falls while the input is 1 V and rises while it is −1 V. The orange triangle, the same input with 50 mV added, is lower with each tooth and after 20 ms has dropped by about 0.9 V

A ±1 V square wave gives a triangle: for 2 ms the input is +1 V and the output falls by 2 V, then for 2 ms the input is −1 V and the output rises by 2 V. The orange curve shows the integrator’s weakness. If the input has just 50 mV of extra DC voltage, the integrator adds that up too, so the triangle goes lower with each tooth. Without R2 the output would reach −12 V after a few seconds and stay there.

  • R1 and C1 set how fast it integrates.
  • R2 (1 MΩ) slowly discharges the capacitor. It doesn’t matter for fast signals, but for a DC voltage it limits the gain to R2 / R1 = 100. Those 50 mV then move the output by at most 5 V, not all the way to the supply.
  • C1 has to be a good film capacitor (polypropylene), not a ceramic or electrolytic one, because their capacitance changes with the voltage and current leaks through them.

What to watch for: the amplifier’s offset (up to 7 mV for the LM358) is integrated just like the signal. That’s why a real integrator has either a resistor like R2, or a switch across C1 that empties it before each measurement.

Differentiator

Schematic: the input voltage U1 goes through R1, 1 kΩ, and C1, 100 nF, one after the other, to the IN− input of amplifier U1 (LM358). R2, 10 kΩ, is between IN− and the output. IN+ is on GND, and the output voltage is U2

A differentiator is an integrator the other way round: the capacitor is at the input and the resistor in the feedback. Current flows through a capacitor only while its voltage is changing, and the faster the change, the larger the current. That current flows through R2, so the output shows how fast the input is changing:

\[ U_2 = -R_2 C_1 \cdot \frac{\Delta U_1}{\Delta t} \]
With R2C1 = 1 ms, an input rising by 1 V in a millisecond gives −1 V.

Graph over 12 ms: the input U1 is a triangle from −1 to 1 V with a 4 ms period. The output U2 is a square wave: −1 V while the input rises and +1 V while it falls, with rounded edges

The triangle rises and falls by 1 V in a millisecond, so the output jumps between −1 and +1 V. The edges are rounded because of R1.

  • C1 and R2 set how many volts at the output a rate of 1 V per millisecond gives.
  • R1 (1 kΩ) isn’t in the basic circuit, but without it the differentiator doesn’t work well. The faster the change, the higher the gain, so without R1 high-frequency noise would be amplified so much that it would bury the signal, and the amplifier would easily start to oscillate. R1 limits the gain to R2 / R1 = 10 and rounds the edges by about 0.1 ms.

What to watch for: a differentiator amplifies noise more than the signal. If the input is noisy, the result is often useless. That’s why it’s rarely used on its own, and more often as part of a PID controller.

Precision rectifier

Schematic with two amplifiers, U1A and U1B (LM358). The input U1 goes through R1, 10 kΩ, to the IN− input of U1A, and directly through R3, 20 kΩ, to the IN− input of U1B. Around U1A are diode D2 from the output to IN−, and diode D1 from the point after R2, 10 kΩ, to the output. That point goes through R4, 10 kΩ, to the IN− input of U1B. In U1B's feedback are R5, 20 kΩ, and C1, 1 µF, side by side. The output voltage is U2

An ordinary diode conducts only above about 0.7 V. For mains voltage that doesn’t matter, but an ordinary diode doesn’t let a 0.5 V signal through at all (see Rectifiers). In a precision rectifier the diodes are in the amplifier’s feedback, so the amplifier itself makes up their 0.7 V.

U1A is an inverting amplifier that passes only one half:

  • while U1 is positive, U1A’s output is negative, D1 conducts, and the point after R2 is at −U1;
  • while U1 is negative, U1A’s output would be positive. Then D2 conducts and holds it at about +0.6 V, D1 doesn’t conduct, and the point after R2 is at 0 V. D2 keeps the amplifier from saturating, so it recovers faster.

U1B adds the input through R3 (20 kΩ) and that half through R4 (10 kΩ), which therefore counts double:

  • while U1 is positive: −(U1 − 2 U1) = U1;
  • while it’s negative: −(U1 + 0) = −U1, which is positive.

The output is always positive: the absolute value of the input, |U1|, with no 0.7 V lost. C1 with R5 smooths the pulses into their average.

Graph over 100 ms: the input U1 is a sine wave from −2 to 2 V at 50 Hz. The light green curve is |U1|, halves of the sine from 0 to 2 V. The output U2 with the capacitor slowly rises and after about 60 ms swings between 1.18 and 1.33 V, around a dashed line at 1.27 V

A 2 V sine wave has an average absolute value of 1.27 V. The output gets there in about 60 ms and ripples around it by 145 mV, 100 times a second. A larger C1 makes the ripple smaller, but then the output follows changes in the input more slowly.

What to watch for:

  • The resistor ratios must be exact. If R3 and R4 don’t match, the two halves aren’t the same size.
  • For fast signals the LM358 is too slow: at every zero crossing U1A’s output has to jump across two diodes. For signals above a few kHz, use a faster amplifier.
  • Both amplifiers are in one LM358 chip, and its supply is ±12 V, as everywhere else. It isn’t drawn in the schematic.

Precision voltage source

Schematic: a 6.2 V Zener diode D1, cathode on GND, anode on a node that R1, 1 kΩ, feeds from −12 V. From that node R2, 6.2 kΩ, leads to the IN− input of amplifier U1 (LM358). A variable resistor R3, 10 kΩ, is between IN− and the output. IN+ is on GND, and the output voltage is U2

This circuit gives a steady voltage that you set with a variable resistor, from 0 to 10 V.

  • D1 is a 6.2 V Zener diode, which gets its current from −12 V through R1. Its anode sits at a steady −6.2 V.
  • R2, 6.2 kΩ, runs from that −6.2 V to the virtual ground. Exactly 6.2 V / 6.2 kΩ = 1 mA always flows through it, whatever the output does.
  • R3 is a 10 kΩ variable resistor. The same 1 mA has to flow through it, so the output is U2 = R3 · 1 mA: 0 V at 0 Ω, and 10 V at 10 kΩ. Each kilohm is one volt.
  • U1 gives all the current to the load. The Zener always has the same load, so its voltage stays steady.

The 6.2 V Zener isn’t a random choice: Zener diodes around 6 V change their voltage least when they warm up. 4.8 mA flows through D1.

What to watch for: a Zener diode is noisy, and the amplifier amplifies that noise R3 / R2 times. A 10 µF capacitor next to the diode reduces it. The variable resistor is the weakest part of the circuit: its wiper adds a dirty contact and noise. Today a voltage reference chip and a DAC are often used instead of a Zener and a variable resistor, as in the lab power supply.

Current source

Schematic of the Howland current source: U1 goes through R1, 1 kΩ, to the IN+ input of amplifier U1 (LM358), and R2, 1 kΩ, leads from the output to the same node. The load RL is between that node and GND, with a current arrow I through it. R3, 1 kΩ, is between IN− and GND, and R4, 1 kΩ, between IN− and the output

An LED, a battery charger or a resistance measurement needs a steady current, not a steady voltage. This circuit (the Howland current source) gives a current set by the input voltage, with the load connected to GND:

\[ I = \frac{U_1}{R_1} \]
With R1 = 1 kΩ and U1 = 1 V the current is 1 mA, whether RL is 100 Ω or 5 kΩ.

How does it work? R3 and R4 hold IN− at half the output, and by the first rule IN+, which is on the load, must also be at half the output. If the load has a higher resistance and its voltage rises, the amplifier raises its output just enough to add, through R2, the current R1 would lose. When all four resistors are equal, the current doesn’t depend on RL.

Graph of the current I against the load's resistance RL from 0 to 10 kΩ. Grey line: ideal, 1 mA. Green: 1 mA up to 5.25 kΩ, then falling to about 0.55 mA at 10 kΩ. Orange, with R4 1 % larger: slowly rising to 1.05 mA at 5 kΩ, then falling like the green one

The graph shows two limits:

  • Voltage: the amplifier’s output is twice the voltage on the load. The output can reach 10.5 V, so 1 mA flows only while RL is up to 5.25 kΩ. Above that the current falls.
  • Resistor accuracy: if R4 is just 1 % larger, the current rises with RL, to 1.05 mA at 5 kΩ. The source then has an output resistance of only about 100 kΩ (here even negative, so the current rises). For a good current source the resistors must be accurate to 0.1 % or better.

What to watch for: this circuit also has positive feedback, through R2. With a load that has capacitance or inductance (a cable, a coil) it can oscillate. The lab power supply holds a steady current another way: amplifier U5B measures the current and controls the regulator.

A PID controller in one amplifier

Schematic: the input voltage U1 goes to the IN− input of amplifier U1 (LM358) through R1, 10 kΩ, and, side by side with it, through R2, 1 kΩ, and C1, 1 µF, one after the other. In the feedback are R3, 100 kΩ, and C2, 1 µF, one after the other. IN+ is on GND, and the output voltage is U2

A controller holds something at a set value: a temperature, a motor’s speed, a servo motor’s position, a supply voltage. Its input is the error: how far the measured value is from the set one. A PID controller makes its output from the error in three parts:

  • P (proportional): the larger the error, the stronger;
  • I (integral): keeps growing as long as there is an error, so it removes even a small error that would otherwise remain;
  • D (derivative): reacts to how fast the error is changing, so the system doesn’t overshoot the target.

One amplifier does all three: in the feedback is R3 with C2, like an integrator that also has a resistor, and at the input is R1, with C1 beside it, like a differentiator:

  • P is R3 / R1 = 10.
  • I is set by R3C2 = 100 ms.
  • D is set by R1C1 = 10 ms.
  • R2 limits D, like R1 in the differentiator: at most R3 / R2 = 100 times the error.
Graph over 200 ms: the input U1 jumps from 0 to −0.1 V at 20 ms. The output U2 jumps to 10.5 V, falls to about 1.1 V within 2 ms, then slowly rises to 2.9 V. Thinner curves: P stays at 1.1 V, I rises evenly from 0, and D is a short spike to about 10 V that quickly disappears

The graph shows how the controller responds to an error that suddenly jumps from 0 to −0.1 V (the output is inverted, so a negative error gives a positive output):

  • D immediately gives a spike of about 10 V, which disappears within about a millisecond;
  • P stays at 1.1 V;
  • I rises by 10 V a second, as long as there is an error.

At its peak the sum would be 11 V, but the output stops at 10.5 V.

What to watch for:

  • While the output is saturated, C2 keeps charging. When the error goes away, the controller then overshoots the target for a long time. This is called integrator windup, and it’s prevented, for example, by two Zener diodes across C2.
  • D amplifies noise and kicks hard at every change of the set value.
  • These three parts affect each other, so this controller is tuned differently from one that adds them separately. Today a PID controller is most often written as a program, and the analogue one stays where it has to react very fast, as in power supplies.

Logarithmic amplifier

Schematic: the input voltage U1 goes through R1, 10 kΩ, to the IN− input of amplifier U1 (LM358). In the feedback is an NPN transistor Q1: collector on IN−, emitter on the output, and base on GND. IN+ is on GND, and the output voltage is U2

The voltage between a transistor’s base and emitter rises with the logarithm of the collector current: when the current grows 10 times, the voltage always rises by the same amount, about 60 mV (see Diode). In this circuit the current U1 / R1 flows into the virtual ground and on through the transistor, so the output is:

\[ U_2 = -U_{BE} \approx -0.06\ \text{V} \cdot \log_{10} \frac{U_1 / R_1}{I_S} \]
IS is a very small current that depends on the transistor and the temperature.

Graph of the output U2 against the input U1 from 1 mV to 10 V, on an axis in steps of ten times. At 25 °C (green) the output is a straight line from −0.42 V at 1 mV to −0.66 V at 10 V. At 50 °C (orange) the line is about 0.05 V higher

An input from 1 mV to 10 V, ten thousand times larger, is only −0.42 to −0.66 V at the output, a straight line on a logarithmic axis: 59 mV for every ten times. That way both very weak and very strong signals can be measured in one range, for example light from darkness to sunshine, or the loudness of sound in decibels.

What to watch for:

  • The input must be positive. For a negative input the transistor doesn’t conduct and the amplifier saturates.
  • The graph shows the biggest problem: when the transistor warms up from 25 to 50 °C, the output shifts by about 0.05 V, as if the input had changed almost seven times. Real logarithmic circuits have a second, identical transistor that cancels that change, or they measure the temperature.
  • A small capacitor usually has to go next to the transistor, or the circuit oscillates.

Note

The diagrams, graphs and values on this page are calculated from simplified models or are typical examples. They are here to explain, and may differ from the actual specifications of the parts. Before you build anything, always check the datasheets of the parts you use.

What did we learn?

  • All the circuits are worked out with the same two rules: the inputs are at the same voltage, and no current flows into the inputs.
  • A current-to-voltage converter holds a photodiode at 0 V and gives U = I · R.
  • Positive feedback gives a comparator two levels, so noise doesn’t flip its output.
  • A capacitor in the feedback makes an integrator, and at the input a differentiator.
  • A diode in an amplifier’s feedback loses no 0.7 V, so even small voltages can be rectified.
  • An amplifier can give a steady voltage, a steady current, PID control and a logarithm, but each circuit has its weakness: noise, resistor accuracy, saturation, temperature.

Quick quiz

1. A current-to-voltage converter has R1 = 1 MΩ. What is the output when the photodiode gives 2 µA?
U = I · R = 2 µA · 1 MΩ = 2 V.
2. A comparator with hysteresis has its levels at +0.95 V and −1.09 V. The input has ±1.5 V of noise. Will the output jump?
It will. There are only 2 V between the levels, and noise of ±1.5 V is 3 V from peak to peak. The hysteresis has to be larger than the noise, so you need a larger R2 or a smaller R1.
3. An integrator has R1 = 10 kΩ and C1 = 100 nF. The input is a steady 0.5 V. How much does the output change in 4 ms?
R1C1 = 1 ms, so the output changes by 0.5 V every millisecond. In 4 ms that’s −2 V (the output falls, because the integrator is inverting).
4. Why can a precision rectifier rectify a 0.2 V signal when an ordinary diode can't?
An ordinary diode conducts only above about 0.7 V. In a precision rectifier the diode is in the feedback, so the amplifier raises its output by those 0.7 V and makes up the voltage drop across the diode.
5. A current source gives 1 mA, and its output is twice the voltage on the load. What is the largest load resistance it can have, if the output goes up to 10.5 V?
The load can have at most 10.5 V / 2 = 5.25 V. At 1 mA that’s 5.25 kΩ.