Digital Logic
Build a digital combination lock with logic gates
A combination lock made of logic gates opens for one setting of its switches and stays shut for every other. For four switches A, B, C and D and the code 1 0 1 1, the rule is OPEN = A · B̄ · C · D: an AND of all four, with a NOT on every switch that must be 0. Built from 2-input gates, that's one NOT and three ANDs.
Write the rule from the code
An AND outputs 1 only when every input is 1. The switches that should be on, A, C and D, can go straight in. B should be off, so it goes through a NOT first: NOT B is 1 exactly when B is 0. Now all four AND inputs are 1 for the code, and at least one is 0 for anything else.
Chain the ANDs
Each AND here takes two inputs, so combine the four signals two at a time:
- AND A with NOT B
- AND C with D
- AND those two results, and wire the output to the OPEN light
Three ANDs and one NOT. Chaining them in a line, ((A · B̄) · C) · D, works just as well and uses the same three gates.
Live experiment
Crack the code
Four switches, A to D, and a vault that opens for 1 0 1 1 only. The door shows the four inputs of the AND that opens it: A, NOT B, C and D. B's lamp is lit when B is off. Get all four lit and the door swings open.
“Project: Combination lock” is the project that ends Gate Grove, Digital Logic World 1. Free with an account. Lessons unlock in order, so you’ll do the 6 before it first.
Test all sixteen
Four switches give 2⁴ = 16 settings, so the truth table has 16 rows with a single 1. Test the code first: OPEN should light. Then test the four near misses, each one switch away from the code: 0011, 1111, 1001 and 1010. Every one must stay dark. A near miss that opens means that switch's wire never reached the ANDs.
| Setting | OPEN |
|---|---|
| 1011, the code | 1 |
| 0011, 1111, 1001, 1010 | 0 |
| the other 11 | 0 |
Make the code settable
A lock you can't re-code isn't much use. Add four code switches, K1 to K4, and compare each lock switch with its code switch using an XNOR gate, which outputs 1 when its two inputs match. AND the four XNOR outputs together and OPEN lights whenever every switch matches its code switch, whatever the code is.
To build it for real, a 74HC08 has four 2-input ANDs and a 74HC04 has six NOTs. Pull each switch input to a definite level with a resistor, so an open switch reads 0 instead of floating.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Project: Combination lock” is the project that ends Gate Grove, Digital Logic World 1. Free with an account. Lessons unlock in order, so you’ll do the 6 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: Two AND gates, one feeding the other. AND the first two, then AND that result with the third. The output of one gate is just a wire, and it can feed another gate.
- Why not “Three AND gates”? Three gates is one more than needed: AND two signals, then AND that result with the third.
- Why not “A NOT gate”? A NOT flips one signal; it can't combine them. Combining three takes two ANDs.
- Why not “One AND gate with a spare wire”? A 2-input AND has room for two signals only. Leaving a wire spare would leave the third signal out of the rule.
Show the answer
Answer: 8. 2 × 2 × 2.
- Why not “3”? 3 is the number of inputs. Each can be 0 or 1, so the combinations multiply to 8.
- Why not “6”? Each input doubles the lines, so three inputs give 2 × 2 × 2 = 8, not 6.
- Why not “9”? 9 would be 3 × 3, but each input has only two values, so it's 2 × 2 × 2 = 8.
Show the answer
Answer: NOT OP1 and NOT OP0. LOAD is opcode 00: both bits must be 0, so both go through a NOT first.
- Why not “OP1 and OP0”? OP1 and OP0 are both 1 only for opcode 11, so that AND picks out HALT, not LOAD.
- Why not “NOT OP1 and OP0”? NOT OP1 and OP0 is 1 for opcode 01, ADD. For 00, both bits go through a NOT.
- Why not “OP1 and NOT OP0”? OP1 and NOT OP0 is 1 for opcode 10, JUMP. LOAD's 00 needs a NOT on both bits.
“Project: Combination lock” is the project that ends Gate Grove, Digital Logic World 1. Free with an account. Lessons unlock in order, so you’ll do the 6 before it first.
Questions people ask
How do I change the lock's code?
In the fixed version, move the NOT gates: one goes on every switch that must be 0. For a settable lock, compare each switch with a code switch using an XNOR gate, which gives 1 when the two match, then AND the four XNOR outputs together.
How secure is a 4-bit combination lock?
Not very: there are only 16 settings, so trying them all takes a minute. Each extra switch doubles that, so 10 switches give 1,024. Real keypads add many more combinations, a delay or lockout after wrong tries, and a latch so the door stays open.
Why does the lock open when I flip extra switches?
That switch isn't really in the ANDs. If changing it never changes OPEN, its wire is missing or goes to the wrong gate. Test the code, then every setting one switch away from it: all four near misses must stay shut.
Related topics
- AND and NOT gates explainedAND needs every input at 1; NOT flips its one input. Together they build safety interlocks and masks.
- How a binary decoder worksTurn a binary code into exactly one lit line: how a CPU decodes its instructions.
- How to make a truth tableEvery input combination and its output: the complete, unarguable description of a logic circuit.
- Logic gates explainedAND, OR, NOT, XOR and their bubbled twins: each one's rule is a truth table.