Level 6 · Logic · project 1 of 4 · 45 min · ●●●●○

The Two-Key Lock

Two NAND gates make an AND: the LED lights only while BOTH buttons are down. The first logic chip, and the reason NAND is called universal.

new here? Start with First Light — one battery, one resistor, one LED. It teaches the loop, LED direction, and why the 220 Ω resistor matters.

You'll make a two-button lock decided by a chip
You'll learn NAND truth table; two NANDs make an AND
Watch for no floating inputs: spare gates tied to a rail, buttons with pull-downs

What you need

🔧 Try before wiring →

you'll learn: Logic gates · Switches

Circuit map

The circuit you'll build, running on the Workbench right now — shown with the buttons held.

simulating…

If it doesn't work

The boring ways this circuit actually breaks — check these first before you unwire everything.

  • LED never lights, even with both buttons held. Check pin 14 to + and pin 7 to − first. Then the two jumpers from pin 3 to pins 5 and 6: gate 2 needs both inputs. Then the LED: long leg toward the 220 Ω.
  • LED on all the time. Pin 4 is wired to the wrong gate (check it is pin 4, not pin 3), or an input of gate 1 is shorted to −, or the chip is in backwards.
  • LED flickers, or lights with only one button. A floating input. Both 100 kΩ pull-downs must reach the − rail, and pins 8, 9, 12 and 13 must be tied to a rail.
  • Chip warm to the touch. Power reversed, or an input floating. Pull the battery, check the notch is at pin 1 and every input has a path to a rail.
  • Works, but the LED is quite dim. Expected: a CD4011 output can only give a few milliamps. The simulation shows about 6 mA; a real chip gives less. Drive a MOSFET from pin 4 if you want full brightness.

What you're building

A lock with two keys. Press button A: nothing. Press button B: nothing. Press both at once: the LED lights. You built this once already with two switches in a row, in The Vault. This time the deciding is done by a logic chip, a CD4011, and the buttons never carry the LED's current at all. They only tell the chip what they are doing. The chip decides.

That is a small circuit and a very large idea. Every decision a computer makes is a chain of exactly these: a few inputs, each either high or low, and a gate that answers with a high or a low of its own.

High, low, and the gate's question

A logic gate only knows two voltages. High means near the + rail, 4.5 V here; call it 1. Low means near 0 V; call it 0. The CD4011 draws the line at about half the supply, 2.25 V: above that an input reads as 1, below as 0.

Each of the chip's four gates is a NAND: NOT-AND. It asks "are both my inputs 1?" and then says the opposite. Its truth table, inputs A and B, output Y:

  • 0, 0 → 1
  • 0, 1 → 1
  • 1, 0 → 1
  • 1, 1 → 0

The output is low in exactly one case: both inputs high. Which is almost the lock you want, except upside down. You want the LED on for 1, 1.

Two NANDs make an AND

Here is the trick that makes NAND famous. Take gate 2 and tie its two inputs together. Now its inputs are always equal: 0, 0 gives 1 and 1, 1 gives 0. It has become a NOT gate: whatever goes in, the opposite comes out.

Feed gate 1's output into that NOT. Gate 1 says 0 only when both buttons are down; the NOT flips that to 1. Every other time gate 1 says 1 and the NOT flips it to 0. The pair together say 1 only when A and B: an AND gate, built from two NANDs. Add a NOT in front of each input and you can build OR too. Every logic function there is can be built from NAND alone, which is why engineers call it the universal gate and why a quad-NAND chip is the first logic chip worth owning.

Reading the pins

The CD4011 is a 14-pin chip. Hold it with the notch (or the dot) at the top left and count anticlockwise: pins 1 to 7 down the left side, 8 to 14 up the right.

  • Gate 1: inputs pin 1 (A1) and pin 2 (B1), output pin 3 (Y1).
  • Gate 2: inputs pin 5 (A2) and pin 6 (B2), output pin 4 (Y2).
  • Gate 3: pins 8, 9 → 10. Gate 4: pins 12, 13 → 11. Unused here.
  • Power: pin 14 (VDD) to +, pin 7 (VSS) to −.

Y1 jumpers to both A2 and B2: that is the NOT. Y2 drives the LED.

The inputs: 100 kΩ pull-downs

