Light the Lamp: a logic gate puzzle game
A free logic gate puzzle game with 153 levels: flick the switches to light the lamp, starting with one wire and working up through NOT, AND, OR, NAND, NOR and XOR gates to number displays, adders and memory, then 50 levels from hard to extreme.
Some links in this article are affiliate links. If you buy through them, Braindumps may earn a small commission at no extra cost to you.
Light the Lamp is a free puzzle game about logic gates, the tiny switching circuits that every computer is built from. Flick the switches to light the lamp. The game starts with a single wire and adds one new part at a time: NOT, AND, OR, NAND, NOR, XOR and XNOR gates, then number displays, gates you choose yourself and circuits that remember. Each new part has a chapter of ten levels that starts easy and gets harder, with real circuits along the way, such as a combination lock, a burglar alarm and an adding machine that shows its answer. Then 50 levels mix every gate, from hard to extreme. There are 153 levels in 16 chapters, and every gate shows its truth table when you tap it.
The Light the Lamp game needs JavaScript switched on in your browser.
How to play
- Press a switch to turn it on (1) or off (0). Wires that are on glow orange.
- Most levels have one lamp with one or more wires going into it. It lights only when every one of its wires is on, and lighting it solves the level. All the switches start off.
- Some early levels have several lamps, each with a tag that shows its goal, ON or OFF. The tag turns green with a tick when the lamp matches it. Make every lamp match its tag to solve the level.
- On a number display level, the tag shows the number to make the display show.
- A switch with a padlock can’t be changed.
- Try to solve each level in the fewest presses. The fewest possible is shown under the board, and matching it earns a gold star.
- Each level shows its difficulty, from 1 (easy) to 10 (extreme).
- Tap any gate to see its truth table, with the row it is using right now highlighted.
- In the “Choose the gate” chapter, a dashed box is an empty socket. Press it to put a gate in, and keep pressing to try others. The table under the board must show a tick on every row.
- Tick “Show 1s and 0s” to see the value on every wire.
The game works with a mouse, a touchscreen or a keyboard (press Tab to move to a switch, then Space or Enter). On a phone the circuits run from top to bottom. Your progress is saved in your browser, so you can come back to it later. The Levels button lists every level, and the first level of each chapter is always open if you want to skip ahead.
How hard is each level?
The difficulty number on each level comes from bots that play the game:
- The random bot presses switches at random until every lamp matches its goal.
- The trial-and-error bot presses a switch, keeps the change if no lamp gets further from its goal, and undoes it if one does.
- The logic bot works backwards from the lamps like a careful player. It writes down every wire it can be sure of, and when it gets stuck it guesses one wire and follows the guess until it works or leads to a contradiction. It counts its steps, its guesses and its dead ends.
- The perfect bot checks every setting of the switches to find the fewest presses, which is the target for the gold star.
The difficulty is an estimate of how long a careful player would take, by whichever way is quicker for that level: working it out, or trying switches. It runs from 1 (a few seconds) to 10 (about ten minutes). More switches make a level harder, because there are more settings to try: six switches have 64 settings and eight have 256. Few lamps, XOR gates and wires that feed several gates make it harder still, because they leave the logic bot fewer sure steps and more guesses.
What is a logic gate?
A logic gate is a small circuit with one or more inputs and one output. Each input and output is either 1 (on, a high voltage) or 0 (off, a low voltage), and the gate works out its output by a fixed rule. Inside a chip, each gate is a handful of transistors used as switches. Join enough gates together and you get circuits that add, compare, choose and remember, which is everything a processor does.
A truth table lists a gate’s output for every combination of its inputs. A gate with two inputs has four combinations (00, 01, 10 and 11), so its truth table has four rows.
The seven logic gates
These are the gates in the game, with their outputs for each pair of inputs and a real chip that contains them. The chips are from the 74HC family, which runs on 2 to 6 volts.
| Gate | The output is 1 when | 00 | 01 | 10 | 11 | Chip |
|---|---|---|---|---|---|---|
| NOT | the input is 0 (it has one input) | 0 gives 1, 1 gives 0 | 74HC04 (six NOT gates) | |||
| AND | every input is 1 | 0 | 0 | 0 | 1 | 74HC08 (four AND gates) |
| OR | at least one input is 1 | 0 | 1 | 1 | 1 | 74HC32 (four OR gates) |
| NAND | at least one input is 0 (NOT AND) | 1 | 1 | 1 | 0 | 74HC00 (four NAND gates) |
| NOR | every input is 0 (NOT OR) | 1 | 0 | 0 | 0 | 74HC02 (four NOR gates) |
| XOR | the inputs are different | 0 | 1 | 1 | 0 | 74HC86 (four XOR gates) |
| XNOR | the inputs are the same | 1 | 0 | 0 | 1 | 74HC7266 (four XNOR gates) |
On a circuit diagram, a small circle on a gate’s output means NOT, which is how NAND, NOR and XNOR are drawn. In Boolean algebra, AND is often written A·B, OR is written A + B, XOR is written A ⊕ B, and a bar over a letter means NOT.
XOR (exclusive OR) is the one that catches people out. An ordinary OR gives 1 when one input or both inputs are 1; XOR gives 1 only when exactly one of its two inputs is 1. With three or more inputs, XOR gives 1 when an odd number of them are 1, which is why it is used for parity checks that spot a flipped bit in stored or sent data.
Why NAND and NOR are called universal gates
You can build any digital circuit from NAND gates alone, or from NOR gates alone. The game shows how in the NAND chapter:
- A NAND gate with its two inputs joined together is a NOT gate (“NAND as NOT”).
- A NAND gate followed by that NOT is an AND gate (“AND from NANDs”).
- NOT gates in front of a NAND gate’s inputs make an OR gate (“OR from NANDs”).
The last one works because of De Morgan’s laws. NOT (A AND B) is the same as (NOT A) OR (NOT B), and NOT (A OR B) is the same as (NOT A) AND (NOT B). The “Neither” level shows the second law: its two lamps always match, one driven by a NOR gate and one by two NOT gates and an AND gate.
The idea is not just a classroom trick. The Apollo Guidance Computer, which helped to fly astronauts to the Moon, had a processor built entirely from NOR gates, with two 3-input NOR gates on each chip.
How gates add numbers
Binary numbers use only the digits 0 and 1, and each column is worth twice the column to its right. Adding two single bits gives these answers:
| A | B | A + B | Carry (C) | Sum (S) |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 10 (two) | 1 | 0 |
The sum column is exactly XOR, and the carry column is exactly AND, so one XOR gate and one AND gate add two bits. That circuit is called a half adder. A full adder also adds a carry from the column before, using two XOR gates, two AND gates and an OR gate (the “Full adder” level). Line up one full adder per column, with each carry out feeding the next carry in, and you can add numbers of any size. A 74HC283 chip adds two 4-bit numbers like this, with extra gates that work out the carries faster.
How a number display works
The digits on clocks, meters and calculators are often seven-segment displays: seven bars, named a to g, that light in different patterns. The “Number displays” chapter starts by lighting the segments directly, then uses gates to turn a binary number into the right segments. That job is called decoding, and a chip such as the 74HC4511 decodes one digit: it takes a number from 0 to 9 on four wires and lights the segments for it, and it leaves the display blank for 10 to 15. Some displays have a decoder built in that shows 10 to 15 as the letters A to F, the digits of hexadecimal, and the later levels use one of those. The last levels join an adder, and then a subtractor, to the display, so the circuit works out the answer and shows it, as a calculator does.
How a latch remembers
Every circuit outside the memory chapter forgets: change a switch and the lamps change straight away. The memory chapter uses feedback instead. In an SR latch, two NOR gates each feed their output into one of the other’s inputs:
- Turn S (set) on and the lower gate’s output drops to 0, so the upper gate’s output, Q, rises to 1. Q then holds the lower gate at 0 by itself, so Q stays on after S goes off again.
- R (reset) does the opposite and turns Q off.
- With both off, the latch keeps whatever it was last set to. Turning S and R on together is not allowed in a real latch: both outputs go to 0, and the result when you let go depends on which switch goes off first.
A D latch adds two AND gates and a NOT gate in front, so there is one data input, D, and an enable input, E. While E is on, Q copies D; when E goes off, Q keeps the last value of D. Eight D latches side by side store one byte, and the 74HC373 chip has exactly that: eight D latches in one package. Registers in a processor are built from circuits that work in a similar way.
Build a level with real chips
Any level in the game can be built on a breadboard. Here is the “Both at once” level (one AND gate, two switches and a lamp) with real parts:
- a breadboard, such as Pimoroni’s 400-point breadboard (ad), and some jumper wires with a pin at each end, such as Pimoroni’s Jumper Jerky (ad)
- a 74HC08 chip (four AND gates)
- a 5 V supply, such as a USB breadboard power module
- two switches or push buttons (Pimoroni’s Maker Essentials switches and potentiometers pack (ad) has five 6 mm push buttons)
- two 10 kΩ resistors
- an LED and a 1 kΩ resistor
- Fit the chip across the gap in the middle of the breadboard. Connect pin 14 to +5 V and pin 7 to 0 V.
- Pins 1 and 2 are the inputs of the first gate. Wire each one to +5 V through a switch, and also to 0 V through a 10 kΩ resistor. The resistor holds the input at 0 when the switch is open. CMOS chips like this must never have an input left unconnected, because a floating input can make the chip misbehave and draw extra current, so connect the inputs of the three unused gates (pins 4, 5, 9, 10, 12 and 13) to 0 V as well.
- Pin 3 is the output. Connect it to the LED’s longer leg, and the LED’s shorter leg to 0 V through the 1 kΩ resistor. With a red LED that keeps the current to about 3 mA, which is plenty for a modern LED and well inside what the chip can supply. Our LED resistor calculator works out the resistor for other LEDs and supplies.
- Close both switches and the LED lights. Open either one and it goes out.
Swap the 74HC08 for a 74HC32 and the same circuit becomes OR; a 74HC86 makes it XOR, and a 74HC00 makes it NAND. Those chips use the same pins as the 74HC08, but the 74HC02 (NOR) does not: its first output is pin 1 and its inputs are pins 2 and 3. Always check the pin diagram in the datasheet before you wire a chip. If you are new to breadboards, start with our guide to making a simple LED circuit on a breadboard, and our explanation of what resistors do.
Questions
What are the seven basic logic gates?
AND, OR, NOT, NAND, NOR, XOR and XNOR. Some lists also include the buffer, which simply passes its input through unchanged.
Which gate is the universal gate?
Both NAND and NOR are universal gates: either one on its own can be used to build every other gate, and so any digital circuit.
What is the difference between OR and XOR?
With two inputs, OR gives 1 when one or both inputs are 1. XOR gives 1 only when the inputs are different, so 1 XOR 1 is 0.
What is a truth table?
A table with one row for every combination of a circuit’s inputs, showing the output for each. Tap any gate in the game to see its truth table, with the row it is using right now highlighted.
Does the game cost anything or need an account?
No. It is free, runs in your browser and needs no account. Your progress is saved in that browser on that device only, and the “Clear my progress” button in the Levels list removes it.
Sources
- Nexperia datasheets: 74HC4511 BCD to 7-segment latch/decoder/driver (one digit, blank for 10 to 15), 74HC00, 74HC02 (and its pin layout), 74HC04, 74HC08 (pins, the 2 V to 6 V supply range and the output current figures), 74HC32, 74HC86 and 74HC373.
- Texas Instruments: SN74HC7266 quadruple 2-input exclusive-NOR gates (datasheet listing), and Implications of slow or floating CMOS inputs.
- Philips: 74HC283 4-bit binary full adder with fast carry.
- Ken Shirriff: A computer built from NOR gates: inside the Apollo Guidance Computer.