Chapter Eight

Relays and Gates

Reduced to its essentials, a computer is a synthesis of Boolean algebra and electricity. The crucial components that embody this melding of math and hardware are known as logic gates. These gates are not unlike the familiar gates through which water or people pass. Logic gates perform simple operations in Boolean logic by blocking or letting through the flow of electrical current.

You’ll recall that in Chapter 6 you entered a pet shop and boldly announced, “I want a male cat, neutered, either white or tan; or a female cat, neutered, any color but white; or I’ll take any cat you have as long as it’s black.” These criteria are summarized by the following Boolean expression:

images

This expression can be interactively realized in a circuit made up of switches, a battery, and a lightbulb:

A circuit with 8 switches, a battery, and lightbulb used for selecting a kitten.

Such a circuit is sometimes called a network, except that nowadays that word is used much more often to refer to connected computers rather than to an assemblage of mere switches.

The circuit contains a combination of switches, some wired in series and some wired in parallel. Switches wired in series perform logical AND operations, symbolized in the Boolean expression with a × sign. Switches wired in parallel perform a logical OR operation corresponding to the + sign. Because this circuit is equivalent to a Boolean expression, if the Boolean expression can be simplified, the circuit can be as well.

Here’s the expression that indicates the characteristics you want in a cat:

images

Let’s try to simplify it. Using the commutative law, you can reorder the variables that are combined with the AND (×) signs and rewrite the expression this way:

images

In an attempt to clarify what I’m going to do here, I’ll define two new symbols named X and Y:

images

Now the expression for the cat that you want can be written like this:

images

After we’re finished, we can put the X and Y expressions back in.

Notice that the N variable appears twice in the expression. Using the distributive law, the expression can be rewritten like this, with only one N:

images

Now let’s put the X and Y expressions back in:

images

Due to the plethora of parentheses, this expression hardly looks simplified. But it has one less variable, which means one switch can be eliminated. Here’s the revised version:

A simplified circuit with 7 switches, a battery, and lightbulb used for selecting a kitten.

Indeed, it’s probably easier to see that this network is equivalent to the earlier one than to verify that the expressions are the same!

But there are still too many switches in this network. There are separate switches for Male and Female, and only one should be required, perhaps on (or closed) for Female and off (or open) for Male. Similarly, there are separate switches for White and Not White.

Let’s make a control panel right now for choosing a cat. The control panel is simply five switches (much like the on/off switches you have on your walls for controlling your lights) and a lightbulb mounted in a panel:

A control panel for choosing a cat with five switches and a lightbulb.

The switches are on (closed) when they’re up, and off (open) when they’re down. The first switch lets you select Female or Male; the second is Neutered or Unneutered. There are three switches for selecting color: Black, White, and Tan. Only one of these should be on at any time, or none of them to select an Other-colored cat.

In computer terminology, the panel of switches constitutes an input device. Input is information that controls how a circuit behaves, in this case describing the characteristics of an ideal kitty. The output device is the lightbulb. This bulb lights up if the switches describe a satisfactory cat. The switches shown in the control panel are set for a female unneutered black cat. This satisfies your criteria, so the lightbulb is lit.

Now all we have to do is design a circuit that makes this control panel work.

In the previous chapter, you saw how devices called relays were crucial to the workings of the telegraph system. Over long distances, the wires connecting telegraph stations had a very high resistance. Some method was needed to receive a weak signal and resend an identical strong signal. The relay did this by using an electromagnet to control a switch, in effect, amplifying a weak signal to create a strong signal.

At this moment, we’re not interested in using the relay to amplify a weak signal. We’re interested only in the idea of a relay being a switch that can be controlled by electricity rather than by fingers. Although relays were originally designed for telegraphs, they eventually became part of the switching circuits used in the vast network of the telephone system, and this is how their versatility became more apparent to imaginative electrical engineers.

Like switches, relays can be connected in series and in parallel as logic gates to perform simple tasks in logic. When I say that these logic gates perform simple tasks in logic, I mean as simple as possible. Relays have an advantage over switches in that relays can be switched on and off by other relays rather than by fingers. This means that logic gates can be combined to perform more complex tasks, such as simple functions in arithmetic and, eventually, the workings of entire computers.

The discovery that relays could be used for performing Boolean operations is generally credited to computer pioneer Claude Elwood Shannon (1916–2001), whose famous 1938 M.I.T. master’s thesis was entitled “A Symbolic Analysis of Relay and Switching Circuits,” but a similar equivalence had been described a couple of years earlier by Japanese electrical engineer Akira Nakashima.

