Digital Logic
Build a traffic light circuit from flip-flops and gates
A traffic light is a state machine: it's always in one of a fixed list of states and moves to the next on a timer. You can build one with no program at all from four D flip-flops and two gates. A divide-by-2 turns a 2 Hz clock into a 1 Hz tick; a Johnson counter of three flip-flops steps through six states, one a second; and gates decode the states into lamps.
The sequence
- Red for two seconds
- Red and amber for one: get ready
- Green for two seconds
- Amber alone for one: stop if you can
- Back to red, for ever: a six-second cycle
1. The divider
CLK1 ticks twice a second. FF1 has its Q̄ fed back to its D, so it toggles on each tick and its output changes once a second. Its Q̄ clocks everything else.
2. The ring
FF2, FF3 and FF4 form a twisted ring on that 1 Hz clock: FF2's Q to FF3's D, FF3's Q to FF4's D, and FF4's Q̄ back to FF2's D. From 000 it fills and empties, six states, one per second.
3. The lights
Read the lamps off the ring's last two flip-flops:
| Ring (FF2 FF3 FF4) | RED | AMBER | GREEN |
|---|---|---|---|
| 000 | on | ||
| 100 | on | ||
| 110 | on | on | |
| 111 | on | ||
| 011 | on | ||
| 001 | on |
Red covers the three states where FF4 is 0, amber lights wherever FF3 and FF4 differ, and green where both are 1. Two gates and one spare output give the whole sequence.
Live experiment
Watch the sequence first
A 2 Hz clock, divided to 1 Hz, drives a twisted ring of three flip-flops. RED is the last flip-flop's Q̄, AMBER an XOR of the last two, GREEN an AND of them. Watch red for two seconds, red and amber for one, green for two, amber for one, and round again.
“Project: Traffic light” is the project that ends Toggle Tides, Digital Logic World 3. Free with an account. Lessons unlock in order, so you’ll do the 20 before it first.
Parts list
| Part | How many | Job |
|---|---|---|
| D flip-flop | 4 | One divider, three in the ring |
| XOR | 1 | Amber |
| AND | 1 | Green |
Red needs no gate at all: it's FF4's Q̄ output.
Check it
Watch for two ticks of CLK1 per step: red, red, red and amber, green, green, amber, then back to red and the same again. If the light never changes, check the twist: FF4's Q̄, not its Q, must feed FF2. If everything runs twice as fast, the ring is clocked from CLK1 instead of the divider.
Real junction controllers are microcontrollers now, with sensors and pedestrian buttons, but inside they're still state machines stepping through a list like this one.
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: Traffic light” is the project that ends Toggle Tides, Digital Logic World 3. Free with an account. Lessons unlock in order, so you’ll do the 20 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: Green only. RED is FF4's Q̄, which is 0. AMBER is 1 XOR 1 = 0. GREEN is 1 AND 1 = 1.
- Why not “Amber only”? AMBER is FF3 XOR FF4, and 1 XOR 1 is 0, so amber is off.
- Why not “Red and amber”? RED is FF4's Q̄, which is 0 when FF4 is 1, and AMBER needs the two to differ.
- Why not “Red only”? RED is FF4's Q̄. With FF4 at 1, red is off.
Show the answer
Answer: Off, off, on. S1 takes S3's opposite, 0; S2 takes S1's old 0; S3 takes S2's old 1. The wave is sitting down.
- Why not “On, on, on”? S1 takes the opposite of S3, which is on, so S1 stays off.
- Why not “Off, on, off”? S3 copies S2's old value, which was on, so S3 stays on.
- Why not “On, off, off”? S1 takes S3's opposite: S3 is on, so S1 is off.
Show the answer
Answer: 4 Hz. Q flips once per tick, so one whole blink, on then off, takes two ticks. 8 ÷ 2 = 4 Hz.
- Why not “2 Hz”? 2 Hz is what a second toggling stage would give. One flip-flop divides by 2, not 4.
- Why not “8 Hz”? 8 Hz is the clock itself. The flip-flop needs two of its ticks for each full blink, on then off.
- Why not “16 Hz”? A flip-flop can only change on its clock's rising edges, so it can't run faster than the clock.
“Project: Traffic light” is the project that ends Toggle Tides, Digital Logic World 3. Free with an account. Lessons unlock in order, so you’ll do the 20 before it first.
Questions people ask
Why does the light show red and amber together?
That's the British sequence, also used in several other European countries: red and amber together means get ready, green is coming. Many other countries go straight from red to green, which this circuit can do too by changing the gates.
How do I change the timings?
Each state lasts one tick of the divided clock, so a longer phase needs more states. A four-flip-flop Johnson counter has eight states to share out between the lights, with new decoding gates. Or slow the clock with another divide-by-2.
Can I build this with an Arduino instead?
Yes: a state machine in code, a variable for the current light, and millis() to time each phase, so the board can still read a pedestrian button while it waits. The flip-flop version shows what that code is doing underneath.
Related topics
- How to program a robot as a state machineOne state at a time, each with its own job and its own ways out.
- How a binary counter divides a clock by 2, 4, 8 and 16Flip-flops that toggle, chained: each one halves the clock, together they count.
- Logic gates explainedAND, OR, NOT, XOR and their bubbled twins: each one's rule is a truth table.
- How a D flip-flop worksCopy D to Q at the clock's rising edge, then hold it: one bit of memory.