Everything digital, from the calculator to the processor in your phone, is ultimately built from logic gates: small circuits that take an input of 0 or 1 and output 0 or 1 according to a simple rule. And once you understand them, you'll understand the language of any digital circuit.
0 and 1 = voltage
- 1 (HIGH): a voltage close to the supply (5 or 3.3 V).
- 0 (LOW): a voltage close to zero.
The basic gates
AND
The output is 1 if all the inputs are 1.
| A | B | Y |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
An example: the machine runs if the start button is pressed and the safety door is closed.
With just switches: two switches in series with a lamp = AND (both must be closed). And two switches in parallel = OR. That's exactly the logic, with no chips.
OR
The output is 1 if any input is 1. An example: the bell rings if someone presses the outside button or the inside one.
NOT
It inverts: 1 becomes 0, and 0 becomes 1.
The simplest NOT: an NPN transistor with a resistor on the collector. Base HIGH → the transistor conducts → the collector is LOW. And base LOW → the collector is HIGH (via the resistor). That's a real inverter.
NAND and NOR
The same as AND and OR, but with the output inverted. NAND's output is 0 only when all the inputs are 1.
XOR
The output is 1 if the two inputs are different. An example: two switches on a staircase controlling the same lamp. Either one changes its state.
A quick summary
| Gate | The output is 1 when… | Code symbol |
|---|---|---|
| AND | All are 1 | A && B |
| OR | At least one is 1 | A or B |
| NOT | The input is 0 | !A |
| NAND | Not all are 1 | !(A && B) |
| NOR | All are 0 | !(A or B) |
| XOR | They differ | A != B |
NAND: the gate that does everything
With NAND gates alone you can build any other gate: NOT (connect the two inputs together), AND, OR… That's why it's called a "universal gate". And the same goes for NOR.
How?
- NOT = a NAND with its two inputs tied together.
- AND = a NAND followed by a NOT (that is, another NAND).
- OR = invert each input with a NAND, then a NAND on them.
So a single 74HC00 chip (4 NAND gates) can do any simple logic you need.
Gates do arithmetic: a half adder
XOR + AND together add two bits:
| A | B | Sum (XOR) | Carry (AND) |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
1 + 1 = 10 in binary: sum 0 and carry 1. Chain several of these together, and you add larger numbers. That's literally what's inside a processor, just billions of times over. See number systems.
The chips
The 74HC family (2 to 6 V) is the best known:
- 74HC00: 4 NAND gates.
- 74HC08: 4 AND gates.
- 74HC32: 4 OR gates.
- 74HC04: 6 NOT gates.
- 74HC86: 4 XOR gates.
Tips:
- Don't leave inputs floating on CMOS chips. Connect unused inputs to ground or the supply, because a floating input can make the chip heat up or misbehave, as we explained in pull-up.
- A 100 nF capacitor next to each chip; see decoupling.
The pin order on 14-pin chips (such as the 74HC00, 08, 32 and 86): pin 14 is the supply and pin 7 is ground, with the gates spread over the rest. The 74HC02 (NOR) and 04 have a slightly different arrangement, so check the datasheet. And how to count the pins is in the IC.
Voltage levels: the boundary between 0 and 1
A chip doesn't see exactly "0 or 1"; it has thresholds:
| Family on 5 V | Treats as 0 below | Treats as 1 above |
|---|---|---|
| 74HC | About 1.5 V | About 3.5 V |
| 74HCT | 0.8 V | 2 V |
What lies between the thresholds is an undefined region. That's why an ESP32 (3.3 V) with a 74HC on 5 V may not be understood (3.3 is below 3.5), while a 74HCT understands it well. See level shifting.
The Schmitt trigger (such as the 74HC14): a NOT gate with different thresholds for rising and falling, so a slow or noisy signal (such as a button or sensor) comes out clean with no chatter. Very useful with buttons.
A practical example: a simple alarm
You want a bell to ring if the system is armed and (the door opens or the motion sensor detects something):
Y = ARMED AND (DOOR OR MOTION)
- An OR gate from a 74HC32 for the door and motion.
- Its output with ARMED into an AND gate from a 74HC08.
- The output drives the bell through a transistor.
The same logic on the Arduino is one line:
bool alarm = armed && (door || motion);
digitalWrite(BUZZER, alarm);
Today
In most modern projects, this logic is written in the microcontroller's code rather than built from gates. But gates are still useful for very simple and very fast things, or with no programming. And understanding them is the basis for understanding number systems and the shift register.
When is a gate better than code? When you need a response in nanoseconds (code takes microseconds), or protection that must work even if the program hangs (such as an emergency stop), or a simple product without programming anything.
FAQ
Can I try gates without buying chips? Yes, in a simulator such as Falstad, which has ready-made gates and lets you watch the output change as you click the inputs.
What's the difference between && and & in code? && works on a single value (true or false), and & works on each bit of the number separately. So && is logic, and & is bitwise operations like those in number systems.