PLC Programming

Practical guides on writing, structuring, and debugging PLC programs — from your first ladder logic rung to reusable function-block libraries.

What this section covers

  • The five IEC 61131-3 programming languages and when to use each
  • Ladder logic contacts, coils, timers, counters, and common instruction patterns
  • Structured Text syntax for calculations, loops, and complex logic
  • Function blocks, data types, and program organisation units (POUs)
  • PLC scan cycle, memory types, and common pitfalls
  • Platform-specific notes for Allen-Bradley (Studio 5000/CCW), Siemens (TIA Portal), Mitsubishi, and CODESYS-based controllers

Articles in this section

Featured Products

What is a PLC?

A programmable logic controller (PLC) is a ruggedised industrial computer designed to control machines and processes in real time. Unlike a desktop PC, a PLC is built to tolerate the electrical noise, temperature extremes, and vibration of a factory floor. It runs a fixed program in a cyclic scan: read inputs, execute program logic, write outputs, repeat — typically completing each cycle in 1 to 50 milliseconds.

PLCs replaced relay-based control panels in the late 1960s and early 1970s, initially programmed in ladder logic specifically to allow relay-trained electricians to read the programs without learning conventional software development. Today's PLC platforms offer five standardised programming languages, modern debugging tools, and integration with Ethernet networks and cloud systems — but the fundamental scan-cycle architecture has not changed.

The IEC 61131-3 programming languages

IEC 61131-3 is the international standard that defines five programming languages for PLCs and other industrial controllers. Its goal is to provide a vendor-neutral programming model so that logic written for one compliant platform can be ported to another with minimal changes.

Ladder Diagram (LD)

Ladder Diagram (LD) — usually just called "ladder logic" — is the most widely used PLC language in North America. Programs are arranged as horizontal rungs between two vertical power rails, mimicking the appearance of a relay control drawing. Each rung consists of input conditions (contacts) on the left and an output element (coil or instruction block) on the right. When all contacts in series evaluate to true, current notionally flows through the rung and the output is energised.

Ladder logic is well-suited to discrete logic: interlocks, start/stop sequences, alarm conditions, and sequential machine control. It is easy to read on-screen in monitoring mode because contacts highlight as they conduct, giving a live visual of logic state. Its main limitation is handling complex mathematics, string manipulation, or data structure access — for these, Structured Text is cleaner.

Structured Text (ST)

Structured Text is a high-level language resembling Pascal, with IF/THEN/ELSE, CASE, FOR, WHILE, and REPEAT constructs. It is ideal for calculations, recipe management, data array processing, and any logic that would require dozens of rungs in ladder. Most modern PLC platforms support Structured Text alongside ladder within the same program, so engineers can use each language where it is strongest.

Function Block Diagram (FBD)

Function Block Diagram represents logic as graphical blocks with input and output pins wired together. Data flows left to right through connected blocks. FBD is popular in process control for PID loops, valve sequencing, and analogue signal conditioning, because the data-flow representation maps naturally to process flow diagrams.

Sequential Function Chart (SFC)

Sequential Function Chart describes a process as a sequence of steps connected by transitions. Only active steps execute; a transition fires when its boolean condition becomes true, deactivating the current step and activating the next. SFC is the natural choice for batch processes, machine cycles, and any application with a defined sequence of states — it makes the machine's operating modes explicit and easy to validate.

Instruction List (IL) — deprecated

Instruction List is a low-level assembly-like language included in IEC 61131-3 editions 1 and 2. It was deprecated in edition 3 (2013) and is rarely encountered in new projects. Legacy programs in older Siemens Statement List (STL) and Mitsubishi mnemonic formats share similarities with IL.

The PLC scan cycle in detail

01 02 03 04 Read Inputs Execute Program Write Outputs Housekeeping Input image table Rung evaluation Output image table Comms · Diag Repeat (1–50 ms typical)
The four-phase PLC scan cycle. Input changes that occur during execution are not seen until the next scan.

Understanding the scan cycle is essential for writing correct PLC programs, because the order in which the CPU reads inputs and writes outputs affects program behaviour.

At the start of each scan, the CPU reads all physical input terminals and copies their states into the input image table (also called the process image input, or PII). The user program then executes from the first rung to the last, reading from the input image table and writing results to the output image table (PIQ). At the end of the scan, the CPU copies the output image table to the physical output terminals. Finally, the CPU performs housekeeping tasks — communication with I/O modules, network updates, and self-diagnostics — before starting the next scan.

A key consequence of this architecture is that an input change that occurs during program execution is not seen until the next scan. For high-speed events (encoder pulses, high-speed counters), dedicated hardware counters or interrupt-driven tasks are needed rather than the main cyclic scan.

Program organisation units (POUs)

IEC 61131-3 defines three types of program organisation unit:

  • Program (PROG): The top-level execution unit called by the task scheduler. A program can call function blocks and functions.
  • Function Block (FB): A reusable POU with internal memory that retains state between calls. Each call creates an instance with its own data. Used for PID controllers, motor starters, counters, timers — anything that needs to remember state.
  • Function (FC): A POU without internal memory; all inputs must be supplied on each call. Used for pure calculations and conversions (e.g., engineering-unit scaling, CRC calculations).

Good program structure uses FBs to encapsulate each machine element (motor, valve, conveyor zone) and calls them from a higher-level program that handles sequencing. This mirrors object-oriented design principles and makes programs easier to maintain and commission.

Data types and tag databases

PLCs use typed variables (tags) to store all programme data. The most common base data types are:

  • BOOL: A single bit, true (1) or false (0). Used for discrete I/O, status flags, and control bits.
  • INT / DINT / LINT: 16-bit, 32-bit, and 64-bit signed integers. Used for counts, positions, and raw ADC values.
  • REAL / LREAL: 32-bit and 64-bit floating-point numbers. Used for engineering-unit values, PID parameters, and analogue scaling.
  • TIME: A time duration (e.g., T#5s). The data type for timer preset values.
  • STRING: A sequence of characters. Used for recipe names, alarm messages, and communication data.

User-Defined Data Types (UDTs) let you group related variables into a named structure. A MotorStatus UDT might contain Running (BOOL), Faulted (BOOL), Speed (REAL), and RunHours (DINT). You then create one instance of the UDT per motor, giving a consistent, self-documenting tag structure.

Timers

Timers are among the most-used instructions in ladder logic. IEC 61131-3 defines three standard timer function blocks:

  • TON (On-Delay Timer): Output Q becomes true a set time (PT) after input IN is energised. Resets immediately when IN de-energises. Use for start delays, filter debounce, and timed sequences.
  • TOF (Off-Delay Timer): Output Q becomes true immediately when IN is energised, and stays true for PT after IN de-energises. Use for post-run cooling fans, seal-in delays.
  • TP (Pulse Timer): Output Q activates for exactly PT on each rising edge of IN, regardless of IN state. Use for fixed-duration outputs, alarm pulses.

Counters

Counter function blocks track events:

  • CTU (Count Up): Increments CV on each rising edge of CU input. Output Q becomes true when CV ≥ PV (preset value). Reset input R resets CV to 0.
  • CTD (Count Down): Decrements CV on each rising edge of CD input. Output Q becomes true when CV ≤ 0. Load input LD loads CV with PV.
  • CTUD (Up/Down Counter): Combines both; QU activates when CV ≥ PV, QD activates when CV ≤ 0.

Common programming patterns

Self-holding (seal-in) circuit

The most fundamental ladder logic pattern: a Start contact in series with a Stop contact drives an Output coil, with a parallel contact on the Output providing memory so the output stays on after Start is released. This is the direct software equivalent of the classic relay start/stop circuit.

One-shot (rising-edge) detection

A one-shot contact (P or OSR instruction depending on platform) passes power for exactly one scan on the rising edge of a boolean. Use this when you need to trigger an event once on a state change rather than continuously while the state is active — for example, incrementing a counter or writing a value to a register on button press.

Exclusive-or interlock

To prevent two outputs from being simultaneously active (e.g., forward and reverse contactors), place a normally-closed contact from each output coil in series on the rung that drives the other. The hardware also needs a mechanical interlock between the contactors, but the software interlock provides a first line of protection.

Platform notes

Allen-Bradley (Rockwell Automation)

Studio 5000 Logix Designer is the programming environment for ControlLogix, CompactLogix, and Micro800 series PLCs. It uses a tag-based (rather than address-based) system, making programs highly readable. RSLogix 500 is the legacy environment for the SLC 500 and MicroLogix families, still common in installed base. Allen-Bradley uses its own instruction set (AOIs — Add-On Instructions are the AB equivalent of function blocks) and supports Ladder, FBD, Structured Text, and Sequential Function Chart within the same project.

Siemens

TIA Portal is the unified engineering environment for the S7-1200, S7-1500, S7-300, and S7-400 families. Siemens uses "Organisation Blocks" (OBs) as the top-level execution context, Function Blocks (FBs) with Instance Data Blocks, and Functions (FCs) without memory. The S7 instruction set includes legacy addresses (I, Q, M, DB) alongside the tag-based approach introduced in TIA Portal V13. All five IEC 61131-3 languages are supported.

CODESYS-based controllers

CODESYS is an IEC 61131-3 development environment used as the runtime by Beckhoff TwinCAT, Wago, Schneider Electric Modicon (partial), Pilz, and many others. It has a large library ecosystem and supports IEC 61131-3 most completely among major platforms. If portability across vendors matters, CODESYS-based platforms have the most compatible code.