## Opamp basics

Difficulty
A small introduction to opamps, greatly simplifying the workings of basic opamp circuits to get the grasp of how they work and how you can calculate your own values.
Estimated Time
a few minutes
Skills Required
Knowing Ohm's law!
If you've been working with robots for a while, you know you can never have enough sensors.
Each one has it's own pros and cons, so a combination can help your 'bot out of some sticky places.

The problem is, not all sensors give a simple voltage output you can just read with a microcontroller.
They need amplification.
So where do we start?

The best known amplifier in the electronics world is the opamp, or better yet, the operational amplifier.

It houses a bunch of transistors and resistors and through the great minds of some smart people they came up with a schematic symbol that looks like this :

First we'll take note of the two connectors in the center, top and bottom.
These are the power supply for the opamp, a positive voltage rail and a negative voltage rail.
Historically, opamps are usually rated for a range of +-15V. That plus and minus 15 volts, not circa 15V.

In some schematics, these connections are left off, and placed separately on the schematic to keep all the power supplies and decoupling / bypassing capacitors neatly in one spot, so when you see one without these rails, don't worry it's the same thing.

Next we have two input pins, a negative input terminal (Vn) and a positive input terminal (Vp), and offcourse an output terminal.
In it's basic form, an opamp will amplify the difference between the two input terminals and put this amplified difference on it's output.

Now, an opamp usually has an open-loop gain of several 100,000's; let's say for example 200,000.

Say the negative input terminal has a input voltage of 1.0002v, and the positive input terminal has an input voltage of 1.0003v.

That's a difference of 0.0001v, amplified by 200,000, gives us a neat and tidy 20V at the output.
That's right, 20V for a difference of 0.1 mV!!!!

Obviously that's not a good way to amplify something, since you'll be clipping hard into your power supply rail in no time at all. Given that we only power the device with +-15V, we can not go above this level and thus the output will be clamped to 15V.

Ok, so how can we use this thing?

First off, we'll simplify things a little bit. Let's say the opamp works in this way:

The output strives to keep the input lines at the same voltage.

Seems a little stupid to say, but let's see how we can put this to work given that one little line of information.

Take the following schematic, which is one of the most used circuits built around an opamp, an inverting amplifier:

Here you can see the power rails aren't shown, as I said before, this doesn't change the circuit at all, it's just left out to simplify things and keep things neat.

Now, we said the output strives to keep the input lines at the same voltage.
Here, the positive input is at ground, or zero volts. If we'll assume that the negative input is also at zero volts, that means the entire input voltage Vin falls across R1, so we can simply calculate the current running through this resistor:
I = U/R = Vin/R1.

Now, obviously the negative input isn't at ground level, as this would short anything you hook up to it. In fact, the inputs have a very high impedance, which means they're highly resistive so they do not load the input voltage too much.

So, if the input isn't at ground level, where's the current flowing to? It can only go through R2 to the output pin, which means the current running through R2 is the same as the one running through R1.

Ok, so now we can calculate the voltage over R2.
U = I*R = -Vin/R1*R2

Note the "-", this is because we are measuring the voltage across R2 with respect to the output pin, ie. the current is drawn into the output pin and thus the voltage across R2 appears negative.

Now we know the output voltage, and the input voltage. The amplification A, which is also referred to as the closed-loop gain, is Vout/Vin, so we can rearrange the calculation above:

A = Vout/Vin = -R2/R1

So now, we can build an amplifier with an amplification set by two resistors. Easy as pie!

Let's take an example; let R1 = 10k, R2 = 100k, Vin = 0,2v.
A = -100k / 10k = 10 * 0,2v so the output voltage is -2v.

Neat!

Let's try another circuit, one that doesn't invert the voltage we put at the input, the non-inverting amplifier:

Here you'll see, the positive input is not connected to ground, but straigth to an input voltage, while the negative input is connected to a voltage divider.

Well, as I said, the output likes to keep the inputs at the same voltage, so let's assume the negative input is at the same voltage as the positive input, Vn = Vp.
That means the voltage across R1 is equal to Vp, or rather the input voltage.
So the current through R1 is Vin/R1.

However, the input cannot provide any power, since it's an input, so the current must be coming from the output, through R2. That means the voltage across R2 is:
U = I*R = Vin/R1 * R2

Now, the output voltage with reference to ground is the sum of the voltages across both R1 and R2, or V1 + V2:

Vout = Vin/R1 + Vin/R1*R2

Rearranging once again to get the amplification A = Vout/Vin:

A = 1+R2/R1

Et voila! We have an amplifier that doesn't invert, however we're stuck with the fact that there's at least an amplification of 1.

See how there's a big difference between the open-loop and closed-loop gain?
That's because we're using negative feedback, meaning as the output voltage rises, so too does the voltage at the negative input, which means the output voltage drops, which means the voltage at the negative input drops, which ...

Now, I've greatly simplified things above, but this is the way I look at opamp schematics to try and understand how things work and how they tick.
In the real world, there's no such thing as a perfect opamp, so the inputs will draw a tiny amount of current, the output will have an output impedance which causes a voltage drop and thus a slightly lower voltage at the output, there's bandwidth issues etc etc...

The point is, electronics can be as hard as you want it to be, or as simple as you want it to be.
I know there's a lot of people out there who can't grasp the concept of the opamp because they're given the hard explanation right away, without even understanding what it's purpose is.

Here's an example for you; take the inverting amplifier schematic, draw it on a piece of paper, but replace R2 with a capacitor.
What does it do? What happens to the currents and voltages?
I'll give you a clue: the current through R1 still acts as if the negative input terminal is at ground.