Engineering · Digital Electronics
From Transistor to Logic Gate
Your Arduino looks like magic: code goes in, decisions come out. Inside, it is made of one part repeated many thousands of times: the transistor, a switch flipped by a voltage. Wire a few switches together and you get a logic gate. Wire gates together and you can add numbers and remember them. That's a computer.
← Back to Engineering- SwitchA transistor is a switch with no moving parts, flipped by voltage.
- GateSwitches in patterns make NOT, AND, OR, and the rest.
- CPUGates make adders and memory. Add a clock and you have a processor.
The big idea
Two voltages, two meanings
Analog circuits care about every voltage in between: 2.71 V means something different from 2.73 V. Digital circuits agree to care about only two ranges. Near 5 V is HIGH, also written 1 or true. Near 0 V is LOW, written 0 or false. These are called logic levels, and they're why a slightly noisy wire doesn't change the answer.
One wire = one bit
A binary digit, or bit, is a single 0 or 1. Eight wires carry a byte: 256 possible patterns.
Logic = rules about bits
Boolean logic combines bits with three basic words: NOT, AND, OR. You already use them in code as !, &&, and ||.
Gates do it in hardware
A gate is a tiny circuit that turns input voltages into an output voltage following one of those rules, in a few billionths of a second.
Sim 1 · The transistor switch
A MOSFET: voltage in, current flows
An N-channel MOSFET has three legs: gate, drain, and source. The gate is insulated, so almost no current flows into it. When the gate voltage climbs past the threshold voltage, a channel opens between drain and source, and current flows through the load. Slide the gate voltage, or disconnect the Arduino pin and see what a floating gate does.
This is the same circuit you used in Outputs to let a 20 mA pin control an LED strip or motor that needs far more current. In logic chips, the “load” is just the input of the next gate.
Sim 2 · Gate workshop
Switches in series and in parallel
Here is the trick. A resistor pulls the output up to HIGH. Transistors underneath can pull it down to LOW. With one transistor, the output is the opposite of the input: a NOT gate. Put two transistors in series and both must turn on to pull the output LOW: a NAND gate. Put them in parallel and either one can: a NOR gate. Flip the inputs and fill in the truth table.
NAND is universal. Every other gate can be built from NAND gates alone: NOT is a NAND with its inputs tied together, AND is NAND followed by NOT, and so on. Early chip makers built entire computers out of a single kind of gate. You can do it too with one 74HC00 chip, which holds four NAND gates.
Sim 3 · Make gates do math
Build a half adder
In binary, 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, and 1 + 1 = 10 (that's “two”: write 0, carry the 1). A half adder needs two outputs: the sum bit and the carry bit. Pick the gate for each output so every row of the table comes out right.
Chain a full adder (two half adders and an OR gate, so it can also add the carry from the column to its right) eight times, and you can add any two numbers from 0 to 255. Your Arduino's processor has an adder like this at its heart, and it's built from the same gates.
Sim 4 · Gates that remember
One bit of memory: the SR latch
Everything so far forgets its answer the moment the inputs change. Now loop two NOR gates back into each other. Press Set and Q turns on. Let go, and it stays on: each gate's output holds the other one in place. Press Reset to turn it off. This is a latch, one bit of memory made from four transistors.
Add a clock
A clock signal is a square wave that ticks millions of times a second. Gate the latch with it and the bit can only change on a tick. That's a flip-flop, and it keeps billions of gates in step. Your UNO's clock ticks 16 million times a second.
Rows of flip-flops
Eight flip-flops side by side hold a byte. That's a register. Thousands of them make the memory where your sketch's variables live.
The whole stack
Transistor → gate → adder and latch → register and arithmetic unit → processor. Your int count = count + 1; really does run on this.
Back to the Arduino
What this means for your sketches
digitalRead() has thresholds
On a 5 V UNO, a pin reads HIGH above about 3 V and LOW below about 1.5 V. In between is a gray zone where the answer isn't guaranteed. A voltage divider sitting at 2.5 V is a bad digital input.
Never leave an input floating
An unconnected input is like the gate you disconnected in Sim 1: it drifts with static and reads random values. Always use a pull-up or pull-down resistor, or pinMode(pin, INPUT_PULLUP).
Gates in code
bool a = digitalRead(2);
bool b = digitalRead(3);
digitalWrite(8, !a); // NOT
digitalWrite(9, a && b); // AND
digitalWrite(10, a || b); // OR
digitalWrite(11, a != b); // XOR
digitalWrite(12, !(a && b)); // NAND Real chips use CMOS
The gates in this lesson use a resistor to pull the output HIGH, which wastes current whenever the output is LOW. Real chips replace that resistor with a second, opposite kind of transistor (P-channel) that pulls up. That pairing is called CMOS, and it's why your Arduino's chip runs cool and why a phone chip with over 10 billion transistors can run on a battery.
Real-bench lab
Build gates on a breadboard
- NOT gate.
5 V → 1 kΩ pull-up → the MOSFET's drain (2N7000 or another logic-level N-channel). Source to ground. The drain is the gate's output: show it with an LED and a second 1 kΩ resistor from the drain to ground. Wire the MOSFET's gate to a button with a 10 kΩ pull-down. Not pressed: the LED is on. Press: the transistor pulls the output LOW and the LED goes out. Measure the drain in both states with your meter.
- NAND gate.
Add a second MOSFET in series between the first one's source and ground, with its own button. The LED only goes out when both buttons are pressed. Fill in the truth table with real measurements.
- NOR gate.
Move the second MOSFET so the two are side by side (in parallel). Either button now turns the LED off.
- Chip version.
Swap the transistors for a 74HC00 (four NANDs) and check the pinout in its datasheet. Pin 14 is 5 V, pin 7 is ground. Build an AND gate from two of its NANDs.
Quick check
Can you think in gates?
1. An N-channel MOSFET's gate is at 0.5 V and its threshold is about 2 V. What happens?
2. A NAND gate has inputs A = 1, B = 1. What is the output?
3. Which gates make a half adder?
4. A button input reads 1, 0, 1, 1, 0 even when nobody touches it. What's the most likely fix?
5. What lets an SR latch remember a bit after you let go of Set?