You can wire a relay with a switch, a lightbulb, and a couple of batteries like this:

A relay wired for demonstration purposes with a switch and battery at the input, and a switch and lightbulb on the output.

The switch at the left is open, and the lightbulb is off. When you close the switch, the battery at the left causes current to flow through the many turns of wire around the iron bar. The iron bar becomes magnetic and pulls down a flexible or pivoting metal contact that connects the circuit to turn on the lightbulb:

A relay wired for demonstration purposes. The switch at the input has been closed so the lightbulb on the output lights up.

When the electromagnet pulls the metal contact, the relay is said to be triggered. When the switch is turned off, the iron bar stops being magnetic, and the metal contact returns to its normal position.

This seems like a rather indirect route to light the bulb, and indeed it is. If we were interested only in lighting the bulb, we could dispense with the relay entirely. But we’re not interested in lighting bulbs. We have a much more ambitious goal.

I’ll be using relays a lot in this chapter (and then hardly at all after the logic gates have been built), so I want to simplify the diagram. We can eliminate some of the wires by using a ground and a capital V (for voltage) to represent the battery, as was done in Chapters 5 and 7. In this case, the grounds simply represent a common connection; they don’t need to be connected to the physical earth. Now the relay looks like this:

A simplified illustration relay showing a voltage and switch at the input, and a lightbulb at the output.

When the switch is closed, a current flows between V and ground through the coils of the electromagnet. This causes the electromagnet to pull the flexible metal contact. That connects the circuit between V, the lightbulb, and ground. The bulb lights up:

A simplified illustration of a relay. When the switch is closed on the input, the battery on the output lights up.

These diagrams of the relay show two voltage sources and two grounds, but in all the diagrams in this chapter, all the V’s can be connected to one another, and all the grounds can be connected to one another.

More abstractly, the relay can be shown without the switch and lightbulb but labeled with inputs and outputs:

A more simplified illustration of a relay with the input and output labeled.

If a current is flowing through the input (for example, if a switch connects the input to V), the electromagnet is triggered, and the output has a voltage.

The input of a relay need not be a switch, and the output of a relay need not be a lightbulb. The output of one relay can be connected to the input of another relay, for example, like this:

Two relays connected so that the output of one is the input to the other. The switch is off so the lightbulb is not lit.

These relays are said to be cascaded. When you turn the switch on, the first relay is triggered, which then provides a voltage to the second relay. The second relay is triggered, and the light goes on:

Two relays connected so that the output of one is the input to the other. The switch is on so the lightbulb is lit.

Connecting relays is the key to building logic gates.

Just as two switches can be connected in series, two relays can be connected in series:

Images

Two relays connected in series. Both switches are off and the lightbulb is off.

Now there are two switches on two relays. The output of the top relay supplies a voltage to the second relay. As you can see, when both switches are open, the lightbulb isn’t lit. We can try closing the top switch:

Two relays connected in series. The first switch is off and the lightbulb is off.

Still the lightbulb doesn’t light, because the bottom switch is still open and that relay isn’t triggered. We can try opening the top switch and closing the bottom switch:

Two relays connected in series. The second switch is off and the lightbulb is off.

The lightbulb is still not lit. The current can’t reach the lightbulb because the first relay isn’t triggered. The only way to get the bulb to light up is to close both switches:

Two relays connected in series. Both switches are on and the lightbulb is off.

Now both relays are triggered, and current can flow between V, the lightbulb, and ground.

Like the two switches wired in series that you saw in Chapter 6, these two relays are performing a little exercise in logic. The bulb lights up only if both relays are triggered. These two relays wired in series are known as an AND gate because it is performing a Boolean AND operation.

To avoid excessive drawing, electrical engineers have a special symbol for an AND gate. That symbol looks like this:

The standard symbol for the AND gate showing two inputs and one output.

This is the first of six basic logic gates. The AND gate has two inputs and one output. You’ll often see the AND gate drawn with the inputs at the left and the output at the right. That’s because people who are accustomed to reading from left to right prefer reading electrical diagrams from left to right. But the AND gate can just as well be drawn with the inputs at the top, the right, or the bottom.

The previous diagram with two relays connected in series is symbolized more succinctly like this:

An AND gate with two switches shown as inputs and a lightbulb as output.

Notice that this symbol for the AND gate not only takes the place of two relays wired in series, but it also implies that the top relay is connected to a voltage and that both relays are connected to ground. Again, the lightbulb lights up only if both the top switch and the bottom switch are closed. That’s why it’s called an AND gate.

