Logic Gates

Motivation

Since having an amazing, when sometimes troubling physics teacher in high school, I wanted to build a small computer from the ground up. Understanding how these machines we use daily work, and not only on an abstract level but on the hands on, is one of the obligations I set myself back then. Here I could now write some more about how a curious mind should try to understand the tools he uses on a daily basis from the ground up, but this would end up too much as a moral lecture and not a fun blog article, so I push this to another time. So the goal of this series of blogs is to build a computer, starting with the basic components like resistors, capacitors, transistors etc. I want to build every thing I later use as an integrated circuit before with these simple components. If I want to use a 555 IC later, I should build it myself on a breadboard before. With this, I would like to work myself up to a full functioning small computer with a CPU, memory and maybe even a graphical output. Later I may be looking in to ways of building my own transistor, I found some interesting ways to do so using a diode, but for now, I will assume that transistors are of the same basic-ness as resistors, capacitors and wires.

Right now I heavily rely on the amazing work that Ben Eater did over the course of the years, to get in to the topic. He has an outstanding teaching style and will be the basis at least for the first few sections of this blog.

Logic Gates

The basic building blocks of any computational device we use today are the so-called logic gates. They implement the logical operators we know from math or spoken language (with some imprecision) like AND, OR or XOR.

The goal here is to implement them in an electronic circuit, where the inputs are switches and the output is an LED. For this we use transistors. Their working mechanism is explained in a different blog post.

Inverter

We start with the most simple gates, the inversion. This turns a 1 in to a 0 and vice verca. When the switch is open, there is a voltage between points A and B. The LED is on and the output is therefore 1. Closing the switch we connect the points A and B via the transistor. Therefore there is no voltage over the LED and it stays of. The output is 0. With this we build an inverter.



AND Gate

We want a gate that has two inputs and one output. If both inputs are 1, we want the output to be 1 and in all other cases, we want the output to be zero. When we have a positive input on A and B, the current can flow from the 5V rail down to ground, lighting up the LED. If only one of the inputs are on, there is no current flowing, since the second transistor is acting as a blockage. Therefore the LED stays of. The same happens when both inputs are low.




OR Gate

We want a gate that has two inputs and one output. If either A or B, or both A and B are high, we want the output to be high. Only if both inputs are low, we want the output also to be low. The idea is to have a parallel circuit, where opening one arm, lights up the LED. Pressing either of the switches, we open the corresponding transistor, letting current flow from the 5V rail to the ground. If we press both buttons, more current can flow, but since we anyway just have to trigger a certain level to light up the LED, this does not bother us. The LED is on, and the task fulfilled. If both switches are open, no current flows through the transistors and the LED stays off.




XOR Gate

This circuit is a little more complicated. We want to have an output only if either A or B are pressed, but not if both. For this we basically build an OR gate, but have to disable it, in the case both are pressed. We therefore have to combine this with an AND gate. The main idea is to set the lower part of the OR gate to 5V when both inputs are pressed and otherwise to ground. For this we have to invert the output of the and gate and connect this today the lower part of the OR gate.

When we only press one of the buttons, the first part, the AND gate will be low. This means that the base of the transistor is connected directly to 5V and is therefore open. This connects the collector to the emitter and therefore to ground. This now grounds the lower part of the OR gate. Since one of the buttons is pressed, the LED inputs connected from the 5V rail to ground through one of the open transistors and lights up.

When we press both inputs, the first AND gate connects thee collector of the upper transistor to ground, grounding the base of the transistor in the second part. This then means that the collector of this transistor is directly connected to the 5V rail, without connection to ground, putting the lower part of the OR gate to 5V. Therefore the two open transistors connect the LED from 5V to 5V, meaning no voltage difference over the LED and therefore the LED stays off. When we press no of the inputs, the LED is not connected to ground since the OR gate is closed, and it also will stay off. Therefore we achieved the building of an XOR gate.

NAND Gates and all other gates

In theory you can build all gates from a NAND gate, which would be simply the AND gates output after an inverter. We therefore could build a NAND gate and all other gates from it. This is a fun exercise which will be done in a later post.

Extra: How to wire up a button

For the correct operation of logic gates or transistors, it is important that you define two states: On and Off or High and Low. This means one state where the circuitry will act and one where it doesnt. This requires the off state of the input to be connected to the same ground as the circuits are. Therefore we have to connect the output of the button to ground. Since when we would do this without any resistors, this would just cause a short-circuit as soon as we close the switch. Therefore we have the connect the output of the switch to ground using a resistor. This way the voltage drops over the resistor and we avoid a short circuit. Non the less, when the switch is open, the output is connected to ground and therefore in a well defined state. If we would not do this, the voltage on the output in the open state could be influenced by fluctuations or by other currents induced in the breadboard. This could lead to unsuspected behavior and some endless troubleshooting that will lead nowhere.