Operational amplifier

The op-amp: the LM358, comparator, the two rules, follower, inverting and non-inverting amplifiers, real limits, and measuring current with an Arduino.

Two black chips on a breadboard, a few resistors, small capacitors and wires, and above them the yellow triangular amplifier symbol with its + and − inputs

An operational amplifier (op-amp for short) is a chip with two inputs and one output. It compares the voltages on its inputs and amplifies their difference about 100,000 times. On its own that isn’t much use, because even the smallest difference pushes the output all the way. But when part of the output is fed back to an input, with two or three resistors, the same chip becomes an exact amplifier, a follower, an adder or a current-to-voltage converter.

Op-amps are everywhere: in audio amplifiers, in measuring with sensors, in chargers and power supplies. Our lab power supply has three: one measures the current, and two hold the voltage and the current at the output.

This page is for the Arduino workshop and for everyone aged 12 and up. It covers the basics: the symbol, the two rules an op-amp works by, the basic circuits and the limits of a real chip. At the end there is an example with an Arduino. More complex circuits, such as the integrator, the precision rectifier and the PID controller, have a page of their own, Op-amp circuits.

The symbol and the chip

The op-amp symbol: a triangle with the inputs IN− and IN+ on the left, the output OUT at the triangle's tip on the right, and the supply V+ from above and V− from below

An op-amp is drawn as a triangle that points the way the signal goes, from left to right:

  • IN− is the inverting input: when its voltage rises, the output falls. It has a − sign next to it inside the triangle.
  • IN+ is the non-inverting input: when its voltage rises, so does the output. It has a + sign.
  • OUT is the output.
  • V+ and V− are the supply. Schematics often leave them out, but they always have to be connected.
The LM358 in a DIP-8 package seen from above, with a dot by pin 1: on the left 1 OUT A, 2 IN− A, 3 IN+ A, 4 V−, on the right 8 V+, 7 OUT B, 6 IN− B, 5 IN+ B

The one on our pages is most often the LM358. Its eight-pin package holds two identical amplifiers, A and B, with a shared supply. Look at the chip from above, with the notch or dot by pin 1 at the top left, and count the pins round the chip, anticlockwise. Many other dual op-amps have the same pinout, for example the TL072 and the MCP6002.

What’s inside

A simplified schematic of the inside between the V+ and V− lines, in three dashed boxes. 1: transistors Q1 and Q2 with a shared current source, IN− and IN+ on their bases. 2: transistor Q3 with capacitor Cc between its collector and base. 3: transistors Q4 and Q5 one above the other, with two diodes between their bases, and their emitters are the output OUT

An op-amp contains dozens of transistors, but they work in three parts:

  1. The input pair: two identical transistors, Q1 and Q2, share the current from one source. If the voltage on IN+ is a little higher than on IN−, Q2 takes a little more of the current and Q1 a little less. The pair senses only the difference between the inputs.
  2. The gain stage: transistor Q3 amplifies that small change many times. Capacitor Cc slows it down at high frequencies, so the amplifier doesn’t oscillate on its own. Because of it the amplifier has a limited speed, which comes up further down the page.
  3. The output: transistor Q4 pushes current from V+ into the output, and Q5 pulls it towards V−. The diodes between their bases keep both transistors slightly on, so the output moves smoothly from plus to minus.

Supply: ±12 V or 5 V

Many op-amps run on a dual supply: V+ on +12 V, V− on −12 V, with GND in the middle. The output can then be positive and negative, like a sound signal swinging around zero. A computer power supply (ATX) has one, and it powers our lab power supply: besides +12 V it also gives −12 V. All the circuits on this page, except the Arduino example, run on ±12 V.

With an Arduino you will more often run the amplifier on a single supply: V+ on 5 V and V− on GND. The output can then only be between 0 and 5 V. That needs an amplifier that also works with its inputs close to GND, as the section The output doesn’t reach the supply explains.

The output can never go outside the supply. An amplifier on ±12 V can’t give 15 V, so when the sum says 15 V, the output stops a little below +12 V. This is called saturation.