If we think of the absence of a voltage as a 0, and the presence of a voltage as a 1, the output of the AND gate is dependent on inputs like this:

The AND gate shown with the four combinations of inputs and the resultant output.

As with the two switches wired in series, the AND gate can also be described in this little table:

A table showing the output of the AND gate based on the two inputs. The output is 1 only if both inputs are 1.

The inputs of the AND gate don’t necessarily have to be connected to switches, and the output doesn’t necessarily have to be connected to a lightbulb. The output of one AND gate can be an input to a second AND gate, like this:

The output of one AND gate is input to another AND gate, to which another switch is also input and a lightbulb is output.

This bulb will light up only if all three switches are closed. Only if the top two switches are closed will the output of the first AND gate output a voltage, and only if the third switch is also closed will the second AND gate output a voltage.

This configuration can also be expressed by this symbol:

The standard symbol for a three-input AND gate.

It’s called a 3-input AND gate. The output is 1 only if all the inputs are 1. You can also create AND gates with many more inputs.

The next logic gate requires two relays that are wired in parallel, like this:

Images

Two relays wired in parallel. Each relay is controlled by a switch and the outputs are connected to light up a lightbulb.

Notice that the outputs of the two relays are connected to each other. This connected output then provides power for the lightbulb. Either one of the two relays is enough to light the bulb. For example, if we close the top switch, the bulb lights up. The bulb is getting power from the top relay:

Two relays wired in parallel. The top switch is closed and the lightbulb lights up.

Similarly, if we leave the top switch open but close the bottom switch, the bulb lights up:

Two relays wired in parallel. The bottom switch is closed and the lightbulb is lit.

The bulb also lights if both switches are closed:

Two relays wired in parallel. Both switches are closed and the lightbulb is lit.

We’ve made a circuit in which the bulb lights up if the top switch or the bottom switch is closed. The key word here is or, so this is called the OR gate. Electrical engineers use a symbol for the OR gate that looks like this:

The standard symbol for an OR gate showing two inputs and an output.

It’s somewhat similar to the symbol for the AND gate except that the input side is rounded, much like the O in OR. (That might help you to remember which is which.)

The output of the OR gate supplies a voltage if either of the two inputs has a voltage. Again, if we say that the absence of a voltage is 0 and the presence of a voltage is 1, the OR gate has four possible states:

The OR gate shown with all four possibilities of two inputs, and the resultant output.

The output of the OR gate can be summarized the same way as the AND gate:

A table showing the behavior of the OR gate. The output is 1 if either input is 1.

OR gates can also have more than two inputs. The output of such a gate is 1 if any of the inputs are 1; the output is 0 only if all the outputs are 0.

The relays that I’ve been showing you here are called double-throw relays. At rest, the pivoting metal bar at the top is touching one contact, and when the electromagnet pulls it, it hits another contact. The lower contact is called the normally open output. That’s the one we’ve been using, but we could just as well use the upper contact, called normally closed. When we use this upper contact, the output of the relay is reversed. The lightbulb is on when the input switch is open:

A double-throw relay showing how it can light up the lightbulb when the input is zero.

When the input switch is closed, the bulb goes out:

A double-throw relay showing how the lightbulb is not lit when the input switch is closed.

A single relay wired in this way is called an inverter. It’s represented by a special symbol that looks like this:

The standard symbol for the Inverter with inputs and outputs.

It’s called an inverter because it inverts 0 (no voltage) to 1 (voltage) and vice versa:

The Inverter inverts a zero to a one, and a one to a zero.

This is a realization of the Boolean NOT operator.

Sometimes when people see the top inverter here, they ask, “How can there be a voltage at the output if there’s no voltage at the input? Where does that voltage come from?” Keep in mind that the inverter is actually a relay that is connected to a voltage.

With the inverter, the AND gate, and the OR gate, we can start wiring the control panel to automate a choice of the ideal kitty. Here it is again:

A control panel for choosing a cat with five switches and a lightbulb.

Let’s begin with the switches. The first switch is closed for female and open for male. Thus we can generate two signals that we’ll call F and M, like this:

A switch to select a female or male cat. The female signal is inverted to become the male signal.

When F is 1, M will be 0, and vice versa. Similarly, the second switch is closed for a neutered cat and open for an unneutered cat:

A switch to select a neutered or unneutered cat. The neutered signal is inverted to become the unneutered signals.

The other three switches select the color: black, white, or tan. Here are all three wired to a voltage:

Three switches to select black, white, or tan cat.

Some simple rules govern how you can connect gates and inverters: The output of one gate (or inverter) can be the input to one or more other gates (or inverters). But do not connect the outputs of two or more gates (or inverters) to one another.

The simplified version of the cat-selection expression was

images

For every + sign in this expression, there must be an OR gate in the circuit. For every × sign, there must be an AND gate.

Images

The circuit diagram to select an ideal cat realized with OR gates, AND gates, and in Inverter.

The symbols down the left side of the circuit diagram are in the same order as they appear in the expression. These signals come from the previous three illustrations. Notice the use of another inverter for the (1 − W) part of the expression.

Now you might say, “That’s a heck of a lot of relays,” and yes, that’s true. There are two relays in every AND gate and OR gate, and one relay for each inverter. But I’m afraid you’ll be seeing a lot more relays in the chapters ahead. Just be thankful you don’t actually have to buy them and wire them at home. (Unless you want to.)

I mentioned earlier that there are six standard logic gates. You’ve already seen three, and now it’s time for the others. The first two use the normally closed output of the relay that is used for the inverter. This output has a voltage present when the relay is untriggered. For example, in this configuration the output from one relay supplies power to a second relay. With both inputs off, the lightbulb is on:

Images

Two relays wired in series using the normally-closed outputs. Both switches are open and the lightbulb is lit.

If the top switch is closed, the bulb goes off:

Two relays wired in series using the normally-closed outputs. The first input switch is closed so the bulb is not lit.

The light goes off because power is no longer being supplied to the second relay. Similarly, if the top switch is open but the bottom switch is closed, the light is also off:

Two relays wired in series using the normally-closed outputs. The second input switch is closed, so the bulb is not lit.

And if both switches are closed, the lightbulb is off:

Two relays wired in series using the normally-closed outputs. Both input switches are closed, and the bulb is not lit.

This behavior is precisely the opposite of what happens with the OR gate. It’s called NOT OR or, more concisely, NOR. This is the symbol for the NOR gate:

The standard symbol for the NOR gate.

It’s the same as the symbol for the OR except with a little circle at the output. The circle means invert. The NOR is the same as an OR gate followed by an inverter.

An OR gate followed by an Inverter.

The output of the NOR gate is shown in the following table:

A table showing the output of the NOR gate. The output is zero except when both inputs are zero, in which case the output is one.

This table shows results opposite those of the OR gate, which are 1 if either of the two inputs is 1 and 0 only if both inputs are 0.

Yet another way to wire two relays is shown here:

Images

Two relays wired in parallel using the normally-closed outputs. Both switches are open and the lightbulb is lit.

In this case, the two outputs are connected, which is similar to the OR configuration but using the other contacts. The lightbulb is on when both switches are open.

The lightbulb remains on when only the top switch is closed because the bulb can get power from the bottom relay:

Two relays wired in parallel using the normally-closed outputs. The top switch is closed but the lightbulb remains lit.

Similarly, the lightbulb remains on when only the bottom switch is closed because it gets power from the top relay:

Two relays wired in parallel using the normally-closed outputs. The bottom switch is closed but the bulb remains lit.

Only when both switches are closed does the lightbulb go off:

Two relays wired in parallel using the normally-closed outputs. Both switches are closed and the lightbulb is not lit.

This behavior is exactly opposite that of the AND gate. This is called NOT AND or, more concisely, NAND. Unlike NOR, the word NAND was coined specifically to describe this type of logic. The word dates from 1958.

The NAND gate is drawn just like the AND gate but with a circle at the output, meaning the output is the inverse of the AND gate:

The standard symbol for the NAND gate.

The NAND gate has the following 'margin-bottom:0cm;text-align:justify;line-height: normal'>A table showing the output of the NAND gate based on combinations of input. The output is zero only when both inputs are one.

You’ll recall that the output of the AND gate is 1 only if both inputs are 1; otherwise, the output is 0. The output of the NAND gate is opposite that.

At this point, we’ve looked at four different ways of wiring relays that have two inputs and one output. Each configuration behaves in a slightly different way. To avoid drawing and redrawing the relays, we’ve called them logic gates and decided to use the same symbols to represent them that are used by electrical engineers. The output of the particular logic gate depends on the input, which is summarized here:

Images

The standard symbol for the AND gate. A table showing the behavior of the AND gate. The standard symbol for the OR gate. A table showing the behavior of the OR gate. The standard symbol for the NAND gate. A table showing the behavior of the NAND gate. The standard symbol for the NOR gate. A table showing the behavior of the NOR gate.

