# Digital Gates using Transistors

In this post, I’ll show you how to make digital gates just out of transistors, as opposed to just using ready-made integrated circuits. We’ll make an AND, OR and NOT gate, and you can make other gates based on these. Knowing how to make digital gates is very useful in all kinds of applications, for example if you’d like to build a simple adding machine, or even an 8-bit computer! So, let’s get started.

## Transistors

Before we can make any gates out of transistors, you must first understand how a transistor works. Basically, transistors fall into these 2 main categories: NPN and PNP. I won’t go into the details about how they differ here, but you can read it here if you’d like. We’ll be using just the NPN version.

Now, a transistor consists of 3 main parts: collector (C), base (B) and emitter (E), with the NPN version shown below. The current then flows from the collector to the emitter, and how much current is able to pass through the transistor depends on the voltage on the base. For example, if the voltage on base is 0V, then no current will flow between collector and emitter, because the transistor will have a theoretically infinite resistance. Oppositely, if the voltage on base is enough to make the transistor “saturated” — for example, if it’s 5V — then the transistor will have theoretically zero resistance, allowing full current to flow from collector to the emitter. Although this explanation is quite simplified, it gives you an idea of the basic working of the transistor. You can also imagine it as an analogy to a water pipe leading from the collector to the emitter, with the base being the pipe’s valve — depending on the valve’s position (the voltage on the base), it can control the amount of water passing through the pipe (the current between collector and emitter).

NPN Transistor schematic

Transistor picture

This property of transistors, that the voltage on base can control the current between collector and emitter, is very useful in all kinds of applications. For example, transistors are used as digital switches, where the voltage on the base represents the state of the switch (on/off), which is used in computers. They can also be used as amplifiers of current, where you can use a smaller current (between base and emitter) to control a larger one (between collector and emitter), which is used e.g. for controlling DC motors which require large current by using a smaller one provided from a microcontroller. In this tutorial, we’ll be using just the switch property of transistors, which is very useful for making digital gates. So, let’s go and make an AND gate!

### AND gate

First of all, what is an AND gate anyway? It’s a component with two inputs, say A and B, and one output, $A \cdot B$. Each of those inputs is either a binary 0 or a binary 1 (i.e., true or false), which in practice means, that the voltage on the input pin is either close to 0V, or close to $V_{CC}$, which in our case will be 5V (just how close it must be depends on the technology — TTL, CMOS, etc., but we won’t deal with any of those in this tutorial). The output of the AND gate will then be a binary 0 or 1 (i.e., close to 0V or 5V), depending on the values of its input pins. If, and only if both inputs are true, the output will be true, and in any other case the output will be false. This can be illustrated on what’s called a “truth table” showing every possible combination of inputs, and for each one showing the output of the gate:

A B A AND B
0 0 0
0 1 0
1 0 0
1 1 1

You can see that this gate is called an AND gate because both A and B must be true in order for the output to be true, otherwise the output is false. This gate may be used in practice, e.g. for checking if both conditions are fulfilled before proceeding further.

But how do you make such a gate using transistors? Well, if you think about it, we can use 2 transistors in series to act like switches — if both of them are binary 0 (i.e., 0V), this means that each of them will have a (theoretically) infinite resistance, allowing no current to flow through them and thus making the output also binary 0. You can imagine it as a water pipe with two valves, A and B, with both of them closed — no water will flow through the pipe. Now, if one of them (either one) would be binary 0 and the other 1, it would mean that one would have a zero resistance, while the other would have an infinite resistance, again allowing no current to flow and making the output binary 0. Analogously, the water pipe has one of its valves opened and the other closed, still allowing no water to flow. Finally, if and only if both transistors had a binary 1 on their bases, each of the transistors would have a zero resistance, allowing the current to flow, and making the output binary 1 — it’s as if both valves of the water pipe were opened. And this is exactly the behavior we want, corresponding to the truth table above.

To make this gate in practice, I’ve drawn a schematic below of these 2 transistors connected in series (click on the image to enlarge). The first one, on the top, has its collector connected to $V_{CC}=5V$, then its emitter is connected to the second transistor’s collector, and the second transistor’s emitter is connected to ground (through a 4.7kΩ resistor to prevent a short circuit in case both transistors would have a zero resistance). The base of each transistor is connected through a switch to an input voltage of either +5V or ground, representing binary 1 and 0 respectively. There’s a 10kΩ resistor between the input voltage and the base of each transistor to limit the amount of current flowing through its base (from the input voltage to the ground).1 To indicate whether input A is binary 0 or 1 (i.e., whether the base of the first transistor is connected 0V or +5V), an LED marked A is connected to the input voltage through a 1kΩ resistor to the ground. If the base of the 1st transistor is connected to 5V, the LED would light up because it would also be connected to 5V. Otherwise, if the base is connected to 0V, the LED wouldn’t light up because there’s no potential difference across it. Therefore, this LED A serves as an indicator of the state of the first input A, and the same goes for LED B. Similarly, to indicate whether the output A AND B is binary 0 or 1, an LED $A \cdot B$ is connected after the 2 transistors, through a 1kΩ resistor to the ground. This LED will therefore light up only if both bases of the resistors are connected to +5V, which will be indicated by both LED A and LED B lighting up. Otherwise, this LED will not light up. To see it in action, you can skip to the documentation section and see the video. If you’d like to make other gates, continue reading.

