An operational amplifier, or op-amp, is a chip that amplifies the difference between two input voltages. With a couple of resistors it becomes an amplifier with a precise gain, a buffer, a filter or a comparator. Op-amps are behind most sensor circuits, audio gear and measurement tools.
The pins
- Non-inverting input (+) and inverting input (−).
- Output.
- Power pins, either a single supply (such as 0 V and 5 V) or a split supply (such as +12 V and −12 V).
On its own, an op-amp multiplies the difference between its inputs by a huge number, often 100,000 or more. Almost all useful circuits use negative feedback, connecting the output back to the inverting input through resistors, which sets a smaller, precise gain.
The two golden rules
With negative feedback, you can analyse most op-amp circuits with two simple rules:
- The output does whatever it takes to make the voltage at the two inputs the same.
- No current flows into the inputs.
Three classic circuits
Non-inverting amplifier
The signal goes into +, with a resistor Rf from output to − and another, Rg, from − to ground. The gain is:
Gain = 1 + Rf ÷ Rg
Inverting amplifier
The signal goes through Rin to the − input, + goes to ground (or a reference voltage), and Rf runs from output to −. The output is flipped:
Gain = −Rf ÷ Rin
Voltage follower (buffer)
Connect the output straight to −, and feed the signal into +. The gain is 1, but the output can drive much more current than the source. It's perfect for reading a high-resistance sensor or divider with an ADC.
def non_inverting_gain(rf, rg):
return 1 + rf / rg
def inverting_gain(rf, rin):
return -rf / rin
print(non_inverting_gain(9_000, 1_000)) # 10
print(inverting_gain(47_000, 10_000)) # -4.7
# A 0.2 V sensor signal into the x10 amplifier:
print(0.2 * non_inverting_gain(9_000, 1_000), "V")
10.0
-4.7
2.0 V
Comparators
Without feedback, the huge gain means the output swings fully high or low depending on which input is larger. That makes a simple comparator, such as "turn on when the temperature sensor voltage passes a set point". Dedicated comparator chips are faster and cleaner for this, and adding a little positive feedback (hysteresis) stops the output chattering when the input is near the threshold.
Choosing an op-amp
- Supply voltage: make sure it works at your supply. Many classic op-amps need well above 5 V.
- Rail-to-rail: older op-amps can't drive their output within a volt or two of the supply rails. For 3.3 V and 5 V circuits, choose a "rail-to-rail input and output" part.
- Bandwidth: enough for your signal at your gain. See the Going deeper section.
Going deeper
Gain-bandwidth product
An op-amp's gain falls as frequency rises. For most general-purpose op-amps, gain multiplied by bandwidth is roughly constant: the gain-bandwidth product (GBW). A 1 MHz GBW op-amp set to a gain of 10 has a bandwidth of about 100 kHz, and at a gain of 100 only about 10 kHz. For high gains, split the gain across two stages.
def bandwidth_hz(gbw_hz, closed_loop_gain):
return gbw_hz / abs(closed_loop_gain)
for gain in (1, 10, 100):
print(f"gain {gain:>3}: about {bandwidth_hz(1e6, gain)/1000:.0f} kHz")
gain 1: about 1000 kHz
gain 10: about 100 kHz
gain 100: about 10 kHz
For the non-inverting amplifier, the relevant figure is the "noise gain", 1 + Rf/Rg, which is also used for inverting stages.
Slew rate
The slew rate is the fastest the output can change, in volts per microsecond. A large, fast signal can hit this limit before the bandwidth limit, distorting sine waves into triangles. The highest undistorted frequency for a sine wave of peak amplitude V is about slew rate ÷ (2πV).
DC errors
- Input offset voltage acts like a small voltage added to the input, multiplied by the gain. A 2 mV offset at a gain of 100 shifts the output by 0.2 V. Precision ("zero-drift" or "chopper") op-amps reduce this to microvolts.
- Input bias current flows into the inputs in reality, creating errors across large resistors. CMOS and JFET-input op-amps have tiny bias currents, suitable for high-resistance sensors.
Single-supply circuits
On a single 3.3 V or 5 V supply, an AC signal like audio swings below 0 V, which the op-amp can't output. The fix is to bias the signal around mid-supply, often with a resistor divider and a capacitor that creates a "virtual ground", and to couple the signal in and out with capacitors.
Stability
Op-amps can oscillate when driving capacitive loads, such as long cables, or when stray capacitance sits at the inverting input. A small series resistor at the output, a small capacitor across the feedback resistor, and short wiring at the inputs usually fix this. Always decouple the supply pins with 100 nF capacitors close to the chip.
Sources
The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.