Level 6 · Logic · project 4 of 4 · 60 min · ●●●●○

The Two-Switch Hall Light

A light with a switch at each end of the hall: flip either and the light changes. Four NAND gates in one CD4011 make an XOR.

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 light that either of two switches can flip
You'll learn XOR, built from four NAND gates
Watch for eight gate inputs, every one wired to something

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 switch closed.

simulating…

If it doesn't work

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

  • The light never comes on. Check pin 14 to + and pin 7 to −. Then pin 11: it should go high with exactly one switch on. If it doesn't, follow the debug steps: N1 on pin 3 first, then pins 4 and 10.
  • The light works like an OR: both switches on leaves it lit. Gate 1's output isn't reaching gates 2 and 3. Check the jumpers from pin 3 to pins 6 and 9.
  • Only one switch does anything. That switch's second jumper is missing: switch A must reach pins 1 and 5, switch B pins 2 and 8.
  • The light flickers or changes when you touch a wire. A floating input. Both 100 kΩ pull-downs must reach −, and every one of the eight input pins (1, 2, 5, 6, 8, 9, 12, 13) must be connected to something.
  • Works, but the LED is dim. Expected: a CD4011 output can only give a few milliamps. The simulation shows about 6 mA; a real chip gives less.

What you're building

Plenty of homes have a hall or staircase with a light switch at each end. You switch the light on downstairs, walk up, and switch it off upstairs. Whichever switch you flip, the light changes. Think about what that means: the light can't simply be "on when switch A is up", because then switch B would do nothing. It has to depend on both switches at once.

Write it down with switches as 1 (on) and 0 (off). Start with both off and the light off. Flip either one and the light comes on. Flip the other one too and the light goes off again:

  • A 0, B 0 → light off
  • A 0, B 1 → light on
  • A 1, B 0 → light on
  • A 1, B 1 → light off

On when the switches disagree, off when they agree. That truth table has a name: XOR, for exclusive OR. "A or B, but not both." You're going to build it out of the four NAND gates in one CD4011.

Why not just OR?

An OR gate is on when A is 1, or B is 1, or both. That's the first three rows right and the last one wrong: with both switches on, OR keeps the light on, so the second switch can't turn it off. XOR is OR with that last row taken away. You could build it that way, from an OR, an AND and a NOT, but that takes six NAND gates. There's a neater way that takes four.

Four NANDs make an XOR

Recall the NAND rule from The Two-Key Lock: the output is 0 only when both inputs are 1. Now wire four of them like this:

  • Gate 1: inputs A and B. Call its output N1.
  • Gate 2: inputs A and N1. Output N2.
  • Gate 3: inputs B and N1. Output N3.
  • Gate 4: inputs N2 and N3. Its output Y drives the light.

Gate 1 asks "are both switches on?" and says 0 only if they are. Gates 2 and 3 each look at one switch together with that answer. Work through the four cases:

A B N1 N2 N3 Y
0 0 1 1 1 0
0 1 1 1 0 1
1 0 1 0 1 1
1 1 0 1 1 0

Take the third row slowly. A is 1, B is 0. Gate 1 has a 0 input, so N1 is 1. Gate 2 sees A = 1 and N1 = 1, both high, so N2 is 0. Gate 3 sees B = 0, so N3 is 1. Gate 4 sees N2 = 0, a low input, so Y is 1: light on. Check the other rows the same way before you build. If one row comes out wrong on paper, it will come out wrong on the breadboard too.

The Y column is exactly the hall-light table. Gate 4 lights the lamp whenever gate 2 or gate 3 has gone low, and those two can only go low when exactly one switch is on. With both on, gate 1's 0 holds gates 2 and 3 high, and the light stays off.

Reading the pins

Notch at the top left, count anticlockwise:

  • Gate 1: pins 1 (A1), 2 (B1) → 3 (Y1). This is N1.
  • Gate 2: pins 5 (A2), 6 (B2) → 4 (Y2). This is N2.
  • Gate 3: pins 8 (A3), 9 (B3) → 10 (Y3). This is N3.
  • Gate 4: pins 12 (A4), 13 (B4) → 11 (Y4). This is Y.
  • Power: pin 14 (VDD) to +, pin 7 (VSS) to −.

