Learn PLCs free
Beginner30 minutesTimersState machineSequential control

Traffic Light PLC Program

The traffic light is the canonical PLC beginner project. It introduces state machines, timer-based transitions, and the discipline of mutually-exclusive output groups — green-N-S must NEVER be on at the same time as green-E-W.

Sequence overview showing inputs flowing into PLC logic and outputs driving actuators, with this example named Traffic Light PLC Program.Process flow: inputs → logic → outputsInputsResetBtn1 signalsPLC LogicTimers · State machineBeginner · 30 minutesOutputsGreen_NS, Yellow_NS, Red_NS6 signals

I/O list

TagTypeDescription
Green_NSDOGreen for N/S direction
Yellow_NSDOYellow for N/S
Red_NSDORed for N/S
Green_EWDOGreen for E/W
Yellow_EWDOYellow for E/W
Red_EWDORed for E/W
ResetBtnDIReset to initial state

Structured Text code

PROGRAM main
VAR
    Step      : INT := 0;
    PhaseTimer: TON;
    PhaseTime : TIME;
END_VAR

(* All outputs default off *)
Green_NS  := FALSE; Yellow_NS := FALSE; Red_NS := FALSE;
Green_EW  := FALSE; Yellow_EW := FALSE; Red_EW := FALSE;

IF ResetBtn THEN Step := 0; END_IF;

CASE Step OF
    0:  (* N-S green, E-W red — 30 sec *)
        Green_NS := TRUE; Red_EW := TRUE;
        PhaseTime := T#30s;
        IF PhaseTimer.Q THEN Step := 1; END_IF;

    1:  (* N-S yellow, E-W red — 5 sec *)
        Yellow_NS := TRUE; Red_EW := TRUE;
        PhaseTime := T#5s;
        IF PhaseTimer.Q THEN Step := 2; END_IF;

    2:  (* All red — 2 sec safety interval *)
        Red_NS := TRUE; Red_EW := TRUE;
        PhaseTime := T#2s;
        IF PhaseTimer.Q THEN Step := 3; END_IF;

    3:  (* E-W green, N-S red — 30 sec *)
        Red_NS := TRUE; Green_EW := TRUE;
        PhaseTime := T#30s;
        IF PhaseTimer.Q THEN Step := 4; END_IF;

    4:  (* E-W yellow, N-S red — 5 sec *)
        Red_NS := TRUE; Yellow_EW := TRUE;
        PhaseTime := T#5s;
        IF PhaseTimer.Q THEN Step := 5; END_IF;

    5:  (* All red — 2 sec *)
        Red_NS := TRUE; Red_EW := TRUE;
        PhaseTime := T#2s;
        IF PhaseTimer.Q THEN Step := 0; END_IF;
END_CASE;

(* Restart timer on every step transition *)
PhaseTimer(IN := TRUE, PT := PhaseTime);
IF PhaseTimer.Q THEN PhaseTimer(IN := FALSE); END_IF;

How it works

Step 0: N-S green for 30 seconds while E-W is red. Cars going north or south can proceed.

Step 1: N-S yellow for 5 seconds, E-W still red. Cars in N-S direction must clear the intersection.

Step 2: All-red interval for 2 seconds. Critical safety phase that prevents collisions when transitioning between directions.

Steps 3-5: Mirror image — E-W green, yellow, all-red.

Cycle repeats by jumping back to step 0.

More examples