The inverter looks like this:

The standard symbol for the Inverter.

It inverts a signal from 0 to 1 or from 1 to 0.

Completing this array of tools is just a regular old relay:

A relay with a switch at the input and a lightbulb at the output.

This is called a buffer, and this is the symbol for it:

A standard symbol for the Buffer.

It’s the same symbol as the inverter but without the little circle. The buffer is remarkable for not doing much. The output of the buffer is the same as the input:

A diagram showing how the output of the Buffer is the same as the input.

But you can use a buffer when an input signal is weak. You’ll recall that this was the reason relays were invented for the telegraph many years ago. In real-life logic circuits, sometimes one output must serve as many inputs. This is known as fan out, and it can result in a lessening of the power available to each output. Buffers can help boost that power. Or a buffer can be used to slightly delay a signal. This works because the relay requires a little time—some fraction of a second—to be triggered.

From here on in this book, you’ll see very few drawings of relays. Instead, the circuits that follow will be built from buffers, inverters, the four basic two-input logic gates, and more sophisticated circuits built from these logic gates. All these other components are made from relays, of course, but we don’t actually have to look at the relays anymore.

Toward the beginning of this chapter, a little control panel was shown that let you select an ideal kitten. It had switches for black, white, and tan cats, but it omitted a switch for other colors—any cat that is not black or white or tan. But that’s a signal that can be created using three inverters and a three-input AND gate:

Three inverted inputs to an AND gate.

Three inputs are inverted and become inputs to an AND gate. Only when B, W, and T are all 0 will all the inputs to the AND gate be 1, causing the output to be 1.

Sometimes a configuration like that is drawn without the inverters:

Three inverted inputs to an AND gate shown as little circles.

Notice the little circles at the input to the AND gate. Those little circles mean that the signals are inverted at that point—a 0 (no voltage) becomes a 1 (voltage) and vice versa.

If you had to choose just one logic gate from the six that I’ve shown you, make it either a NAND or a NOR. You can use a NAND or a NOR to create all the other logic gates. For example, here’s how to combine the inputs of a NAND gate to create an inverter:

A NAND gate with its two inputs connected to become an Inverter.

You can use that inverter on the output of another NAND gate to make an AND gate. At first, it doesn’t seem possible that you can make an OR gate from a NAND gate, but you can. That’s because an AND gate with all its inputs inverted does exactly the same thing as a NOR gate:

Images

Two inverted inputs to an AND gate is the same as a NOR gate.

The output is 1 only if both inputs are 0.

Similarly, an OR gate with the two inputs inverted is equivalent to a NAND gate:

Two inverted inputs to an OR gate is the same as a NAND gate.

The output is 0 only if both inputs are 1.

These two pairs of equivalent circuits represent an electrical implementation of De Morgan’s laws. Augustus De Morgan was another Victorian mathematician, nine years older than George Boole, whose book Formal Logic was published in 1847, the very same day (the story goes) as Boole’s The Mathematical Analysis of Logic. Indeed, Boole had been inspired to investigate logic by a very public feud that was being waged between De Morgan and another British mathematician involving accusations of plagiarism. (De Morgan has been exonerated by history.) Very early on, De Morgan recognized the importance of Boole’s insights. He unselfishly encouraged Boole and helped him along the way and is today sadly almost forgotten except for his famous laws.

De Morgan’s laws are most concisely expressed this way:

A symbolic expression of De Morgan's Laws.

A and B are two Boolean operands. The bars on top indicate an inversion. In the first expression, A and B are inverted and then combined with the Boolean AND operator. This is the same as combining the two operands with the Boolean OR operator and then inverting the result (which is the NOR). It also works in English: If it’s not raining and it’s not snowing, then it’s not raining or snowing.

In the second expression, the two operands are inverted and then combined with the Boolean OR operator. This is the same as combining the operands with the Boolean AND operator and then inverting (which is the NAND). If I’m not big or I’m not strong, then I’m not big and strong. De Morgan’s laws are an important tool for simplifying Boolean expressions and, hence, for simplifying circuits. Historically, this was what Claude Shannon’s paper really meant for electrical engineers. But obsessively simplifying circuits won’t be a major concern in this book. It’s preferable to get things working rather than to get things working as simply as possible.

The next major project is nothing less than a digital adding machine implemented entirely with logic gates. But that project will need to be deferred for several chapters while we go back to elementary school and learn to count.

If you find an error or have any questions, please email us at admin@erenow.org. Thank you!