Logic Gates & Truth Tables
Meet AND, OR, NOT, NAND, NOR, and XOR. Wire real 74HC logic chips on a breadboard and use your Arduino to print their truth tables automatically.
1What Is a Logic Gate?
A logic gate is a tiny circuit that takes one or more binary inputs and produces a binary output according to a fixed rule. Every digital system, including your Arduino's processor, is built from millions of these gates.
The behavior of a gate is described by a truth table: a list of every possible input combination and the output for each one. A gate with n inputs has 2ⁿ rows in its truth table.
2The Six Basic Gates
NOT (inverter) AND OR
A | Y A B | Y A B | Y
0 | 1 0 0 | 0 0 0 | 0
1 | 0 0 1 | 0 0 1 | 1
1 0 | 0 1 0 | 1
1 1 | 1 1 1 | 1
NAND NOR XOR
A B | Y A B | Y A B | Y
0 0 | 1 0 0 | 1 0 0 | 0
0 1 | 1 0 1 | 0 0 1 | 1
1 0 | 1 1 0 | 0 1 0 | 1
1 1 | 0 1 1 | 0 1 1 | 0- NOT: output is the opposite of the input
- AND: output is 1 only when all inputs are 1
- OR: output is 1 when at least one input is 1
- NAND: AND followed by NOT (0 only when all inputs are 1)
- NOR: OR followed by NOT (1 only when all inputs are 0)
- XOR (exclusive OR): output is 1 when the inputs are different
Note: Arduino code has the same operations: && (AND), || (OR), ! (NOT), and ^ (XOR on bits). You've been using logic gates in software all along.
3Real Gate Chips: the 74HC Family
Logic gates come packaged in 14-pin chips. The 74HC series works at 5V, so it pairs well with an Arduino Uno. Each chip holds several identical gates that share one power connection.
- 74HC08: four 2-input AND gates
- 74HC32: four 2-input OR gates
- 74HC04: six NOT gates (hex inverter)
- 74HC00: four 2-input NAND gates
- 74HC86: four 2-input XOR gates
74HC08 / 74HC00 / 74HC32 / 74HC86 pinout (top view, notch on the left)
1A [1] ● [14] VCC
1B [2] [13] 4B
1Y [3] [12] 4A
2A [4] [11] 4Y
2B [5] [10] 3B
2Y [6] [9] 3A
GND [7] [8] 3Y⚠ Warning: Never leave a CMOS input floating (unconnected). It can pick up noise, switch randomly, and waste power. Tie every unused input to VCC or GND.
Note: The 74HC02 (NOR) has a different pinout from the chips above: its outputs are on pins 1, 4, 10, and 13. Always check the datasheet.
4Build an Automatic Truth-Table Tester
Instead of flipping switches by hand, let the Arduino drive the gate's inputs, read its output, and print the whole truth table.
- Chip pin 14 → 5V, pin 7 → GND
- 100nF capacitor directly between pins 14 and 7
- Arduino pin 2 → chip pin 1 (input A), Arduino pin 3 → chip pin 2 (input B)
- Chip pin 3 (output Y) → Arduino pin 4, and also → 470Ω → LED → GND
- Tie the inputs of the three unused gates (pins 4, 5, 9, 10, 12, 13) to GND
const int inA = 2, inB = 3, gateOut = 4;
void setup() {
pinMode(inA, OUTPUT);
pinMode(inB, OUTPUT);
pinMode(gateOut, INPUT);
Serial.begin(9600);
Serial.println("A B | Y");
for (int a = 0; a <= 1; a++) {
for (int b = 0; b <= 1; b++) {
digitalWrite(inA, a);
digitalWrite(inB, b);
delay(5); // let the output settle
Serial.print(a); Serial.print(" "); Serial.print(b);
Serial.print(" | ");
Serial.println(digitalRead(gateOut));
}
}
}
void loop() {}Note: The 100nF decoupling capacitor supplies the brief bursts of current the chip draws each time it switches, keeping the supply voltage steady. Put one next to every logic chip.
5Challenges
- Swap in a 74HC00, 74HC32, and 74HC86 and confirm each truth table matches the section above.
- Chain two gates of the 74HC08 to build a 3-input AND, and extend the tester to 3 inputs (8 rows).
- Add a check that compares each reading with the expected value and prints PASS or FAIL, turning the sketch into a chip tester.
Skill Check
8 questions · Up to 180 XP
Q1.Which gate outputs 1 only when all of its inputs are 1?
Q2.What does an XOR gate output when both inputs are 1?
Q3.What does a NAND gate output when A = 1 and B = 1?
Q4.What should you do with unused inputs on a 74HC chip?
Q5.What does a 74HC08 contain?
Q6.For two inputs, which gate outputs 1 when exactly one input is 1?
Q7.What does a NOR gate output when both inputs are 0?
Q8.Why place a 100nF capacitor between a logic chip's VCC and GND pins?
Sign in to earn XP for your answers.