Skip to content
Structured TextRuns without an account · Updated

Traffic light sequencer in Structured Text

A traffic light is a sequence with no inputs: green, amber, red, repeat. This example builds it from three on-delay timers chained into a ring, where each timer starts when the one before it finishes.

Run it
The traffic light example in Structured Text, running in PLCflow Sim. The green timer is timing and the green lamp is on.
Traffic light sequencer, running in the PLCflow Sim workspace.

The program

// Free-running traffic light: three timers chained into a ring, each one
// armed by the previous one finishing. The last timer resets the first,
// so the sequence cycles forever with no input at all — press Run and watch.
TON(GreenTimer, IN := NOT RedTimer.DN, PT := T#6s);
TON(AmberTimer, IN := GreenTimer.DN, PT := T#2s);
TON(RedTimer, IN := AmberTimer.DN, PT := T#6s);

Green := NOT GreenTimer.DN;
Amber := GreenTimer.DN AND NOT AmberTimer.DN;
Red := AmberTimer.DN AND NOT RedTimer.DN;

How it works

GreenTimer runs whenever RedTimer is not done. At start-up nothing is done, so GreenTimer starts timing its 6 seconds.

When GreenTimer finishes, its DN bit starts AmberTimer (2 seconds). When AmberTimer finishes, it starts RedTimer (6 seconds).

When RedTimer finishes, NOT RedTimer.DN goes false, so GreenTimer's input drops and it resets. That clears GreenTimer.DN, which resets AmberTimer, which resets RedTimer. On the next scan RedTimer.DN is false again and GreenTimer starts over. The ring has closed.

The three lamp lines decode which stage the ring is in. Green is on until GreenTimer is done, Amber between GreenTimer and AmberTimer finishing, Red between AmberTimer and RedTimer finishing. One full cycle takes 14 seconds.

The timers use named parameters (IN, PT) and T# duration literals, as IEC 61131-3 Structured Text does.

Try this

In the simulator, an input changes when you click it in the tag list: click once to turn it on and again to turn it off. To press a button, turn it on; to release it, turn it off; to pulse it, do both.

  1. Press Run and watch GreenTimer.ACC, AmberTimer.ACC and RedTimer.ACC take turns climbing.
  2. Change the amber time to T#4s and run it again.
  3. Add a Walk lamp that lights only during red.
Run it

Tags

TAGROLE
GreenTimerTON, 6 s. Times the green phase.
AmberTimerTON, 2 s. Times the amber phase.
RedTimerTON, 6 s. Times the red phase and restarts the ring.
Green, Amber, RedBOOL lamps decoded from the timers.

More examples

Tank level alarms

Structured Text

Level alarms with hysteresis, in Structured Text. Each alarm sets at one level and clears at another.

LevelHighAlarmFillValve

Conveyor with dwell

Ladder (NT)

The belt runs, a 5-second timer gives it time to get up to speed, and only then does the ready lamp light. Watch the accumulator climb.

RunRequestDwellTimer.ACCReadyLamp

Read more about the Structured Text simulator, or see all examples.