Without feedback: a comparator

Schematic: a divider of R1, 10 kΩ, and R2, 2.2 kΩ, between +12 V and GND gives U2 = 2.16 V to the IN− input of amplifier U1, an LM358. The input voltage U1 goes to IN+, and the output voltage is U3. The amplifier has a +12 V and −12 V supply

With no connection at all between the output and an input, the amplifier works as a comparator. A difference of only 0.1 mV, multiplied by 100,000, is already 10 V, so the output nearly always sits at one end:

  • when U1 is higher than U2, the output is high, about +10.5 V for the LM358;
  • when U1 is lower than U2, the output is low, about −12 V.
Graph over 2000 ms: U1 is a sine wave from −5 to 5 V, and U2 a dashed line at 2.16 V. The output U3 is at −12 V and jumps to 10.5 V each time U1 rises above U2, and back to −12 V when U1 falls below U2

In the graph U1 slowly swings between −5 and 5 V. The output U3 jumps up each time U1 passes 2.16 V and drops when U1 falls below that level. That way a comparator turns any voltage into a yes or no signal.

There are also chips made only for this job, called comparators, for example the LM339 with four comparators. They switch faster, and their output is a transistor that connects to GND, as in the Hall sensor. The pinball machine uses such a chip for its break-beams. If the input is noisy, a comparator flips up and down many times around the level. How two levels fix that is on the Op-amp circuits page.

Negative feedback and the two rules

If part of the output is fed back to the IN− input, the amplifier calms itself down. When the output rises too far, the voltage on IN− rises too, so the amplifier brings the output down. When it falls too low, the amplifier raises it. The output quickly settles at exactly one voltage. This is called negative feedback.

While the amplifier has negative feedback and the output isn’t saturated, two simple rules hold:

  1. The inputs are at the same voltage. The amplifier changes its output until the voltage on IN− equals the voltage on IN+. Only a few microvolts of difference remain.
  2. No current flows into the inputs. The input current is so small (about 20 nA for the LM358, and less than one picoampere for some amplifiers) that we leave it out of the sums.

With just these two rules and Ohm’s law you can work out every circuit on this page.

The voltage follower

Two schematics. Left: a divider of two 100 kΩ resistors between +12 V and GND, with the load RL straight on its middle. Right: the same divider on the IN+ input of amplifier U1 (LM358), whose output is connected to its own IN− and to the load RL. U1 is the voltage at the divider's middle, and U2 the voltage on the load

The simplest circuit is the voltage follower: the output is connected straight to IN−, and the signal goes to IN+. By the first rule IN− is at the same voltage as IN+, and IN− is the output. So the output equals the input: U2 = U1.

What is an amplifier that amplifies nothing good for? By the second rule it takes no current from its input, and its output gives the load as much current as it needs. In the left schematic a voltage divider of two 100 kΩ resistors gives 6 V only while nothing is connected to it. The load RL takes current and the voltage drops. In the right schematic the divider sees only the amplifier’s input, so it stays at 6 V, and the load gets its current from the amplifier’s output.

Graph of the voltage U2 on the load against its resistance RL from 100 Ω to 10 MΩ. Grey curve, the load straight on the divider: about 0 V below 1 kΩ, 1 V at 10 kΩ, 4 V at 100 kΩ, and close to 6 V only above 1 MΩ. Green curve, through the follower: 6 V from about 150 Ω up, and 4 V at 100 Ω

The graph shows the calculation. Straight on the divider, a 10 kΩ load gets only 1 V instead of 6 V. Through the follower it gets 6 V, until it needs more current than the amplifier can give. The LM358 gives about 40 mA, so below 150 Ω even the follower can’t hold 6 V.

A follower is also called a buffer: it separates a delicate source from its load. That is how you measure, for example, the voltage on a photoresistor or the output of a high-resistance sensor.

The non-inverting amplifier

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

If only part of the output goes back to IN−, through a divider of R2 and R1, the amplifier has to raise its output until that part equals the input. IN− gets U2 · R1 / (R1 + R2), and that must equal U1. That gives the gain:

