Logic Gates and Truth Tables: the short answer
Digital circuits work with two states: HIGH (1) and LOW (0). A logic gate takes one or more inputs and gives an output by a fixed rule. Example: Only one of the eight rows (G=1, S=1, P=0) gives RUN = 1, which is exactly the safe behaviour you want.
- Level
- Beginner
- Time
- About 6 min
- Topic
- Digital logic
Read a truth table and build simple decisions from AND, OR and NOT, in hardware and in code.
Before this: Digital Inputs and Outputs
Digital circuits work with two states: HIGH (1) and LOW (0). A logic gate takes one or more inputs and gives an output by a fixed rule. NOT turns 1 into 0 and 0 into 1. AND gives 1 only when all inputs are 1. OR gives 1 when any input is 1. NAND and NOR are AND and OR followed by NOT, and any other logic can be built from NAND gates alone.
A truth table lists every input combination with its output. Two inputs give four rows (00, 01, 10, 11); three inputs give eight. Writing the table first is the easiest way to get a decision right, in chips or in code.
In a microcontroller sketch the same gates are the operators &&, || and !. For example, `if (doorOpen && !armed)` is an AND with a NOT. Real gate chips such as the 74HC00 (four NAND gates) are still useful when you need a decision faster than code can make it, or without any microcontroller at all. Never leave a CMOS gate input unconnected: tie unused inputs to GND or VCC.
Gates from first principles
A gate decides the output from its inputs with a fixed rule. The rules that matter: NOT inverts; AND needs all inputs high; OR needs any high; XOR is high when the inputs differ. NAND and NOR are the inverted forms and are the real building blocks, because every other gate can be made from NAND gates alone.
In a chip, a gate is a handful of transistors: for CMOS, pairs of P- and N-channel MOSFETs arranged so that one path to the supply or to ground is always open, and almost no current flows once it settles.
From a sentence to a circuit
Start from the plain-English rule, for example: the garage light turns on if the door is open and it is dark, or if the manual switch is on. List the inputs (door, dark, switch), write out all eight rows of the truth table, and read the rule off it: (door AND dark) OR switch.
The same table works for a chip, a program or a PLC. When there are many inputs, a table keeps you from missing the awkward case, for example what happens when the door is open, it is light and the switch is off.
Useful gates in the real world
A 74HC00 holds four NAND gates and a 74HC08 four ANDs. An SR latch made of two NAND gates remembers its last input: a first step to memory. A gate with a Schmitt trigger input (74HC14) cleans a slowly changing or noisy signal such as an RC-smoothed switch.
Unused inputs on a CMOS gate must be tied to a supply or ground. A floating input picks up noise, can sit half-on, and makes the chip draw extra current and misbehave.
Logic families and voltage levels
The 74HC series works from 2 to 6 V and is the default choice for 3.3 V and 5 V projects. The 74HCT series accepts TTL-level inputs, which lets a 3.3 V output drive a 5 V HCT gate reliably. A 5 V HC gate's output into an ESP32 pin can exceed its rating, so shift the level first.
Gates have a propagation delay of a few nanoseconds. Ignore it in slow projects, but when many gates are chained or a clock runs fast, it decides how quickly the circuit can work.
- Leaving unused gate inputs unconnected.
- Writing the logic by intuition without a truth table and missing the corner case.
- Mixing = and == in code, or & and && when you mean a logical AND.
A machine may start only when the guard is closed and start is pressed, unless stop is pressed
- Inputs: G (guard closed = 1), S (start = 1), P (stop = 1).
- Rule: RUN = G AND S AND NOT P.
- Check rows: G=1, S=1, P=0 → 1. Any row with P=1 → 0. G=0 → 0.
- In code: bool run = guard && start && !stop;
Result. Only one of the eight rows (G=1, S=1, P=0) gives RUN = 1, which is exactly the safe behaviour you want.
With two buttons (using INPUT_PULLUP) and one LED, write a sketch that lights the LED for AND, then change one operator for OR, then for XOR (a != b). Fill in a truth table for each by pressing the buttons.
3 quick questions
What does an AND gate output when its inputs are 1 and 0?
How many rows does a truth table with three inputs have?
Which C++ expression is the same as a NAND of a and b?
