Introduction: Simple Logic Gates and Circuits
Logic gates are some of the basic building blocks of digital logic circuitry. In this Instructable we will talk about a few of the simplest of these devices, and see some of the fun things you can do with them.
So what are logic gates? Logic gates have one or more input terminals, and the voltage at these terminals are translated into Boolean inputs of 1 or 0. Typically a high voltage is read as a 1 and a low voltage as a 0. These inputs can come from anything, be it a battery, a sensor, some IC or even another logic gate. The logic gate also has one output terminal, and the voltage at this terminal will be high or low depending on the type of gate and the inputs received. Logic gates may be strung together and combined almost infinitely, and such combinations can be designed to make pretty complex decisions based on any number of variables.
By the end of this Instructable, we will have covered:
- AND gates
- OR gates
- NOT gates
- Logic circuit synthesis
There is a 123Dcircuits circuit that goes with this Instructable here.
You will need to fork the circuit to be able to experiment with the circuitry like we will a little later, but if you can't be bothered its OK if you just hit the simulate button and get started.
So lets get right to it!
Step 1: AND Gates
The first type of gate we will look at is the AND gate. The circuit symbol for a two-input AND gate is that funny bullet-shaped thing. The terminals marked A and B are the inputs, and the one marked F is the output.
An AND gate determines its output by the following rule: if both inputs are 1, the output is 1; if either or both inputs are 0, the output is 0 (recall that 0 and 1 are "low" and "high" voltages, respectfully). The AND operation is often called Boolean multiplication because the result is as if you "multiplied" the two inputs, and is also written as F = AB, similar to multiplication. The table below the diagram is called a truth table. This kind of table lists all possible combinations of inputs, and shows you what their corresponding outputs would be. This particular truth table is for the AND operation; notice that the output is 1 only when both inputs are 1, as you would expect.
Most logic gates can be made quite easily with transistors. In the circuit diagram above you can see a simple AND gate built using two transistors. Yes I realize my artistic skills never left the second grade. In essence this is like wiring two switches up in series; current does not flow to the output unless both switches are closed. Using transistors as switches, the circuit can be controlled completely electronically.
Now take a look at the 123D circuit (click here). You should see three breadboards with a multimeter each. Each is the circuit for a different logic gate, and each multimeter measures the voltage across the output. The first circuit is the one we want to look at here. Hit the simulate button and you should notice the reading of a tiny voltage, on the order of a few millivolts. This is normal, and is small enough to be disregarded. The two switches towards the bottom of the circuit are like the inputs. Try flipping one of the switches, then the other. You will notice the voltage increase a little with only one switch on, but again it is negligible. Only when both switches are flipped on does the voltage jump to any considerable level. This is the basic working of an AND logic gate.
Step 2: OR Gate
Another type of logic gate is the OR gate. The circuit symbol is that arrowhead kind of shape, and inputs and outputs are marked the same way as the AND gate.
Let's look at the OR gate's truth diagram. As you can see, as long as any one of the inputs is 1, the output will be 1 and only if all inputs are 0 will the result be 0. The OR operation is also known as Boolean addition, and is written accordingly, as F = A+B. Note that there is only "high" and "low" voltage, and no such thing as "higher." Therefore 1+1 still equals 1 not 2. Keep in mind that all these operations are analogous to mathematical operations, but not strictly the same thing.
The OR gate can be seen as the equivalent of a circuit with two switches wired in parallel between input and output. As long as either one or both of the switches are closed, current can flow.
Now let's take a look at the second breadboard in the 123D circuit. Play around a bit and you will notice the transistors are wired up in parallel as per the provided circuit diagram. Once you flip any one of the switches, you should see the voltage jump immediately to 7 volts. Only if both switches are off does the output drop back down, just as you would expect.
Step 3: NOT Gates and Combinations
The NOT gate is relatively quite simple. It has only one input and what it does is take the value of of the input and invert it. It has a very simple truth table that illustrates this. The output is F = A̅ (read A-bar). The bar indicates inversion, so A̅ is simply A inverted.
Now let's head back to the 123D circuit, turning our attention to the final breadboard. With the switch on, the output will be low and with the switch off the output will be high. Now let's try something different. Remove the connection between the AND gate's output resistor and the breadboard's ground, then connect the resistor to the input of the NOT gate right above the switch (make sure the NOT gate's switch is off). Notice what this does? The AND gate's output is now the input of the NOT gate, and the NOT gate inverts whatever the AND gate outputs. You can do something similar with the OR and NOT gates, and even AND and OR gates. Transistor-based logic gates can be strung together like this almost infinitely. Feel free to play around a bit, the best part of using 123D circuits is that you can experiment without cost and without damaging real parts.
In the next section we will take this idea of combining further, talk about designing specific logic circuits based on the desired truth table, and finish off with a detailed example that uses everything talked about so far.
Step 4: Logic Circuit Synthesis
Logic gates can be strung together in many different ways. Each combination gives you a different Boolean "Function." Here we outline two simple ways to design a logic circuit that results in the exact truth table we want to get, in this case the table displayed above.
In this approach we concentrate on the rows of the truth table that need to produce an output of 1. For now let's look at the first row. Notice that if we invert the 0 inputs (all of them in this row) and multiply them together (this would be done with a 3-input AND gate), we will get a function that is 1 if and only if the conditions in the first row are met: A̅B̅C̅ is 1 only when A=B=C=0. This product will be one of the terms in our 'sum-of-products'. Let's look at the next row where F=1 which is the third row. Which inputs do we need to invert to ensure a value of 1 when they are multiplied together? A and C are zero so must be inverted. The result is A̅BC̅, which will be the second term in our sum. In the same way the seventh and eighth rows give us ABC̅ and ABC as our third and fourth terms. Note what happens when we add all our terms together and setting F = A̅B̅C̅ + A̅BC̅ + ABC̅+ ABC. Each of these terms will be 1 only with a specific set of inputs. By adding them together F will be 1 when any one of these terms is 1, and 0 if none of them are 1 (picture above). Therefore, F conforms with our truth table. Now we just need to build the logic circuit described by the function F. You can find the circuit diagram above.
The second approach to designing logic circuits is very similar to the one already discussed. As you may have guessed, when using the product-of-sums method, instead of summing products, we multiply sums.
Instead of looking at the rows of the truth table with a result of 1, we look at those with a result of 0. Let's look at the second row. What sum will produce a 0 with these inputs? If we invert C then add it to A and B, we will get 0+0+0=0. So the term for this row is A + B + C̅, which equals 0 only in the case of the second row. Moving on to the fourth row, it is obvious we need to invert B and C, giving us A + B̅ + C̅. Carrying on through to the sixth row, we get our four terms: A + B + C̅, A + B̅ + C̅, A̅ + B + C, A̅ + B + C̅. Notice what happens if we multiply these together: F = (A + B + C̅)(A + B̅ + C̅)(A̅ + B + C)(A̅ + B + C̅). As long as any one of our sums equals 0, F will equal 0. Only when all of them equal 1 will F equal 1. This complies exactly with the truth table. One possible implementation of this circuit is given in a diagram above.
It is obvious that the two circuits for the truth table are rather complex, quite more so than they need be. Logic expressions can be simplified quit a lot, in much the same way that they could be simplified if they were purely mathematical expressions.
As in basic algebra, some properties apply:
- AB = BA
- A(BC) = (AB)C = ABC
- A + B + C = A + (B + C) = (A + B) + C
- A(B + C) = AB + AC
Since we are dealing with Boolean algebra, we have some additional properties, which I'm sure are quite intuitive:
- AA = A
- AA̅ = 0
- A + A = A
- A + A̅ = 1
Once you have these simplification is a piece of cake. Here's a simple example.
Say F = A̅B̅D + A̅BD + BCD + ABC. If implemented directly, this circuit would be rather complex, requiring seven gates to complete. Let's simplify it a little.
Notice that A̅D can be factored out of the first and second term giving us F = A̅D(B̅ + B) + BCD + ABC
Since B̅ + B always equals 1 it falls out and we are left with F = A̅D + BCD + ABC
The next step is a little more intuitive. BCD equals 1 only when B, C and D are all 1. But in this situation either A̅D will equal 1 or ABC will equal 1 (check this). Since in addition if any one of the terms are 1 the result is 1, the BCD term is completely redundant and can be dropped. This leaves us with the final result of F = A̅D + ABC. This is a much easier expression to design a circuit for, and can be completed with only four components, a huge improvement! If you are curious you can write up truth tables for the initial and final expressions, and see that they are the same.
Next, let's put everything we have learned so far to use in one big real-world example!
Step 5: Final Example
As a grand ending to this Instructable, we will try to combine everything from the previous steps into one giant example. If you grasped everything covered so far it should be relatively easy, and if there are still one or two concepts not completely clear to you then this example should clarify.
You have been tasked with designing the circuit that controls the power-on mechanism of a toothpaste-capping machine. The circuit should determine whether or not the machine powers on based on the following rules:
- The machine does not power on unless the safety has been disengaged.
- The machine does not power on unless toothpaste caps have been loaded into it.
- The machine does not power on if an error or malfunction has occurred.
- There is a manual override that switches the machine on regardless of caps and warnings.
We have four inputs: S is 1 while the safety is on and 0 when off. Input C becomes 1 once all caps have been loaded. In the case of any error or malfunction input E becomes 1. Input O is controlled by the manual override switch, and is 1 if switched on. The output P turns on (1) only when the given conditions are satisfied. The first thing we need to do is to make a truth table, so let's see how to do that.
This part would probably be easier on pencil and paper, so go grab yourself some. Now draw the table you've seen in all the truth diagrams so far, and label the inputs S, C, O and W, and the output P. The next step is to write down every combination of inputs possible; since we have 4 inputs we should get a total of 2^4=16 combinations. Once that's done we have to figure out the output of each set. If we think about it for a moment we can drastically reduce the amount of work required. Notice from the rules that if the safety is still on, the machine will never power on. This means that anywhere S=1, P=0. Just like that half the outputs are done. Let's look at the manual override next. For the remaining eight combinations as long as the override is on, the output will be 1 regardless of the other two inputs. That should be four more down, halved again! Now let's look at the remaining four systematically. Note that the order may be different in your truth table depending on the order of your inputs. On my table the next blank is where S C O E equal 0 1 0 1. Since E=1 the output will be 0 (rule 3). Next, when S C O E equal 0 1 0 0 we find that P=1 because the safety is off, caps are loaded, and no errors have occurred which complies with all the rules. Moving on to when S C O E equal 0 0 0 1. We see from the input that the caps have not been loaded yet (rule 2) and some error or malfunction has been detected (rule 3). Since the manual override has not been engaged the machine should not start up and P=0. Finally the last blank is where all inputs are 0. Obviously the caps have not been loaded again, so P=0.
Now your truth table should be complete, and you can check it against the one provided above. Again, your order may differ but the same inputs should be produce the same output. The next step is to come up with a function that satisfies our truth table.
Here we have a choice between using POS or SOP. Looking at the output column we see many, many 0's but only a few 1's. It seems simpler to use the SOP approach because then we will have fewer terms to deal with in our function.
We have five rows with output of 1. They are: (in the order S C O W) 0111, 0110,0100,0011 and 0010. It is relatively straightforward to find that our sum of products is P = S̅COW + S̅COW̅ +S̅CO̅W̅ + S̅C̅OW +S̅C̅OW̅
This is would be a rather complex circuit to try and build, so let's see how we can simplify things. Obviously we can pull out an S̅, and if we pull out CO and C̅O where possible we get P = S̅[ CO(W + W̅) + C̅O(W + W̅) + CO̅W̅ ]
Remembering that an input added to its inverse always equals 1 (check the identities on the previous page), we can scratch out (W + W̅) and are left with P = S̅( CO + C̅O + CO̅W̅ ). Finally, If we pull out O and scratch out the resulting C + C̅ term, our result will be P = S̅O + S̅CO̅W̅. This may be as far as we can go mathematically, but there is still one more simplification we can make. Both terms have O's in them, one inverted and one not. This is often a sign that we can try to make further simplification. Let's see if we can. Obviously O is either 1 or 0. If it is 1 and S̅ is also 1, then the left term will be 1 making P 1. If O is 0 and S̅CW̅ = 1, then again P = 1. But notice, if S̅, C, and W̅ all equal 1, then P = 1 regardless of O. In this case if O = 1 then the first term will equal 1 (S̅O = 1), and if it equals 0 then the second term equals 1 (S̅CO̅W̅ = 1). Therefore we find that if S̅, C, and W̅ are 1 then O is irrelevant and can be dropped from that term. Practically this means that if the safety has been switched off, the caps have been loaded and no warnings are in effect, the machine can power on regardless of whether the manual override is on or not (check the truth table and you will find this is true). Now, our final result is P = S̅O + S̅CW̅, much more practical isn't it. One possible implementation of this function is shown above. Notice how few components it uses in comparison to the original SOP we started with.
There you have it! In the space of a few minutes we have taken a problem, found a solution, optimized it and implemented the results. This Instructable barely scratches the surface of the wonders of digital electronics, but hopefully you have gained a basic understanding and an appreciation for it. This then concludes the Instructable, I hope it has inspired you to design and make something of your own.