\[ U_2 = U_1 \cdot \left(1 + \frac{R_2}{R_1}\right) \]
With R2 = 22 kΩ and R1 = 10 kΩ the gain is 1 + 2.2 = 3.2. The output is in phase with the input: when the input rises, the output rises too. That’s why it’s called non-inverting. Here too the input takes no current, so the amplifier doesn’t load the source. The gain can’t be less than 1: with R2 = 0 it becomes a follower.

The inverting amplifier and the virtual ground

Schematic: the input voltage U1 goes through R1, 10 kΩ, to the IN− input of amplifier U1 (LM358), and R2, 22 kΩ, is between IN− and the output. IN+ is on GND. The IN− node is marked 0 V. Current arrows I flow through R1 towards the node and on through R2 towards the output. The output voltage is U2

Here IN+ is connected to GND, so by the first rule IN− is also at 0 V, even though it isn’t connected to GND. Such a point is called a virtual ground. So the current through R1 is I = U1 / R1. It can’t go into the amplifier’s input (the second rule), so all of it flows on through R2. For the same current to flow through R2 from 0 V towards the output, the output must be negative:

\[ U_2 = -U_1 \cdot \frac{R_2}{R_1} \]
With the same resistors the gain is −2.2. The minus means the output is inverted: when the input rises, the output falls. The gain can also be less than 1, for example −0.5 with R2 half of R1. Unlike the non-inverting amplifier, this one takes current from the source: the source sees the resistance R1.

Graph over 2000 microseconds: the input U1 is a sine wave from −2 to 2 V. The non-inverting amplifier's output (gain 3.2) swings from −6.4 to 6.4 V in phase, and the inverting amplifier's output (gain −2.2) from −4.4 to 4.4 V, inverted

The graph shows both amplifiers with the same input: a 2 V sine wave. The non-inverting one gives 6.4 V in phase, and the inverting one 4.4 V, inverted. If the non-inverting amplifier’s input were larger than 3.28 V, the output would reach its limit of 10.5 V and the peaks would be cut off.

The summing amplifier and the difference amplifier

Schematic: three input voltages U1, U2 and U3 go through R1, R2 and R3, 10 kΩ each, to the shared IN− node of amplifier U1 (LM358). R4, 10 kΩ, is between IN− and the output U4. IN+ is on GND

The virtual ground also makes a summing amplifier possible. Each input sends a current into the same node through its own resistor, and all those currents together flow through R4. The inputs don’t mix with each other, because the node always stays at 0 V. When all the resistors are equal:

\[ U_4 = -(U_1 + U_2 + U_3) \]
That’s how a sound mixer works: each microphone has its own input, and with different resistors each input can have its own volume.

Schematic: U1 goes through R1, 10 kΩ, to IN−, and R3, 100 kΩ, is between IN− and the output U3. U2 goes through R2, 10 kΩ, to IN+, and R4, 100 kΩ, is between IN+ and GND. Amplifier U1 is an LM358

A difference amplifier amplifies the difference between two voltages and ignores what they have in common. When R3 / R1 equals R4 / R2:

\[ U_3 = \frac{R_3}{R_1} \cdot (U_2 - U_1) = 10 \cdot (U_2 - U_1) \]
If both inputs are at 5 V, the output is 0 V. If U2 is 0.1 V higher, the output is 1 V. That’s how the lab power supply measures current: amplifier U2 measures the small voltage on a 0.1 Ω resistor whose two ends are both at the output voltage. The resistor ratios must be very exact: there even 1 % of difference gives an error of several hundred milliamps, so the ratio is adjusted with a trimmer.

The limits of a real op-amp

The two rules describe an ideal amplifier. A real chip has limits, and it’s good to know them before you build a circuit.

The output doesn’t reach the supply

Graph of a follower's output U2 on a 5 V supply against the input U1 from 0 to 5 V. The MCP6002 (blue) follows the input almost to 5 V. The LM358 (green) follows it to 3.5 V and then stays at 3.5 V