So switch A goes to pins 1 and 5, switch B to pins 2 and 8, N1 (pin 3) to pins 6 and 9, N2 (pin 4) to pin 12, N3 (pin 10) to pin 13, and pin 11 to the LED. All eight inputs are used, so there are no spare gates to tie off this time, but every input still needs a firm 1 or 0 at all times.

The inputs: 100 kΩ pull-downs

Each slide switch connects + to its input. Switch off, the input would float, so each also has a 100 kΩ pull-down to −. Off: 0 V, a firm 0. On: the switch wins and the input sits at 4.5 V, while the pull-down passes 4.5 ÷ 100 000 = 45 µA. Each switch drives two gate inputs, but CMOS inputs take only picoamps, so that costs nothing extra.

What the LED sees

When Y goes high, the LED lane is the chip's output (about 200 Ω in the simulator) plus your 220 Ω plus the yellow LED's 2.0 V:

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

Clearly lit. A real CD4011B, rated at about 1 mA per output at 5 V, will light it dimmer than the simulation shows. A real hall light would have Y drive a transistor or MOSFET, and the transistor would switch the lamp.

The signal passes through three gates on its way to the LED (gate 1, then 2 or 3, then 4), each taking about 100 ns. So the light answers a switch in a few tenths of a microsecond. You won't notice.

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 −. Check it twice.
  3. Switch A: from + to pin 1; 100 kΩ from pin 1 to −. Jumper pin 1 to pin 5.
  4. Switch B: from + to pin 2; 100 kΩ from pin 2 to −. Jumper pin 2 to pin 8.
  5. N1: jumper pin 3 to pin 6, and pin 3 to pin 9.
  6. N2 and N3: jumper pin 4 to pin 12, and pin 10 to pin 13.
  7. The lamp: pin 11 → 220 Ω → LED long leg; LED short leg → −.
  8. Power up with both switches off: dark. Flip A: lit. Flip B: dark. Flip A back: lit. Flip B back: dark. Every flip changes the light.

On the Workbench the chip's output pads glow amber when that gate's output is high, so you can watch N1, N2 and N3 change as you flip the switches and check them against the table.

Debug like an engineer

Check the table one column at a time. With both switches on, pin 3 (N1) must be low; in any other position it must be high. If it isn't, the problem is in the switches or pull-downs. Then pins 4 and 10: each should go low only for its own switch alone (pin 4 for A only, pin 10 for B only). Last, pin 11 should be high whenever pin 4 or pin 10 is low. The first column that disagrees with the table points at the jumper to fix.

🧠 Your challenge

No single right answer. That's the point.

  • Wire it upside down. Change one connection so the light is on when the switches agree and off when they differ (that's XNOR). Is there a way that needs no extra parts at all, using only the switches?
  • A three-switch hall. Long corridors have three switches. Write the truth table for three switches where any single flip changes the light. How many rows are on? Can you build it from two XORs, and how many NAND gates, and CD4011 chips, would that take?
  • Make the OR. Build an OR from three NAND gates of the same chip (hint: NAND with both inputs inverted). Then explain, using its truth table, why an OR hall light would be annoying to live with.
  • Count the cost. With both switches on, how much current do the two pull-downs take together? With the light on, roughly how much does the whole circuit take? Estimate how many days three AA cells would last if the light were left on.
  • Harder: real stairwells do this with no chip at all, using two changeover (SPDT) switches and two wires between them. Sketch how that could work. (Sketch only. House wiring runs on mains voltage, and that is never a breadboard project.)

For grown-ups: safety notes

  • Battery power only: 4.5 V from three AA cells cannot shock you.
  • Never try this on a real house light. Hall and stair lights run on mains voltage, which can kill; changing house wiring is a job for a licensed electrician.
  • 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.
  • Every input must reach a rail or a gate output. A missing jumper leaves an input floating; the light flickers and the chip can run warm. If the chip feels warm, pull the battery and find the loose wire.
  • Keep the 220 Ω in the LED lane.

Checked against

Keep building