Blog
Logic Gates and the CPU
The on-off switch from earlier in the course, wired into logic gates that decide, adders that count and flip-flops that remember, running a fetch, decode and execute loop. We build one, then run a building controller's damper program on it.

Part 21 of 24 in Cobler's Electricity Fundamentals course. New here? Start with the course map.
The meeting room's CO2 reading is sitting in the level 9 air handling unit (AHU) controller as a 12-bit count (Part 20). So let's take the lid off the controller and see what's doing the deciding.
Next to the wiring terminals there's a circuit board, and on it a small chip. That chip is built from the same on-off switch we've been following since Part 4, just a lot of them.
So the question is how a CPU works, and the nice thing is that we already have most of the pieces. We only need to put them together.
Transistors as switches
Let's start with the switch we built in Part 15. A small voltage on the transistor's gate turns a path through the silicon on or off, with nothing moving. In a motor drive that switch carries hundreds of amps, and the whole point is the power it lets through.
Now what if we make the transistor tiny and stop caring about the current? The transistors on a chip are far smaller than a bacterium, and each one only has to drive the gates of a few others.
So what matters now is only its state. On, we call 1, and off, we call 0. And that's the bit from Part 20, held by a switch that another voltage can flip.
Chips pair up two kinds of transistor, one that turns on with a voltage on its gate and one that turns off with it. Wired as a pair, almost no current flows through them except at the moment they flip, and we call this pairing CMOS (complementary metal-oxide-semiconductor).
That's how a chip with billions of switches can run without melting. It uses energy mainly when something changes.
We can also put numbers on how far the switch has come:
- The relay in Part 6 manages at most five operations a second.
- The drive transistor in Part 15 switches 5,000 times a second.
- The processor in a laptop ticks a few billion times a second, with its transistors keeping up.
Logic gates
Back in Part 6, we wired relay contacts in series and got AND, in parallel and got OR, and an NC contact gave us NOT. We can do exactly the same with transistors.
Put two of them in series between the output and 0 V, and the output is pulled to 0 only when both gates are on. Add the matching pair on the other side to pull it up to 1 the rest of the time.
Now four transistors give us a little circuit whose output is 1 in every case except one, when both inputs are 1. If we compare that with the AND column in Part 6's truth table, every answer is flipped.
So it's an AND followed by a NOT, and we call it NAND. A circuit like this, a few transistors answering one yes-or-no question about their inputs, is what we call a logic gate.
Now here's the kind of surprising part. NAND is the only gate we actually need:
- Tie its two inputs together and it becomes NOT.
- Put a NOT after it and we're back to AND.
- Put NOTs in front of both inputs and we get OR.
So with NAND gates alone we can build any yes-or-no rule, as we could with relays (NAND logic).
Binary adding
So far our gates make decisions. But the controller also has to do sums, like turning a count into ppm and working out how far past 600 we are.
So how do we go from yes-or-no to arithmetic? Well, let's add two numbers in binary and watch what we're actually doing.
In binary, each column holds only a 0 or a 1, so adding one column has only four cases. We can make a little table, where the carry is what spills into the next column:
A | B | Sum | Carry |
|---|---|---|---|
0 | 0 | 0 | 0 |
0 | 1 | 1 | 0 |
1 | 0 | 1 | 0 |
1 | 1 | 0 | 1 |
Now look at the carry column. It's exactly the AND column from Part 6. And the sum column is 1 when one input is on but not both, which we call XOR (exclusive OR).
A few NAND gates can make that too. So a handful of gates side by side add one column. Give them a third input for the carry coming in from the column before, and we call it a full adder.
Let's try it on 5 + 3, which in four bits is 0101 + 0011.
- Right-hand column: 1 + 1 is 0, carry 1.
- Next column: 0 + 1, plus the carry, is 0, carry 1.
- Next: 1 + 0, plus the carry, is 0, carry 1.
- Last: 0 + 0, plus the carry, is 1.
So the answer is 1000, which is 8. Four full adders in a row, each passing its carry to the next, did exactly what we do on paper. For the controller's 12-bit counts we need twelve in a row.

Four full adders add 0101 and 0011 from right to left, each passing its carry to the next, and out comes 1000, which is 8.
Subtraction uses the same adder with one trick. The chip adds the negative of the second number, written in a form called two's complement.
Then the top bit of the answer tells it whether the result came out below zero. So "is the CO2 above 600 ppm?" becomes one subtraction, and the answer is a single bit.
And this is where scale starts to show. A common full-adder design uses 28 transistors, so twelve of them come to 336 transistors just to add two 12-bit numbers.
Imagine wiring that out of relays. In silicon it's a speck you couldn't see, and it finishes within one tick of the chip's clock.
Memory
Everything so far forgets straight away. Change the inputs and the outputs change with them. But the controller has to hold the count, the setpoints and its place in the program. So how do we make a switch remember?
Well, we already did it once. In Part 6's seal-in circuit, the relay's own contact fed current back to its coil. So it held itself on after we let go of the start button.
We can do the same with gates. Cross-connect two NAND gates so that each one's output feeds the other's input.
Then the pair settles into one of two states and stays there until a set or reset input changes it. That's the seal-in circuit again, now in a few transistors, and we call it a latch.
Add a clock, a steady tick that says "update now, and only now", and the latch becomes a flip-flop. It takes in a new value only on the tick.
Line up twelve flip-flops and they hold one 12-bit number, which we call a register. So the count of 2,048 from Part 20 sits in a register as twelve flip-flops, one on and eleven off.
Try it
How a CPU works
Now let's put everything on one bench. We have:
- registers to hold numbers, and an adder to work on them
- memory to hold lots more numbers
- a clock ticking millions or billions of times a second
The last pieces are a list of instructions, stored in memory as bits like everything else, and a counter that remembers which instruction comes next.
On every step, the processor does the same small loop (the instruction cycle):
- It fetches the next instruction from memory.
- It decodes which of its few simple jobs that instruction asks for. Those jobs are things like load a number, add, subtract, compare or jump to another step.
- It executes the instruction and moves the counter on.
That loop, repeated very fast, is basically how a CPU works, and the list of instructions is what we call a program, or code.
Honestly, when I first saw that, I expected something cleverer at the bottom, but there isn't anything cleverer. A CPU can't "open a damper".
It can only do steps like these, so somebody has to break the job down into them. For the level 9 sequence, the steps look something like this (illustrative):
- Load the CO2 count from analog input 1 into a register.
- Convert it to ppm, using the 4-20 mA scaling from Part 20.
- Subtract 600. If the result is below zero, set the damper to 0 % and skip to step 5.
- Otherwise, set the damper to (ppm − 600) ÷ 300 × 100 %, capped at 100 %.
- Write the damper % to analog output 1, where a digital-to-analog converter turns it into 0-10 V for the actuator.
- Jump back to step 1.
Let's run it once at 750 ppm. Step 3 gives 150, which isn't below zero. Step 4 gives 150 ÷ 300 × 100 = 50 %, so the output sends 5 V and the damper sits half open.
Each of those six lines turns into a handful of the processor's small instructions, and the whole pass takes a tiny fraction of a second.
From adder to app
So we've got a loop that fetches, decodes and runs instructions. But how does that become a laptop that opens apps and shows what we type? This is the step I struggled with longest.
What finally made it click is that opening an app, clicking and typing aren't something separate from computing. Each one is the processor doing sums and comparisons, the same as the damper program.
First, everything inside is a number:
- A letter is a number. In the code computers share, the letter A is 65, which is 0100 0001 in eight switches.
- A pixel's colour is three numbers, for red, green and blue.
- An instruction is a number too. Some of its bits say which job to do, and the rest say which numbers to do it to.
Then the adder gets company. Put it beside a subtractor, a comparator and some plain AND and OR gates. And feed all their answers into a chooser, a few gates that let only one answer through, depending on a couple of control bits.
That whole block is what we call the ALU, the arithmetic logic unit. With its control bits set one way it adds, and set another way it subtracts or compares. Each chip has its own codes for those settings.
Memory gets addresses. We line up millions of registers in rows and number each row. Then a decoder made of gates switches on only the row whose number we give it. So the processor can ask for whatever's in row 1,042.
Now here's the part that made it click for me. When the processor decodes an instruction, nothing reads it the way we read a sentence.
Its bits go straight onto wires. The "which job" bits become the ALU's control bits, and the "which numbers" bits become memory addresses. So an instruction is basically a set of switch positions.
The jump in the damper program is an instruction that writes a new address into the program counter, only when the compare bit is 1. That's how a list of numbers can make decisions and repeat itself.
So what's an app? It's a long list of those instruction numbers stored on the laptop's drive. People don't write the numbers by hand. They write code, and a program called a compiler turns it into them.
The operating system, which is itself a program running the same loop, copies the app's numbers into memory. Then it puts the address of the app's first instruction into the program counter. From that tick on, the processor is running the app.
Now let's watch a click (the numbers are illustrative). The mouse doesn't send "I'm over the Save button". It only sends how far it moved, say 12 steps to the right.
If the pointer was 640 across the screen, its new position is 640 + 12 = 652, and that's our adder at work.
The app knows its Save button covers 600 to 700 across and 40 to 70 down, so it asks: is 652 at least 600? Is it at most 700? Then the same two questions for the height.
Each question is a subtraction whose top bit gives the answer. When all four come back yes, the processor jumps to the instructions that save the file. So a click is a handful of sums and comparisons, then a jump.
Typing works the same way. Let's follow one key.
- Pressing A closes a switch in a grid of switches under the keys, Part 4's switch once again.
- A small microcontroller inside the keyboard spots which switch closed and turns it into a number.
- The computer collects that number over USB, which is bits on a wire, the same idea as Part 22.
- The USB controller sends the processor an interrupt. That's a signal that makes it save its place and jump to the instructions that handle the keyboard. Afterwards it goes back to where it was.
- The operating system passes the key to the app you're typing in, and the app stores 65 in the document.
- To show it, the app calculates which pixels form the letter. It writes their numbers into the block of memory the screen is drawn from. The graphics chip sends that block to the display about 60 times a second.
All of that is something like a few hundred thousand instructions (illustrative), and at a few billion a second it takes well under a millisecond.
That's why the letter seems to appear the instant you press the key. Yet the processor has just run a long chain of sums, comparisons and jumps to put it there.
Try it
From 2,300 to 28 billion transistors
None of this is new. Intel's 4004, the first commercial microprocessor, came out in 1971 with about 2,300 transistors (IEEE Spectrum).
Apple's M4 chip, from 2024, has 28 billion (Apple). So it's the same idea multiplied about twelve million times in a little over fifty years.
You can't solder billions of anything, so chips are printed with light instead. A pattern is projected onto a thin slice of very pure silicon, called a wafer, then etched and treated.
The whole thing is repeated layer by layer, dozens of times, until the transistors and the wiring between them are built.
A building controller doesn't need anything like an M4. Its processor is often a small microcontroller, which is a CPU with its own memory and input and output pins on one chip. But it runs the same fetch, decode and execute loop, and that's plenty for one AHU.
The level 9 AHU controller
So let's go back to the AHU controller on level 9 and watch it work. A building controller runs its whole program over and over, and each pass is what we call a scan.
For a sequence like this one, a scan every second or so is common (illustrative). In each scan it:
- Reads its inputs: the CO2 count, the supply air temperature, and the airflow switch that proves the fan is turning (a digital input).
- Runs the program: the damper steps above, plus a loop that holds the supply air at its setpoint by moving the chilled-water valve.
- Writes its outputs: 0-10 V to the damper and to the fan's variable speed drive from Part 15. It also sets the digital output that keeps the fan's relay coil from Part 6 energised.
Then the room answers back. The damper opens, fresh air dilutes the CO2, and on a later scan the count comes in lower, so the program eases the damper back.
The output changes the thing being measured, which changes the next reading, and that's what we call a closed loop, or feedback.
A simple rule like ours can make a valve swing back and forth, which operators call hunting. So most loops use PID control to settle smoothly.
Worth knowing: PID Control: From Watt's Governor to Your Chiller and how control loops work go into that. What a sequence of operation is shows how a whole plant's steps get written down.
And the interlocks from Part 6 haven't gone anywhere. The duty and standby pumps are still wired through each other's NC contacts in copper. So even a bug in the program can't run both pumps together.
Sharing the numbers
So one controller now runs one AHU by itself, scan after scan. But the building management system (BMS) server sits in the basement control room, and the analytics run somewhere else.
And the chiller plant's thirty power meters, each like the one in Part 19, have small processors of their own holding their own numbers. Running a separate 4-20 mA pair from every number to every place that needs it would take thousands of cables.
So how do we get the numbers out of each box? Well, what if they travel as bits, with lots of devices sharing one wire? That's Part 22: Modbus RTU and RS-485.
Go deeper (optional)
"How do Smartphone CPUs Work? Inside the System on a Chip" by Branch Education
The silicon story continues in two optional extras. The ARM Story is about the design inside most phone chips. From Schematic to Board is about how chips get wired together on a circuit board.
This is Part 21 of 24 in Cobler's Electricity Fundamentals course. Previous: Sensors and 4-20 mA Signals. Next: Modbus RTU and RS-485.
Cobler builds CobiNeural, which reads what a building's controllers and meters are counting. It turns that into energy, demand and fault numbers a facility team can act on. To see it on your own site, request a demo.
FAQ
Frequently asked questions
A CPU is a very large number of transistor switches wired into logic gates. Gates make adders that do arithmetic, and gates fed back into each other with a clock make flip-flops that remember bits. A counter steps through a program stored in memory. For each instruction, the CPU fetches it, decodes it and executes it, millions or billions of times a second.
Yes. The mouse only reports how far it moved, so the processor adds that to the pointer's position. The app then compares the result with each button's edges using subtractions. When every comparison passes, it jumps to that button's instructions. A key press arrives as a number and is stored as a number (the letter A is 65). It is shown by calculating the numbers of the pixels that form the letter.
Every other logic function can be built from NAND gates alone. Tie its inputs together and it is a NOT gate. Add a NOT after it and it is AND, and put NOTs on both inputs and it is OR. In CMOS a NAND gate takes only four transistors, which makes it a cheap, fast basic building block.
Two gates are cross-connected so that each one's output feeds the other's input. The pair holds whichever state it was last set to, which is called a latch. Adding a clock, so it only takes a new value on each tick, makes a flip-flop. It is the same idea as a relay seal-in circuit, whose own contact keeps its coil energised.
It runs the controller's program in a repeating loop called a scan. Each scan reads the inputs, such as sensor counts and status contacts. It calculates the outputs, such as a damper position, a fan speed or a pump start, and writes them. Then it starts again. Many building controllers repeat this every second or so.
Intel's 4004, the first commercial microprocessor, had about 2,300 transistors in 1971. Apple's M4 chip, from 2024, has 28 billion. Chips that size are printed with light on silicon wafers, layer by layer, rather than assembled from separate parts.
Ask us about this topic
Something in this article you want to dig into — or a situation in your own building it doesn't quite cover? Send us your question. We don't run public comments; the team replies to you directly by email.


