Teach Your Kid Electronics

Beginner · Project 8

Secret Code Lock

Build a lock that only opens (lights up green) when you press the right three buttons at once, and discover AND and OR logic.

Build this only with an adult present. Before you start, read our safety notice and disclaimer.

Beginner⏱ About 25 min

🧰 What you need

  • 1×Half-size breadboardA board full of little holes with metal clips inside, so you can build circuits without soldering.
  • 1×3×AA battery holder with wire leadsHolds three AA batteries in a row and gives you a red (+) and black (−) wire.
  • 3×AA batteryA regular household battery. Each one gives 1.5 volts.
  • 1×220 Ω resistor — red, red, brown, goldOur go-to LED resistor. Colors: red, red, brown, gold.
  • 1×5 mm LED (any color) — green, for "unlocked"A tiny light that only lets current flow one way. The long leg is +.
  • 3×Push button (tactile switch) — plus 1–2 extra as decoysA little button with 4 legs. It connects its two sides only while you press it.
  • 5×Jumper wireA short wire with stiff ends that push into the breadboard.

✨ The finished project

A three-button code lock: the green LED only lights when all three buttons in series are pressed ++−−++−−aabbccddeeffgghhiijj115510101515202025253030 220 Ω +− AAAAAA3 × AA = 4.5 V+−
A three-button code lock: the green LED only lights when all three buttons in series are pressed ++−−++−−aabbccddeeffgghhiijj115510101515202025253030 220 Ω +− AAAAAA3 × AA = 4.5 V+−
Schematic: A three-button code lock: the green LED only lights when all three buttons in series are pressed R1 220 Ω D1 LED S1 button S2 button S3 button + 4.5 V current
The finished build. This picture is generated from the circuit itself and checked by computer, so every leg is in the right hole.

Every computer, phone and game console makes millions of tiny decisions every second using logic: simple rules that turn yes-or-no questions into a yes-or-no answer. The simplest kinds of logic are called AND and OR, and you can build them with nothing but buttons!

AND: all of them, together

Remember the Two buttons experiment in Push-Button Light? When buttons are in series (one after another), the current has to get through every button to complete the loop. So the LED only lights if button 1 AND button 2 AND button 3 are pressed.

That’s a lock! Three buttons, and only pressing all three opens it.

Build the lock

  1. Red wire: T+3 (top red + rail, column 3) to a3.

  2. Resistor: b3 to b7.

  3. Green LED: long leg in e7, short leg in e8.

  4. Button 1: across the gap, legs in columns 10 and 12 (e10, f10, e12, f12). Connect it to the LED with a wire from a8 to a10.

  5. Button 2: legs in columns 14 and 16. Wire a12 to a14.

  6. Button 3: legs in columns 18 and 20. Wire a16 to a18.

  7. Black wire: a20 to T-20 (top blue − rail, column 20).

  8. Power: battery red wire into T+1, black into T-2. Press one button: nothing. Two: nothing. All three at once: 🟢 unlocked!

The AND table

Engineers write logic as a truth table: a table that lists every way the inputs could be set, and what happens for each one. Here’s the lock (✋ = pressed):

Button 1Button 2Button 3Lock
–––locked
✋––locked
✋✋–locked
–✋✋locked
✋✋✋🟢 open

Part 2: OR, any one will do

Now put two buttons side by side (in parallel) instead. Each button is its own path for the current, so pressing button 1 OR button 2 (or both!) lights the LED. That’s how a doorbell with two buttons, one at the front door and one at the back, works.

An OR circuit: two buttons side by side, either one lights the yellow LED ++−−++−−aabbccddeeffgghhiijj115510101515202025253030 220 Ω +− AAAAAA3 × AA = 4.5 V+−
An OR circuit: two buttons side by side, either one lights the yellow LED ++−−++−−aabbccddeeffgghhiijj115510101515202025253030 220 Ω +− AAAAAA3 × AA = 4.5 V+−
Schematic: An OR circuit: two buttons side by side, either one lights the yellow LED 4.5 V 220 Ω yellow LED button button
The OR version: two buttons side by side. Either one lights the yellow LED. This picture is generated and checked by computer too.

To turn your lock into an OR circuit:

  1. Take out button 3 and the wires around it, plus the wires at a12 and a14.
  2. Join the two buttons’ first sides underneath: a wire from j10 to j14.
  3. Connect each button’s other side to the − rail: a12 → T-11 and a16 → T-16.

Buttons 1 and 2 stay where they are (columns 10–12 and 14–16). The picture shows a yellow LED, but your green one works exactly the same.

Button 1Button 2Light
––off
✋–on
–✋on
✋✋on

Not working?

  • Lights with only some buttons pressed? One button is turned the wrong way, so it’s always “pressed”. Give it a quarter turn.
  • Never lights, even with all three? Check each link wire lands in the right columns, and that every button really straddles the middle gap.
  • Use the one-at-a-time trick: replace each button with a jumper wire, one at a time. When the LED lights, the button you just replaced was the problem.

Try it!