The LM358’s output can get almost to V−, but only to about 1.5 V below V+. On ±12 V that’s a range from −12 V to +10.5 V. On 5 V it’s only from 0 to 3.5 V, and the LM358’s inputs also work only up to 3.5 V. The graph shows a follower on 5 V: the LM358 follows the input up to 3.5 V and then stays there.

Rail-to-rail amplifiers have their output and inputs right up to the supply. On 5 V, with a load of 10 kΩ or more, the MCP6002 gives at least 25 mV to 4.975 V, and it usually gets within 5 to 10 mV of the supply. With a 1 kΩ load it stays 0.15 to 0.2 V from the supply. For an Arduino on 5 V it’s the better choice. Rail-to-rail amplifiers mostly work only at low voltages, up to 5 or 6 V.

The offset

The two transistors at the input are never exactly the same. So the amplifier behaves as if there were a small extra voltage on one input, the offset voltage: up to 7 mV for the LM358, up to 4.5 mV for the MCP6002 (usually 1 to 2 mV). The amplifier amplifies it just like the signal. With a gain of 100 and an offset of 5 mV the output is 0.5 V even when there’s nothing at the input.

Some amplifiers, such as the 741, have pins for cancelling the offset with a trimmer, as in the lab power supply. With the others, measure it while there’s no signal and subtract it in the program, as in the Arduino example.

Gain and frequency

Graph of the gain against frequency from 1 Hz to 10 MHz, both axes in steps of ten times. Grey curve, the amplifier without feedback: 100,000 up to 10 Hz, then ten times lower for every ten times higher frequency, reaching 1 at 1 MHz. Green, gain 100: flat up to about 10 kHz, then following the grey one. Blue, gain 10: flat up to about 100 kHz

The gain of 100,000 holds only for very slow signals. Capacitor Cc in the amplifier makes it 10 times smaller each time the frequency rises 10 times. For the LM358 the gain falls to 1 at about 1 MHz (0.7 to 1.2 MHz, depending on the maker). That number is called the gain-bandwidth product (GBW), because the gain times the frequency always stays about 1 MHz:

  • with a gain of 10 the amplifier works up to about 100 kHz;
  • with a gain of 100 only up to about 10 kHz.

So for sound (up to 20 kHz) with a gain of 100 the LM358 is too slow. Then split the gain between two amplifiers one after the other, 10 each, or use a faster amplifier.

The slew rate

Graph over 200 microseconds: the input U1 is a sine wave from −10 to 10 V at 10 kHz. The follower's output U2 is a triangle from about −7.5 to 7.5 V, lagging behind the input, because the output can't change faster than 0.3 V per microsecond

The output can’t change as fast as it likes. For the LM358 it changes by at most 0.3 to 0.5 V in a microsecond, depending on the maker. This is the slew rate. A 10 V sine wave at 10 kHz rises by 0.63 V in a microsecond on its steepest part, faster than that. The graph shows what happens at 0.3 V in a microsecond: the follower’s output can’t keep up with the input, and instead of a sine wave you get a triangle of only 7.5 V. Such an LM358 follows a 10 V sine wave only up to about 4.8 kHz, and one with 0.5 V in a microsecond up to about 8 kHz.

A capacitive load

A long cable or a large capacitor straight on the output can make the amplifier oscillate on its own, even when everything else is connected correctly. If you have to drive such a load, put a resistor of 50 to 100 Ω between the output and the load.

Which op-amp?

ChipSupplyOutputGBWSlew rateWhat for
74110 to 36 V (±5 to ±18 V)about 2 V from the supply0.4 to 1.5 MHz0.5 V/µsthe old classic, has offset null
LM3583 to 32 V (±1.5 to ±16 V)to V−, 1.5 V from V+0.7 to 1.2 MHz0.3 to 0.5 V/µscheap, inputs work down to V−, two in a chip
TL07210 to 30 V (±5 to ±15 V)about 1.5 V from the supply3 MHz13 V/µssound, almost no input current
MCP60021.8 to 5.5 V (±0.9 to ±2.75 V)rail-to-rail1 MHz0.6 V/µsArduino, micro:bit, batteries
LM3392 to 36 V (±1 to ±18 V)transistor to GND––comparator, four in a chip