AND gate — click to enlarge.

### OR gate

Now that we’ve made an AND gate, let’s make an OR gate. This will now be much easier since now you know the principles of how gates are made. The difference between an AND an an OR gate is summarized in the truth table of the OR gate shown below:

A B A OR B
0 0 0
0 1 1
1 0 1
1 1 1

From the table, you can see that for OR gate, just one of the inputs (either A or B) needs to be true in order for the output to be true. Only if both inputs A and B are false is the output false. And that’s why the gate is called an OR gate — either A or B needs to be true in order for the result to be true.

With that out of the way, let’s make the actual gate. Similar to the AND gate which utilized two transistors in series, an OR gate can be made by connecting 2 transistors in parallel. This will produce an OR gate, since just one of the bases of the transistors needs to be connected to +5V in order for the current to flow through it and through the LED marked A+B (i.e., A OR B), lighting it up, which indicates that the output is true. You can imagine this as two parallel water pipes which start from and end at a common pipe, with a valve on each one — to make the water flow, just one of the valves needs to be opened. Other than the fact that the transistors are positioned in parallel, and the output-indicating LED is renamed from $A \cdot B$ to $A+B$, there’s no difference between the schematic of an AND and an OR gate, with all the other components being positioned as before and having the same function as before. See the schematic below.

OR gate — click to enlarge.

### NOT gate

Lastly, we’ll make a NOT gate, aka inverter, which is actually much easier than the previous two. Basically, what it does is that it inverts the input value and displays it on the output. That is, if the input is true, the output will be false, and vice versa — see the truth table below.

A NOT A
0 1
1 0

To make this gate, we’ll use the schematic I’ve drawn below. The important part of the gate is shown in the middle, where the +5V power source is connected to a 4.7kΩ resistor, which is connected to a transistor’s collector, and the transistor’s emitter is connected to the ground. This circuit in the middle is actually called a voltage divider, because the 5V voltage divides between the 4.7kΩ resistor and the transistor. And, depending on the transistor’s resistance, there’ll be a different voltage drop across it, meaning that the output voltage will be different. For example, if the input A is binary 0, the transistor’s base will be connected to 0V, meaning that the transistor will have a theoretically infinite resistance (or at least much larger than the 4.7kΩ resistor connected above it), meaning that almost all of the 5V voltage will be dissipated on the transistor and almost 0V across the 4.7kΩ resistor, which means that the output voltage (which is between the resistor and the transistor) will be very close to 5V, making the output LED to light up. Oppositely, if the input A is binary 1, the transistor’s base will be connected to +5V, making the transistor to have a near 0 resistance, and so almost all 5V voltage will be dissipated on the 4.7kΩ resistor, leaving almost no voltage for the output, and so the output LED will not light up. Therefore, we can see that the voltage on the output will be the opposite of the voltage on the input, which constitutes a NOT gate. And that’s it!

NOT gate — click to enlarge.

So, in this post, I showed you how to make 3 types of gates — AND, OR and NOT — and by connecting the output of one gate to the input of another gate, you can combine these gates to make all kinds of interesting circuits. For example, you can make a simple binary adder, about which I’d like to make a tutorial in the future. However, you might encounter some problems with voltage drops, where the output voltage of one gate isn’t high enough to be the input for the other gate, one solution to which is described on Electronics StackExchange.

So, I hope this post has been interesting to you, and if you want to see the gates in action, watch the video below. Also, if you have any questions or other feedback, feel free to post a comment. Thanks!

## Documentation

1. However, this 10kΩ resistor probably isn’t necessary because there’s already a 4.7kΩ resistor before the ground. I think it’s used there because a) it’s a safety measure in case there wouldn’t be any resistor before the ground, and b) to further limit the amount of current through the base, since if just the 4.7kΩ resistor were used, it may let too much current pass through the transistor’s base and that’s why a 10kΩ may be used as well. But anyway, that’s just my guess. []

Tags:

Categories:

Updated: