TExcelTAD
Home › Control Systems & Automation › IEC 61131-3 Languages: Ladder, Structured Text and Function Blocks

IEC 61131-3 Languages: Ladder, Structured Text and Function Blocks

An electrician on night shift opens the PLC program of a water tank because the pump will not start. If the logic is drawn as a ladder of contacts and coils, they can follow it like a wiring diagram and see, live, which contact is open. The engineer who wrote the level-control maths preferred text, and the integrator who built the dry-run protection drew it from blocks. All three styles are standard: IEC 61131-3 defines the programming languages of PLCs so that programs, and people, can move between vendors. This lesson reads one small application, a tank-fill controller with dry-run protection, in ladder, structured text and function blocks, then runs it in a Python model of the scan cycle from PLCs and the Scan Cycle. No PLC runtime is available in the browser, so the PLC listings are shown, not executed; the Python simulation is the runnable part.

The standard in brief

IEC 61131-3 (fourth edition, May 2025; most tools still implement the third edition of 2013, which the fourth extends, e.g. with UTF-8 strings) defines:

Language Style Typical use
LD, ladder diagram graphical: contacts and coils between two power rails interlocks and discrete logic, read by electricians
FBD, function block diagram graphical: blocks wired by signals analog processing, PID, process control
ST, structured text textual, Pascal-like calculations, loops, state machines, data handling
SFC, sequential function chart graphical: steps and transitions sequences (start-up, batches, machine cycles)
IL, instruction list assembler-like deprecated in the third edition (2013) and no longer among the languages of the fourth (2025; its Annex B lists the removed and deprecated features); legacy code only

Programs are built from program organisation units: functions (no memory, like ABS or MAX), function blocks (with memory, like timers, counters and PID; each use is an instance with its own state) and programs. The standard library includes timers (TON on-delay, TOF off-delay, TP pulse), counters (CTU, CTD) and edge detectors (R_TRIG, F_TRIG); vendors add PID, motion and communication blocks.

The application

A tank is filled by a pump. Two level switches: Level_Low is TRUE below 20 cm20\ \text{cm}, Level_High is TRUE above 80 cm80\ \text{cm}. Requirements:

  1. The pump starts when the level is low and runs until the level is high (on/off control with hysteresis: Nonlinearities).
  2. If the pump runs for 5 s without the flow switch Flow_OK confirming flow, a dry-run alarm latches and stops the pump.
  3. The alarm is cleared only by an operator reset, and the pump then restarts only if the level is low.

Ladder diagram

text
     Level_Low                Level_High    Dry_Alarm          Pump
 |------| |------+-------------|/|-----------|/|---------------( )----|
 |               |                                                     |
 |     Pump      |                                                     |
 |------| |------+                                                     |
 |                                                                     |
 |      Pump       Flow_OK                      DryTimer               |
 |------| |---------|/|---------------------[ TON  IN    Q ]---        |
 |                                     T#5s-[      PT   ET ]           |
 |                                                                     |
 |   DryTimer.Q                                                Dry_Alarm|
 |------| |------+-----------------------------------------------(S)---|
 |   Reset_Btn                                                 Dry_Alarm|
 |------| |------------------------------------------------------(R)---|

| | is a normally-open contact (passes power when the variable is TRUE), |/| a normally-closed contact (passes when FALSE), ( ) a coil, (S)/(R) set and reset coils. The first rung is the seal-in pattern of the previous lesson: Pump holds itself on through the parallel branch until Level_High opens the circuit.

Structured text

text
PROGRAM TankFill
VAR_INPUT  Level_Low, Level_High, Flow_OK, Reset_Btn : BOOL; END_VAR
VAR_OUTPUT Pump : BOOL; END_VAR
VAR        Dry_Alarm : BOOL; DryTimer : TON; END_VAR

Pump := (Level_Low OR Pump) AND NOT Level_High AND NOT Dry_Alarm;

DryTimer(IN := Pump AND NOT Flow_OK, PT := T#5S);
IF DryTimer.Q THEN
    Dry_Alarm := TRUE;
END_IF;
IF Reset_Btn THEN
    Dry_Alarm := FALSE;
END_IF;
END_PROGRAM

Function block diagram

Level_Low Pump >=1 Level_High & Dry_Alarm & Pump Pump Flow_OK & TON IN PT Q ET T#5s SR S1 R Q1 Reset_Btn Dry_Alarm

The SR block is a set-dominant flip-flop: if S1 and R are both TRUE the output is set. In the ST and ladder versions above the reset is written after the set, so reset wins there. Such details differ between versions of "the same" logic and must be decided deliberately: here a held reset button should not be able to override an active dry-run condition forever, so set-dominant is the safer choice, and the ST would be reordered.

Running it: a scan-cycle simulation

The Python model executes the ST logic once per 100 ms100\ \text{ms} scan against a simulated tank (area 0.1 m20.1\ \text{m}^2, pump 0.5 L/s0.5\ \text{L/s}, constant draw-off 0.2 L/s0.2\ \text{L/s}). The TON timer is implemented exactly as a PLC does it: it accumulates scan times while its input is TRUE. At t=700 st = 700\ \text{s} the suction runs dry (the pump runs but Flow_OK stays FALSE); at t=760 st = 760\ \text{s} the operator fixes it and presses Reset.

python · try it yourself
class TON:
    """IEC on-delay timer, updated once per scan."""
    def __init__(self, pt):
        self.pt, self.et, self.q = pt, 0.0, False
    def __call__(self, IN, dt):
        self.et = min(self.et + dt, self.pt) if IN else 0.0
        self.q = IN and self.et >= self.pt
        return self.q

dt = 0.1                                      # scan time, s
level, pump, alarm = 0.50, False, False       # m
dry_timer = TON(5.0)
events, prev = [], (None, None)
for n in range(int(1000 / dt)):
    t = n * dt
    dry = 700 <= t < 760                      # suction blocked in this window
    # 1. read inputs (process image)
    Level_Low, Level_High = level < 0.20, level > 0.80
    Flow_OK = pump and not dry
    Reset_Btn = 760 <= t < 761
    # 2. execute the program (the ST above)
    pump = (Level_Low or pump) and not Level_High and not alarm
    if dry_timer(pump and not Flow_OK, dt):
        alarm = True
    if Reset_Btn:
        alarm = False
    # 3. write outputs -> process
    inflow = 0.5e-3 if Flow_OK else 0.0       # m^3/s
    level += dt * (inflow - 0.2e-3) / 0.1
    if (pump, alarm) != prev:
        events.append(f"t = {t:6.1f} s  level {100 * level:5.1f} cm  pump {'ON ' if pump else 'off'}  alarm {'SET' if alarm else '-'}")
        prev = (pump, alarm)
print("\n".join(events))
t =    0.0 s  level  50.0 cm  pump off  alarm -
t =  150.1 s  level  20.0 cm  pump ON   alarm -
t =  350.4 s  level  80.1 cm  pump off  alarm -
t =  650.8 s  level  20.0 cm  pump ON   alarm -
t =  705.0 s  level  33.7 cm  pump ON   alarm SET
t =  705.1 s  level  33.7 cm  pump off  alarm SET
t =  760.0 s  level  22.7 cm  pump off  alarm -
t =  773.6 s  level  20.0 cm  pump ON   alarm -
t =  973.9 s  level  80.1 cm  pump off  alarm -

Read the log against the requirements: the pump cycles between the two switches (fill 3 mm/s3\ \text{mm/s} net, drain 2 mm/s2\ \text{mm/s}), the dry-run alarm is set at 705.0 s705.0\ \text{s}, 5 s after the flow disappeared, and the pump drops out one scan later (the pump rung runs before the timer rung, so it sees the alarm in the next scan); after the reset it restarts only when the level is next low. A run like this, with the plant simulated, is how PLC programs are tested before commissioning ("virtual commissioning").

Sequences: SFC and state machines

Machines that go through steps (fill, heat, mix, drain) are written as SFC: steps with actions, transitions with conditions, exactly one active step in each branch. In ST the same thing is a CASE state machine:

text
CASE Step OF
  0: (* idle *)   IF Start THEN Step := 10; END_IF;
  10: (* fill *)  Valve_In := TRUE;  IF Level_High THEN Valve_In := FALSE; Step := 20; END_IF;
  20: (* heat *)  Heater := TRUE;    IF Temp >= 60.0 THEN Heater := FALSE; Step := 30; END_IF;
  30: (* drain *) Valve_Out := TRUE; IF Level_Low THEN Valve_Out := FALSE; Step := 0; END_IF;
END_CASE;

Each step's actions are tied to the step, so an output cannot stay on by accident when the sequence moves on, and the active step is visible on the HMI for diagnosis.

Guided lab: the tank logic on a microcontroller

Hand calculation. From 50 cm50\ \text{cm} the tank drains at 2 mm/s2\ \text{mm/s} to 20 cm20\ \text{cm} in 150 s150\ \text{s}. Each cycle then fills from 2020 to 80 cm80\ \text{cm} at 3 mm/s3\ \text{mm/s} net (200 s200\ \text{s}) and drains back at 2 mm/s2\ \text{mm/s} (300 s300\ \text{s}): 500 s500\ \text{s} per cycle.

Simulation. Change the draw-off to 0.4 L/s0.4\ \text{L/s} in the program and predict the new cycle before running it.

What you would measure. Build the logic on a USB-powered microcontroller board: three push-buttons for Level_Low, Level_High and Flow_OK (with pull-ups, read as active-low), one for Reset, and two LEDs for Pump and Alarm. Implement the TON from scan times as in the program. Press and hold the buttons to walk through the requirements and time the alarm with a stopwatch: 5 s5\ \text{s} plus at most one scan.

Hazards and safe setup. LEDs and buttons on a USB-powered board only. A real pump with water near mains electricity is outside this course; real level and pump control in plants follows the electrical and machinery-safety standards discussed in the next lesson.

Pitfalls

  • Writing the same coil in two rungs: the last one wins every scan.
  • Mixing set/reset coils and normal coils on one variable: hard to follow and easy to break.
  • Forgetting that a function block instance has memory: calling one TON instance for two purposes merges their timing.
  • Timers in tasks that do not run every scan (inside an IF): the timer is not updated and stops counting.

Practice

  1. In the ladder above, what happens if Level_Low and Level_High are both TRUE (a faulty switch)?
  2. Write the ST line for a lamp that is on when either of two doors is open (Door1_Closed, Door2_Closed are TRUE when closed).
Answer
  1. The pump stays off: NOT Level_High blocks the rung. A good program would also raise a "level switch implausible" alarm, because a tank cannot be both low and high.
  2. Lamp := NOT Door1_Closed OR NOT Door2_Closed;

Test yourself

Which IEC 61131-3 language was deprecated in the third edition (2013) and is no longer listed in the fourth (2025)?

  1. Structured Text
  2. Ladder Diagram
  3. Instruction List
  4. Sequential Function Chart

A TON with PT := T#5S sets its output Q

  1. 5 s after its input becomes FALSE
  2. when its input has been TRUE continuously for 5 s
  3. for one 5 s pulse after a rising edge
  4. every 5 s

In ladder logic, a contact drawn |/| passes power when its variable is

  1. TRUE
  2. FALSE
  3. changing
  4. an analog value above zero

Which language suits a fill-heat-drain machine sequence best?

  1. Instruction List
  2. Sequential Function Chart (or a CASE state machine in ST)
  3. a single ladder rung
  4. a PID block