The supply is the range the chip may run on, and the other values are typical, from the makers’ datasheets. Newer versions of some chips (LM358B, TL072H) work over a wider range, and the TL072H is also faster: 5.25 MHz and 20 V/µs. The TL072 doesn’t work well with its input close to V−, so it isn’t for a single supply. The LM339 isn’t an op-amp, only a comparator: it has no capacitor Cc, so it mustn’t be used with feedback.

An op-amp and an Arduino: an LED’s current

An Arduino measures the voltage on an analogue input in 1024 steps of 4.9 mV each (see Digital and analogue). A small voltage takes up only a few steps, so the measurement is coarse. An amplifier can make it big enough to fill the whole range from 0 to 5 V.

The example measures the current of an LED. Under it is a small 10 Ω resistor, on which 13 mA gives only 0.13 V. The Arduino reads that voltage twice: directly on A1, and amplified 22.4 times on A0.

Schematic: D8 of an Arduino Uno powers a red LED D1, followed by R1, 220 Ω, and R2, 10 Ω, to GND. The point between R1 and R2 goes to A1 and to IN+ (pin 3) of amplifier U1, an MCP6002. R3, 2.2 kΩ, is between IN− (pin 2) and GND, and R4, 47 kΩ, between IN− and the output (pin 1), which goes to A0. Pin 8 is on 5V, with capacitor C1, 100 nF, to GND, and pin 4 on GND

Parts: an MCP6002 (or another rail-to-rail amplifier for 5 V in a DIP-8 package), a red LED, resistors of 220 Ω, 10 Ω, 2.2 kΩ and 47 kΩ, a 100 nF capacitor.

  • R2 (10 Ω) turns the current into a voltage: 13 mA gives 0.13 V. It’s so small that it hardly lowers the LED’s current.
  • U1, R3 and R4 are a non-inverting amplifier. The gain is 1 + 47 kΩ / 2.2 kΩ = 22.4, so 0.13 V becomes 2.8 V.
  • C1 sits next to pin 8 and catches interference on the amplifier’s supply.
  • The amplifier runs on the Arduino’s 5 V, so its output can’t go outside 0 to 5 V and can’t damage the pin.

Don't connect an amplifier on ±12 V straight to an Arduino

The output of an amplifier on ±12 V can be up to +10.5 V, or negative. Such a voltage on an Arduino's analogue input will damage the pin. With an Arduino, use an amplifier on 5 V, as in this example, or protect the output with a resistor and diodes to 5V and GND.

The program

The program first switches the LED off and measures what A0 and A1 show when there is no current. That is the offset (mostly the amplifier’s offset, amplified 22.4 times), which it later subtracts from every measurement. Then it switches the LED on and prints the current measured on both pins every second. Each measurement is the average of 64 readings, to calm the noise.

// C++
// An LED's current, measured twice:
// A0 through the amplifier (MCP6002, gain 22.4), A1 straight from R2, a 10 ohm resistor.

const int LED_PIN = 8;
const float R_SHUNT = 10.0;               // R2, in ohms
const float GAIN = 1 + 47.0 / 2.2;        // 1 + R4 / R3
const float STEP = 5.0 / 1024;            // one step of the converter, in volts

float offsetA0 = 0;   // what the pins show while the LED is off
float offsetA1 = 0;

// the average of 64 readings, to calm the noise
float average(int pin) {
  long sum = 0;
  for (int i = 0; i < 64; i++) {
    sum += analogRead(pin);
  }
  return sum / 64.0;
}

void setup() {
  Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT);

  // LED off: the current is 0, so whatever the pins show is the offset
  digitalWrite(LED_PIN, LOW);
  delay(100);
  offsetA0 = average(A0);
  offsetA1 = average(A1);
  Serial.print("Offset A0: ");
  Serial.print(offsetA0);
  Serial.print("  A1: ");
  Serial.println(offsetA1);

  digitalWrite(LED_PIN, HIGH);
}

