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.
I/O list
| Tag | Type | Description |
|---|---|---|
| Green_NS | DO | Green for N/S direction |
| Yellow_NS | DO | Yellow for N/S |
| Red_NS | DO | Red for N/S |
| Green_EW | DO | Green for E/W |
| Yellow_EW | DO | Yellow for E/W |
| Red_EW | DO | Red for E/W |
| ResetBtn | DI | Reset 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
Water Tank Level Control
Two-pump water tank with high/low level sensors, alternating pumps for even wear, high-high alarm trip
Conveyor with Photoeye Sort
Conveyor running at constant speed sorting parts by photoeye into two reject bins
HVAC Zone Temperature Control
Single-zone temperature control with PID heater output, thermostat dead-band, and occupied/unoccupied scheduling
Motor Reversing with Interlock
Bidirectional motor with start/stop buttons for forward and reverse, with software interlock preventing both contactors energising simultaneously