Each button connects + to one input of gate 1. Button up, the input is connected to nothing, and a logic input connected to nothing is neither 1 nor 0; it drifts, and the chip guesses. So each input also has a 100 kΩ to −, a pull-down. Button up: 0 V, a firm 0. Button down: the button wins and the input sits at 4.5 V, a firm 1, while the pull-down passes 4.5 ÷ 100 000 = 45 µA. That is the only current the buttons ever carry. A CMOS input itself takes a few picoamps, which is why a resistor this large is enough, and why the Switch That Uses No Current used the same value on its MOSFET gate.

Tying off the spare gates

Gates 3 and 4 do nothing, but their inputs must not float either. A CMOS input left in mid-air drifts toward half the supply, where both halves of the gate conduct at once: the chip oscillates, warms up and can upset the gates you are using. So A3 and B3 go to −, and A4 and B4 go to +. Either rail works; the rule is only that every input goes somewhere.

What the LED sees

When both buttons are down, Y2 goes high. A CD4011 output is a weak thing. The simulator models it as about 200 Ω between the pin and the rail it is driving, so the LED lane is 200 Ω inside the chip plus your 220 Ω plus the LED's 2.0 V:

(4.5 − 2.0) ÷ (200 + 220) ≈ 6 mA

Clearly lit, dimmer than First Light's 11 mA. On a real CD4011B expect dimmer still: the datasheet rates the output at about 1 mA at 5 V, and it sags under load. That is normal for the 4000 series. Logic outputs are signals, not power; when you need brightness, the output drives a MOSFET gate, which costs it nothing.

The LED resistor still matters. Without it the output would try to push (4.5 − 2.0) ÷ 200 ≈ 12 mA, which a real chip cannot deliver and would not enjoy trying.

Build it

  1. Rails: + along the top, − along the bottom, battery between them.
  2. The chip: across the centre gap, notch at the top. Pin 14 to +, pin 7 to −. Do this first and check it twice.
  3. Spare gates: pins 8 and 9 to −; pins 12 and 13 to +.
  4. Input A: button from + to pin 1; 100 kΩ from pin 1 to −.
  5. Input B: button from + to pin 2; 100 kΩ from pin 2 to −.
  6. The NOT: jumper pin 3 to pin 5, and pin 3 to pin 6.
  7. The lamp: pin 4 → 220 Ω → LED long leg; LED short leg → −.
  8. Power up. Dark. Press A: dark. Press B: dark. Press both: lit.

On the Workbench the board runs in time, because a chip has state. Hold each button with the mouse, then both; the output pads on the chip glow amber when that gate's output is high, so you can watch Y1 drop and Y2 rise the moment the second key goes down.

Debug like an engineer

Test the halves separately. Pin 3 (Y1) should sit near 4.5 V with the buttons up and drop near 0 V only with both held: that proves gate 1, the pull-downs and the buttons. Then pin 4 (Y2) should be the opposite of pin 3: that proves the NOT. If pin 3 never moves, an input is floating or a button is in the wrong rows. If pin 3 moves but pin 4 does not, one of the two jumpers from pin 3 is missing, so gate 2 has a floating input.

🧠 Your challenge

No single right answer. That's the point.

  • Make it an OR lock (either key opens it) using the two gates you have left. Work it out on paper first with the truth table: you need a NOT on each input, then a NAND of the two NOTs. How many gates is that in total, and does one CD4011 have enough?
  • Make it a NOT lock: the LED is on until either button is pressed. Which single gate does that, and what do you do with the second input?
  • Swap the 100 kΩ pull-downs for 10 kΩ. Does the lock still work? What changes about the current while a button is held? Now try removing one pull-down entirely and touching the bare input pin. Explain what you see.
  • Harder: wire Y2 to a MOSFET gate (from the Switch That Uses No Current) and let the MOSFET drive the LED through 220 Ω. Predict the LED current now, and explain why the chip's weak output no longer matters.

For grown-ups: safety notes

  • Battery power only: 4.5 V from three AA cells cannot shock you.
  • The CD4011 is a CMOS chip and a static spark can damage an input. Touch something earthed before handling it, and keep it in its foam or tube.
  • Pin 14 to + and pin 7 to −, checked before the battery goes in. A chip powered backwards gets hot within seconds and is usually destroyed.
  • Never leave an input floating with the power on, including the spare gates. A floating CMOS input makes the chip oscillate and run warm.
  • Keep the 220 Ω in the LED lane. The output is weak, but shorting it straight into an LED still asks more of it than the datasheet allows.

Checked against

Keep building