Skip to content

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:

RED = FF4 Q̄, AMBER = FF3 XOR FF4, GREEN = FF3 AND FF4
Ring (FF2 FF3 FF4)REDAMBERGREEN
000on
100on
110onon
111on
011on
001on

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.

Try it yourself: free lesson

“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

PartHow manyJob
D flip-flop4One divider, three in the ring
XOR1Amber
AND1Green

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.

Try it yourself: free lesson

“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 needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3The ring's last two flip-flops, FF3 and FF4, are both 1. Which lights are on?
    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.
  2. Question 2 of 3The lamps read S1 off, S2 on, S3 on. Next tick they read…
    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.
  3. Question 3 of 3A toggling flip-flop is fed a clock of 8 Hz. Its Q blinks at…
    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.
Keep practising: free lesson

“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.

Every topic