void loop() {
  float nA0 = average(A0) - offsetA0;
  float nA1 = average(A1) - offsetA1;

  float currentA0 = nA0 * STEP / GAIN / R_SHUNT * 1000;   // in mA
  float currentA1 = nA1 * STEP / R_SHUNT * 1000;

  Serial.print("A0: ");
  Serial.print(nA0, 0);
  Serial.print(" steps, ");
  Serial.print(currentA0, 2);
  Serial.print(" mA    A1: ");
  Serial.print(nA1, 0);
  Serial.print(" steps, ");
  Serial.print(currentA1, 2);
  Serial.println(" mA");

  delay(1000);
}

In the Serial Monitor (9600 baud) both currents should be about 12 to 13 mA. The difference is in the steps:

  • on A1 a current of 12.6 mA takes up only 26 steps, and one step is 0.49 mA, so the current changes in jumps of half a milliamp;
  • on A0 the same current takes up 577 steps, and one step is 0.022 mA, more than twenty times finer.

The largest current A0 can show is 22 mA: the amplifier’s output is then at 4.97 V. With an LM358 it would stop at 15.7 mA, because its output on 5 V doesn’t go above 3.5 V. The MCP6002’s largest offset, 4.5 mV, is amplified to 0.1 V, which is 0.45 mA. That’s why the program measures it and subtracts it. If the offset is negative, the output stays at 0 V for small currents, so A0 can’t see the smallest currents.

The same amplifier can measure the current of a motor or a servo motor, with a 0.1 or 1 Ω resistor instead of 10 Ω. Such a resistor has to handle the power I² · R.

What next

The Op-amp circuits page has circuits that use the same two rules: a current-to-voltage converter for a photodiode, a comparator with two levels, an integrator and a differentiator, a precision rectifier, a precision voltage source and a current source, a PID controller in one amplifier, and a logarithmic amplifier.

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?

  • An op-amp amplifies the difference between the voltages on its inputs IN+ and IN− about 100,000 times. Without feedback it works as a comparator.
  • With negative feedback two rules hold: the inputs are at the same voltage, and no current flows into the inputs.
  • A follower gives the same voltage at its output as at its input, but takes no current from the source.
  • A non-inverting amplifier amplifies 1 + R2/R1 times, and an inverting one −R2/R1 times, around a virtual ground.
  • The output can’t go outside the supply, and the gain times the frequency stays about the GBW.
  • An Arduino on 5 V needs a rail-to-rail amplifier on 5 V, not an amplifier on ±12 V.

Quick quiz

1. IN+ is at 2.00 V and IN− at 2.01 V. The amplifier has no feedback and runs on ±12 V. Where is the output?
IN− has the higher voltage, so the output is low, about −12 V for an LM358. A difference of 10 mV times 100,000 is 1000 V, far above the supply, so the amplifier is saturated.
2. A non-inverting amplifier has R1 = 4.7 kΩ and R2 = 47 kΩ. What is the gain?
1 + 47 kΩ / 4.7 kΩ = 1 + 10 = 11. An input of 0.3 V gives 3.3 V.
3. Why is the IN− node of an inverting amplifier at 0 V, even though it isn't connected to GND?
Because of the first rule: IN+ is connected to GND, and the amplifier changes its output until IN− reaches the same voltage. That’s why the node is called a virtual ground.
4. How much can an amplifier with a GBW of 1 MHz amplify, if it has to work up to 20 kHz?
1 MHz / 20 kHz = 50 times. For a higher gain you need two amplifiers one after the other, or a faster amplifier.
5. Why isn't an LM358 on 5 V a better choice for the Arduino example?
The LM358’s output on 5 V doesn’t go above about 3.5 V, so A0 would show at most 15.7 mA. The MCP6002 is rail-to-rail and goes up to 4.97 V, so A0 shows up to 22 mA.