Data in electronics can be generally divided into two categories: digital and analog. With digital having two electrical states, and analog having infinite states. And it is quite often that we have to run mathematical operations in our circuits. For digital, this is quite easy as we can use a microcontroller or a full adder to accomplish such tasks. However, analog is a bit more tricky to get setup, and that is because of the famous operational amplifier, also known simply as an op-amp. How can we use such a component to calculate operations for us? Well in this video, I will show you how to not only use an op-amp, but also how to build one for yourself. Let’s make it.
Before we actually make the op-amp, let me describe basic op-amp knowledge and behavior. The typical op-amp has 5 pins, two are the positive and negative voltage supplies, and the others are the output, and the inverting and non-inverting inputs. There are a few rules about the op-amp that you should know so that you can start understanding op-amp circuits. First, keep in mind that the op-amp really wants to keep its two inputs of equal voltage, and will drive the output however necessary to achieve that. The other rule to keep in mind is that the inputs are very high impedance, so ideally, they do not affect your circuit at all, allowing you to use weak signals on the inputs. Now, these two rules are great, but we need some actual circuits to better understand how to use these op-amps. For all of these examples, I will be using the LM358 as my op-amp, but feel free to choose another op-amp if you want to.
If you want to follow along, you do not need a negative power supply. You can make a virtual ground by following this schematic with a potentiometer, an op-amp, a 1k resistor, a 1uF capacitor, and a transistor. You will understand what the op-amp here does in a second, but basically the circuit will generate an output that is half of the input, if you tune the potentiometer. So for example, if you supply 18 volts to this part of the circuit you will get 9 volts out. Remember that voltages are relative, so if you shift your way of viewing the voltages, you have +/- 9 volts. Remember, ground is what we say it is, the voltages just flow from high to low. Just make sure to keep all of your connections straight so that you don’t accidentally short circuit your virtual ground to the supply’s ground.
Let’s start with the classic comparator. The comparator is wired up in a configuration with no feedback from the output, meaning that no matter how the op-amp drives the output, it cannot affect the input. This means that it will go as high or as low as it possibly can, depending in the input that has the higher voltage. If the non-inverting input is higher, then the op-amp will output as close to the positive supply as possible, and if the inverting input is higher, then the op-amp will output as close to the negative supply as possible. The comparator is especially useful for analog to digital applications or electronic switches, however dedicated comparators often have better performance than op-amps. The LM393 is an example of such a dedicated comparator. However, it may be worth considering using an op-amp as a comparator if its performance is good enough for your circuit and you have an extra op-amp sitting unused on an IC.
Let’s get into some more complicated circuits that use feedback. The simplest circuit using feedback is the buffer configuration. Simply connect the signal you want to replicate to the non-inverting input and connect the inverting and output pins together. Now, the ouput will match the signal. Why would you want to do this? Well in some cases, the input signal is weak, and it is not capable of driving a load by itself, so you would attach it to this buffer so that the op-amp will drive it according to the input. However, in some cases even the op-amp cannot drive a load, so you can add an external NPN transistor and wire the feedback a little differently. Now the op-amp will drive the transistor however necessary so as to match the input signal, with a lot more power capability. And if you remember, this is the same way that we split the power supply at the beginning of the video.
There is another type of buffer that we can make, and that is the inverting buffer. It does exactly what it says, it copies the input onto the output, but also makes it negative, or positive it the input was negative to begin with. This example also introduces the concept of gain. We start by connecting the non-inverting input to ground. This will be the point in which the op-amp inverts about. The inverting input is connected through a resistor both to the input and the output. Basically, our aim here is to have the inverting input be 0 volts, so when an input voltage of say 5 volts is applied, we need an output to counteract that and make it 0 volts again. The easiest way to examine this circuit is to assume the inverting input is at 0 volts and calculate currents based on that. Remember, when two different currents meet, they add up. Let’s say that the two resistors are both 1k and the input is 5 volts. So the input resistor will drop 5 volts at 5 mA. The output needs to counteract this with a negative 5 mA, so it outputs -5 volts across the resistor. However, we can do more than just invert our signal, we can also amplify it or even integrate it. Let’s start with amplification. Remember how we can calculate currents to determine our output voltage, well if we change the resistors we need to change the output voltage to maintain equal currents. If we increase the input resistor, then the required current is lower, so the output has a lower voltage. As you can see, the amplification is just a ratio between the two resistors. The equation can be given by the formula: gain equals the feedback resistor divided by the input resistor multiplied by negative one. As you can see, the amplificaton can be really useful, especially for circuits like my hat speaker video, where we amplified the phone’s output audio voltage to play on speakers.
Next up are integrators, which are nearly the same as the inverting buffer that we just looked at, with one key difference. Now, if you do not know what an integral is, this part may not make sense to you and I recommend that you study some calculus, but as an extremely quick reminder, the integral is the area under the curve. So if you took the integral of a square wave, you would get a triangle wave. Anyways, we can take advantage of the capacitor’s function to integrate functions for us. The equation for a capacitor is the current equals the capacitance multiplied by the change in voltage over time. After placing the capacitor parallel to the feedback resistor, we have our integrator, but how does it work? Well, let’s take the simple square wave example. When the square wave is HIGH, the current is constant, so the capacitor will make sure to keep current flowing based on its equation. Ignoring the feedback resistor for a bit, and assuming that the input is 5 volts with 1kOhms, the current will be 5 mA. The capacitor will have 5 mA flowing through it, so in-turn, it will start generating a voltage drop. The more time that passes, the more voltage it will have to drop to keep the current flowing, so we get a ramp. The capacitor does the opposite on the negative portion of the square wave and generates another ramp. Allow this to continue oscillating and we have integrated a triangle wave from a square wave. And for either of these examples, if you want to return back to a positive voltage, you can simply just put the output into another inverting buffer.
I will go over one more example before diving into building our own op-amp, and that is the differential amplifier. The point of this circuit is to basically subtract the inverting input from the non-inverting input. Taking the example where all resistors are equal, in this case 1k, we can easily calculate the output. The non-inverting input will be 9 volts and the inverting input will be 7 volts. The non-inverting input has a voltage divider, so its voltage will be 4.5 volts. Now since the inputs must match, the inverting input will also be 4.5 volts. That means that there will be a 2.5 volts drop, creating 2.5 mA of current. The output will match this in the same way and drop that same 2.5 volts across the resistor, leaving only 2 volts on the output. That math checks out, 9 minus 7 is 2. So basically, this circuit is the same as the inverting buffer, but we change the point of reference on the non-inverting input to make it subtract the two inputs.
I hope you learned a lot from those examples, now its time to take a closer look at the input of the op-amp to discover how it works and make one of our own. It all starts with two NPN transistors in a differential pair. The differential pair here is the heart of the op-amp. The base of each transistor is one of the inputs on the op-amp, and the side that connectects to the output is the non-inverting side. Let’s give an example in comparator mode. The non-inverting side has 8 volts applied to it, and the inverting side has 5 volts applied to it, so the output should be close to the positive supply. The way this works is that since the transistor acts like two diodes in some ways, the emitter will be 0.7 volts lower than the base. The side with the higher input, in this case the non-inverting side, will determine the emitter voltage for both transistors. That means that barely any current will flow through the other transistor because its base is lower than its emitter. The output will be close to the positive rail because of all of the current flowing through the non-inverting side. The same is true in the opposite configuration, just in the opposite order.
One more addition that we will make is just an amplification stage. We can use a PNP and an NPN transistor to both drive the output of the op-amp and make it a little more stable and powerful. Now, this is cool already, I went ahead and turned it into a PCB. Now we can solder it together and have a big op-amp. If you want to build one for yourself check out the schematic in the description.
This may not be the most practical way of using an OP-AMP, but it certainly is interesting. Hopefully now you understand not only how to use op-amps, but also how they work internally. If you enjoyed this video and found it helpful please consider subscribing so that you can see my other videos. Have a good one.