Skip to content

Search is only available in production builds. Try building and previewing the site to test it out locally.

Demo 4.1: Logic Gates and What They Are For

A digital circuit works with two voltage levels rather than a continuous range. Once you agree that one level means 1 and the other means 0, a circuit stops being something you analyse with Ohm’s law and becomes something you can reason about with algebra. A logic gate is the smallest piece of that algebra built in hardware: a few transistors that take one or two logic levels in and produce one out.

There are only seven gates worth learning, plus the buffer. That is the whole vocabulary. Every counter, every memory cell and every processor you meet later in this module is built from these parts and nothing else.

The difficulty is not the truth tables. Those take ten minutes to memorise and are forgotten just as quickly. The difficulty is knowing which gate you reach for when you have a problem in front of you, and that comes from understanding what each gate is for. This demonstration is built around that question.

Pick a gate from the row of buttons, then click the inputs to change their levels. The schematic, the highlighted row of the truth table and the output all update together.

The View switch is the part worth your attention. In Abstract view the ports are named A, B and Y, which is how gates appear in a textbook. In Application view the model is identical and the logic is identical, but the ports carry the names of real signals in a real situation, and a sentence below tells you what the current combination means. Switching between the two views on the same gate is the exercise.

Logic Gates and What They Are For

Choose a gate, click the inputs, and switch between the abstract view and a real use of that gate.

View
A0B00Y
InputsY =0
ABY
000
010
100
111
ExpressionY = A · B
RoleAll conditions must hold.

Work through these four steps in order. Each one takes a couple of minutes.

Step 1: AND and OR are about how many conditions you need

Section titled “Step 1: AND and OR are about how many conditions you need”

Select AND and switch to Application view. The scenario is a machine press that may only cycle when the guard is closed and both hands are on the buttons. Set the inputs to 1 and 0, then 0 and 1. In both cases the press stays locked, because one condition on its own is not enough.

Now select OR, still in Application view. The scenario is a fire alarm fed by a ceiling smoke detector and an alarm button on the wall. Try the same two combinations. This time either one is enough on its own.

That contrast is the whole distinction. AND asks do all the conditions hold? and OR asks does any condition hold? Everything else about these two gates follows from that.

Select NOT and look closely at the symbol. It is a triangle, which on its own would be a buffer, with a small circle at the output. That circle is the inversion, and it is the same circle you will find on the nose of a NAND or a NOR.

Compare AND with NAND, then OR with NOR, switching back and forth and watching the truth table. The output column flips completely. Once you can read the bubble, you never have to memorise the inverted gates separately: NAND is AND with the output inverted, and NOR is OR with the output inverted.

In Application view, notice that NAND is doing something useful with that inversion. The scenario is a robot arm working inside a fenced cage, and the output is not a permission but a fault: motor power is cut unless the cage door is shut and the light curtain across the loading hatch is unbroken. Open the door, or reach an arm through the hatch, and the power goes. A machine that stops when anything goes wrong is safer than one that runs unless something tells it to stop.

Step 3: XOR asks whether the inputs differ

Section titled “Step 3: XOR asks whether the inputs differ”

Select XOR in Application view. The scenario is a stairwell lamp with a switch at the top and a switch at the bottom.

Set both switches to 0. The lamp is off. Now change either one on its own and the lamp lights. Change the other one as well and it goes off again. There is no combination in which a switch fails to do anything, which is exactly what you want from a light switch, and it is why this circuit is wired into essentially every staircase.

Read the truth table again with that in mind. XOR outputs 1 when the inputs disagree. XNOR, its inverted partner, outputs 1 when they agree, which makes it a one-bit equality test. Select XNOR and check that against the digital lock scenario.

Step 4: The buffer does nothing, and is still worth having

Section titled “Step 4: The buffer does nothing, and is still worth having”

Select Buffer. Its truth table is the most boring in digital electronics: the output copies the input.

Switch to Application view and look at the second figure. The top row is the circuit: a sensor, 30 metres of cable, the buffer, and the logic input it feeds. The bottom row plots the voltage on that same cable as you travel along it, with the 0 V and 5 V rails drawn so you can judge the size of the droop against the full swing.

The sensor drives a clean 5 V, but the cable has resistance, and the current drawn along it drops voltage. By the time the signal arrives it has fallen to 2.4 V. That is still above V_IH, so it still counts as a logic 1, but most of the margin is gone. A little more cable, a little more noise, and it lands in the shaded band where it is neither a guaranteed 1 nor a guaranteed 0.

The buffer reads that weakened level while it is still readable, decides, and re-drives a full 5 V swing. Nothing changed logically. What changed is the electrical quality of the signal, and that turns out to matter a great deal once wires get long.

Quiz
Select 0/1

A pump may only start when the tank level is above minimum AND the outlet valve is open AND the motor is not in a fault state. Which single gate implements this permissive?

Concept Match

Match each gate to its role

Code Check

One row of this NOR truth table is wrong

Quiz
Select 0/3

Which of these are true of a buffer? Select all that apply.

Quiz
Select 0/1

A robot cell is guarded by a cage door sensor and a light curtain, both reading 1 when the cell is safe, feeding a NAND gate whose output cuts motor power when it is 1. Why is the fault, rather than the permission, the useful output here?

Quiz
Select 0/1

A stairwell lamp is controlled by an XOR gate fed from a switch at the top and a switch at the bottom. Someone at the bottom flicks their switch while the lamp is already on. What happens, and why?

Eight gates, and each one answers a different question about its inputs:

  • AND asks whether all conditions hold, which is the shape of every safety permissive.
  • OR asks whether any condition holds, which is the shape of every alarm.
  • NOT inverts, which is what you need whenever the signal you have is the opposite of the signal you want.
  • NAND and NOR are AND and OR with the output inverted. The bubble on the symbol tells you so, and it saves you memorising them separately.
  • XOR asks whether the inputs differ, XNOR whether they agree. XNOR is a one-bit equality test, and a row of them is how any two numbers get compared.
  • Buffer changes nothing logically and exists for entirely electrical reasons, which makes it the exception that proves how physical this subject still is underneath the algebra.

Two things are worth carrying forward. The first is the bubble: once you read it as inversion rather than as decoration, half of these gates stop being separate things to learn. The second is that these gates are not a fixed set of building blocks handed down from somewhere. NAND on its own can be wired to produce every other gate in the list, which is why a manufacturer can fill a chip with nothing but NAND gates and still build anything. That is the subject of the next demonstration.