PLC Book Part II — Ladder Logic Programming Essentials Chapter 5
Chapter 5 Part II · Ladder Logic Programming Essentials Intermediate ⏱ 50 min read ✦ 8 PLC Programs

05

Basics of PLC Programming

Welcome to Part II — this is where we move from understanding a PLC to programming one. We’ll see how the CPU organises its memory, how the scan cycle actually executes your code, and which programming language to use when. Then we’ll write eight ladder programs together — including the seal-in latch, the most important pattern in industrial control.

What you’ll be able to do after this chapter

Your goals for this chapter:

  • Describe how a PLC’s memory is split between the program file and the data file, and what each one stores.
  • Walk through the four steps of the program scan cycle and estimate scan time.
  • Name the five IEC 61131-3 programming languages and pick the right one for a given problem.
  • Read and write instruction addresses for both fixed (e.g. I:1/0) and tag-based (e.g. Start_PB) PLC platforms.
  • Build ladder rungs that use bit-level instructions, branch instructions, and internal relays.
  • Explain the difference between Run, Program, and Test modes and when to use each.
  • Wire a 4–20 mA analog input and use a comparator instruction to drive a digital output.
  • Write the seal-in latch rung — the cornerstone of motor start/stop control — from memory.

Key Concepts & Terms

Program fileData fileI/O image table Program scanScan time Ladder Diagram (LD)Function Block Diagram (FBD)Structured Text (ST)Instruction List (IL)Sequential Function Chart (SFC) IEC 61131-3 XIC · XIO · OTEOTL · OTU Instruction addressing Branch instruction Internal relay Seal-in / latch Run · Program · Test mode Analog input4–20 mA loopGEQ comparator
Section 5.1

Processor Memory Organization — Program Files vs Data Files

Inside every PLC’s CPU there are two big bookshelves. One holds the program — your ladder rungs, structured text routines, and configuration. The other holds the data — every input value, output value, timer count, and internal flag the program uses while it runs. Understanding this split is the foundation of every concept that follows.

Program Files — Where Your Code Lives

The program file is read-only while the PLC is running. It’s the recipe. It contains:

  • The main routine — the rungs that always execute, top to bottom, every scan.
  • Subroutines — blocks of code called by the main routine when needed.
  • Interrupt routines — code that runs only when triggered by an event (high-speed counter overflow, fault, etc.).
  • Configuration — chassis layout, I/O module types, communication settings.

Data Files — Where the Numbers Live

The data file is the working memory. Every variable your program touches — every input bit, every output bit, every timer accumulator — sits here and gets read or written every scan. Older platforms (Allen-Bradley SLC 500, Siemens S7-300) split the data file into numbered tables:

FileLetterHoldsExample address
0OOutput image tableO:2/0
1IInput image tableI:1/0
2SStatusS:1
3BBit (internal relays)B3:0/0
4TTimerT4:0.DN
5CCounterC5:0.ACC
6RControl (for shift registers, sequencers)R6:0
7NIntegerN7:0
8FFloating pointF8:0

Modern platforms (Allen-Bradley ControlLogix, Siemens TIA Portal, Schneider M340) replace these numbered files with tags — friendly names like Start_PB, Tank_Level, Motor_Run. The underlying memory still has all the same regions, but now you address them by name instead of file/element/bit. Tag-based addressing is more readable, so we’ll use both styles in this chapter so you recognise either when you meet it on the job.

PLC PROCESSOR MEMORY ORGANISATION The CPU stores your code on one side, your data on the other. CPU MEMORY 📘 PROGRAM FILE your code — read-only at runtime PF 2 — Main routine (always runs) PF 3 — Subroutine (Start sequence) PF 4 — Subroutine (Fault handler) PF 5 — Interrupt (high-speed count) PF 0 — System / config 📊 DATA FILE your variables — read & written every scan DF 0 · O — Outputs DF 1 · I — Inputs DF 2 · S — Status DF 3 · B — Bits DF 4 · T — Timers DF 5 · C — Counters DF 6 · R — Control DF 7 · N — Integers DF 8 · F — Floats DF 9+ · User defined addresses: I:1/0 · O:2/3 · B3:0/5 · T4:0.DN

Figure 5.1 — How the CPU Organises MemoryThe program file holds your code. The data file holds the live values your code reads and writes during every scan.

Real-world analogy

Think of a chef in a busy kitchen. The recipe book on the shelf is the program file — you don’t change it during service, you just follow it. The cutting board with chopped onions, garlic, and salt is the data file — it changes constantly as the dishes are made. The PLC works the same way: the recipe stays the same, the ingredients change every scan.

Section 5.2

The Program Scan — How Your Code Actually Runs

You met the program scan briefly in Chapter 1. Now we need the details, because every quirk of PLC behaviour traces back to it.

The PLC repeats four steps over and over, thousands of times per second:

  1. Input scan. The CPU reads every input terminal and copies the result into the input image table (the I file). At this moment, the program freezes a snapshot of the field — every contact, every sensor — and from now on, the rungs only see this snapshot, not the live wires.
  2. Program execution. The CPU runs the rungs one at a time, top to bottom, left to right. Each rung’s logic is evaluated against the snapshot taken in step 1. Outputs that should be ON are written to the output image table (not yet to the actual terminals).
  3. Output update. The CPU copies the entire output image table out to the physical output terminals. All outputs change at once, atomically.
  4. Housekeeping. The CPU services communications (engineering laptop, HMI, network), runs diagnostics, then loops back to step 1.

The total time for one full loop is the scan time. For a typical small program it’s 1–10 milliseconds. For a large process it might be 30–50 ms. Faster scan = faster reaction to inputs.

Why the snapshot matters

Because the program runs against an input snapshot, an input that changes during step 2 won’t be seen until the next scan. This is why a button pressed for less than one scan time can be missed entirely. It’s also why the order of rungs almost never matters logically — they all see the same snapshot — although it can affect timing by one or two scans.

Estimating Scan Time

A rough rule of thumb: figure 1 µs per simple rung instruction on a modern CPU, plus the I/O update overhead (typically 1–3 ms for a small chassis). So a 500-instruction program lands around 1.5–4 ms. Always check the actual scan time in the CPU status word — it’s usually displayed live in the programming software.

Section 5.3

PLC Programming Languages — The IEC 61131-3 Standard

Every major PLC vendor today supports the same five programming languages defined by the international standard IEC 61131-3. You don’t have to use all five — most engineers stick to one or two — but you should at least be able to read each.

THE FIVE IEC 61131-3 LANGUAGES Same job — five different ways to write it. LD — Ladder Diagram Most popular. Looks like relay schematic. A B Y FBD — Function Block Diagram Like a wiring schematic with logic gates. A B AND Y ST — Structured Text High-level, like Pascal/Python. IF A AND B THEN Y := TRUE; ELSE Y := FALSE; IL — Instruction List Assembly-style. Compact but rare today. LD A AND B ST Y SFC — Sequential Function Chart For step-by-step state machines. Step 1 A Step 2

Figure 5.2 — Five Languages, One StandardSame logic (“Y is on if A AND B are on”) expressed in each. Most factories use Ladder for everyday rungs, with Structured Text or FBD reserved for the harder maths.

LanguageBest forTrade-off
LD — Ladder DiagramBit-level relay logic, machine control, anything an electrician will troubleshootVerbose for arithmetic and string handling
FBD — Function Block DiagramProcess control, PID loops, signal flowLess compact than ST for complex maths
ST — Structured TextCalculations, loops, data manipulation, custom function blocksLooks like code — harder for a non-programmer to read
IL — Instruction ListLegacy small PLCs (deprecated in IEC 61131-3 third edition)Cryptic; almost gone in new projects
SFC — Sequential Function ChartState machines, batch processes with explicit step-by-step sequencesOverkill for purely combinational logic

Why does ladder logic still dominate? Because the people who maintain factory equipment at 2 a.m. on a Saturday — electricians and technicians, not software engineers — were trained on relay schematics. Ladder is the language they can read, debug, and force-bit their way through. We’ll spend the rest of Part II in ladder for exactly that reason.

Section 5.4

Bit-Level Logic Instructions — XIC, XIO, OTE, OTL, OTU

You met three bit-level instructions in Chapter 4: XIC, XIO, and OTE. Let’s add two more that complete the toolkit for everyday motor and valve control: OTL and OTU.

InstructionSymbolWhat it doesMemory
XIC — Examine If Closed—| |—Passes power if the bit is 1Stateless
XIO — Examine If Open—|/|—Passes power if the bit is 0Stateless
OTE — Output Energize—( )—Sets the bit to match the rung’s state every scanStateless
OTL — Output Latch—(L)—Sets the bit to 1 and keeps it set even after the rung goes falseRetentive
OTU — Output Unlatch—(U)—Resets the bit to 0 and keeps it clearedRetentive

The crucial difference between OTE and OTL/OTU: an OTE coil follows its rung — turns on when the rung is true, off the moment it goes false. An OTL coil remembers — once set, it stays set until something else explicitly unlatches it. We’ll see the practical difference in worked program 5 below.

A way to remember

OTE is like pressing and holding a doorbell — the bell rings only while you press. OTL is like flipping a wall switch — push it once, and the light stays on until you push it again to turn it off. OTU is the “off” push of that wall switch. Both are needed for things that should latch: motor running, alarm acknowledged, batch step complete.

Section 5.5

Instruction Addressing — Where in Memory Are We Pointing?

An instruction without an address is meaningless. Every XIC, XIO, OTE etc. needs a pointer to a specific bit in the data file. Different vendors use different addressing styles, and you’ll meet at least two on the job.

Style 1 — File / Element / Bit (Allen-Bradley SLC, MicroLogix, Siemens S7)

Format: FileLetter:Element/Bit   — examples: I:1/0   O:2/3   B3:0/5   T4:0.DN.

  • I:1/0 — input file, slot 1, bit 0. The first input on the module in slot 1.
  • O:2/3 — output file, slot 2, bit 3. The fourth output on slot 2’s module.
  • B3:0/5 — bit file 3, word 0, bit 5. An internal flag.
  • T4:0.DN — timer file 4, timer 0, the “Done” bit (DN). True when the timer reaches its preset.

Style 2 — Tag-Based (ControlLogix, TIA Portal, Codesys)

Format: just a friendly name. Examples: Start_PB, Tank_Level, Motor_Run, Mixer_Timer.DN.

The hardware mapping is done once in a tag database — after that, you write code with words. Tag-based addressing is what you’ll encounter in modern installations. The principle is the same: the instruction reads or writes a single bit in CPU memory.

Both styles in this book

Throughout this chapter and the next, we’ll use Allen-Bradley file-style addresses (I:1/0, O:2/0) because they make the slot/bit relationship visible — useful while you’re learning. From Chapter 9 onwards, we’ll switch to tag names for the more complex programs. The logic doesn’t change either way.

Section 5.6

Branch Instructions — Adding Parallel Paths to a Rung

You used a parallel branch for the OR gate in Chapter 4. Let’s name what we did. A branch instruction adds an alternative path between two points on a rung. Power flowing into the start of the branch can take any branch arm to reach the end.

  • Parallel branch — two or more contacts wired in parallel. Acts like an OR.
  • Nested branch — a branch inside a branch. Used to express logic like (A·B) + (C·D·E).
  • Branch limit — most platforms cap branches at 7–8 levels deep and 5–6 wide. If you exceed it, the editor refuses to compile.

A branch is just a wire — it doesn’t consume CPU time the way an instruction does. So break a complex rung into multiple parallel branches whenever it makes the logic clearer. Don’t try to be clever and pack everything into one impossibly nested rung.

Section 5.7

Internal Relays — The PLC’s “Scratch Paper”

Sometimes a logic condition is shared by many rungs. Instead of repeating the same combination of XIC and XIO contacts everywhere, you compute it once, store the answer in an internal relay bit, and then reference that one bit later.

Internal relays are bits in the B (bit) file — they’re not connected to any field device. You can think of them as software-only equivalents of an old control panel’s “auxiliary relays”. Allen-Bradley calls them B-bits; Siemens calls them M-bits (“memory”); ControlLogix calls them BOOL tags.

When to reach for an internal relay

Whenever you find yourself writing the same group of contacts twice, that group should be promoted to an internal relay. For example, if “machine is permitted to run” depends on five conditions and you need that fact in three rungs, compute it once into B3:0/0 (call it Machine_Permit) and use a single XIC of that bit in the other rungs. Cleaner code, easier to change.

Section 5.8

Examine If Closed vs. Examine If Open — A Word About Naming

Here’s a point that confuses every student exactly once: the names Examine If Closed (XIC) and Examine If Open (XIO) have nothing to do with the physical state of a switch in the field. They refer only to whether the bit in memory is a 1 or a 0.

  • XIC = “examine if the bit is a 1” (a normally-open contact in the analogy — it’s “closed” by a 1, allowing power through).
  • XIO = “examine if the bit is a 0” (a normally-closed contact — it’s “open” by a 1, blocking power).

The actual field switch could be wired normally-open or normally-closed — that’s an electrical decision made by the panel builder. Inside the program, you choose XIC or XIO based on what bit value you want to test for, regardless of the field wiring. The panel wiring documents the physical behaviour; the program logic uses the bit value.

Important Convention

Stop buttons should always be wired NC

  • A stop button is wired normally closed in the field, so it presents a 1 to the input when not pressed and a 0 when pressed (or when its wire is cut).
  • In the program, you test for the 1 with an XIC. If the wire breaks, the input becomes 0, the XIC blocks, and the motor stops — the safe direction.
  • Wiring stop buttons NO + testing with XIO would do the opposite: a broken wire keeps the motor running. Don’t do that.
Section 5.9

Entering Ladder Diagrams — Working in the Editor

You write your ladder logic on a laptop running the vendor’s programming software (Studio 5000, TIA Portal, Codesys, GX Works2 — same idea, different brands). The editor offers a graphical canvas: drop a contact onto a rung, drop a coil at the right end, label them with addresses, save.

Two essential features to know:

  • Tag database / address symbol table. A separate window where you list every tag and its description. Once you’ve defined Start_PB as I:1/0, you can reference it by name in every rung.
  • Online vs offline editing. Offline = you edit while disconnected from the PLC, then download the whole project. Online = you connect, view rungs executing live (energised paths shown in green), and even make small edits without stopping the process. Online edits are powerful and dangerous — change a wrong rung, the machine moves.
Section 5.10

Modes of Operation — Run, Program, and Test

Every PLC CPU has a key switch (or software equivalent) that selects one of three modes:

ModeWhat happensWhen to use it
RUNProgram scans, inputs are read, outputs are written. The machine operates.Normal production.
PROG (Program)Program does not scan. Outputs are de-energised. Editor lets you download a new program.Initial download, major edits, before opening the panel.
TEST (Remote Test)Program scans normally but physical outputs are inhibited — the output image table updates, but the modules don’t fire.Verifying logic without moving any actuators.

A fourth concept you’ll meet: force mode. While in RUN, you can force a specific input or output to a value, overriding the actual hardware. Forces are a brilliant troubleshooting tool but also a famous cause of accidents — a forced output stays forced even when the laptop disconnects, until someone explicitly removes the force. Always check the force list before walking away from a CPU.

Safety Note

Switching modes affects safety circuits

  • Switching the CPU from RUN to PROG de-energises every output the PLC controls. If those outputs include “valve closed” solenoids on a hot process, the valves spring open. Always plan mode changes against the process state.
  • Test mode is safer — outputs stay de-energised but the program runs and you can see which rungs energise. Use Test for verification, not Run.
Section 5.11

Connecting Analog Devices — Beyond ON/OFF

Up to now we’ve worked with discrete inputs and outputs — bits that are either 1 or 0. But many factory signals aren’t on/off: a tank level, a temperature, a flow rate, a motor speed. These are analog — continuous values that need an analog I/O module.

The 4–20 mA Current Loop

The most common industrial analog signal is the 4–20 mA current loop. The transmitter (sensor) sends a current proportional to its measurement: 4 mA at the bottom of the range (e.g. 0 °C), 20 mA at the top (e.g. 100 °C). The PLC’s analog input module measures the current and converts it into a 16-bit number.

Why current and not voltage? Because the current is the same all the way down the wire — even if the wire is 500 metres long with some resistance, you still read the right value. A voltage signal would lose accuracy over distance. Bonus: 4 mA at the low end (instead of 0) lets you detect a broken wire — if the reading drops to 0 mA, the loop is open.

Scaling Raw Counts to Engineering Units

Inside the PLC, the analog module presents the reading as a raw integer — typically 0 to 32 767 across the input range. Your program needs to convert this to engineering units (°C, kPa, litres, etc.) using a scale instruction or simple linear maths:

Worked Calculation

Convert raw analog counts to °C

Sensor range: 0 °C to 100 °C (4–20 mA) Module range: raw 0 to 32767 counts (4–20 mA) Linear scaling: °C = (raw / 32767) × 100 Example: raw = 16384 (the middle of the range) °C = (16384 / 32767) × 100 = 0.500 × 100 °C ≈ 50.0 °C Most platforms have an SCP or SCL instruction that does this in one block — give it the raw min/max, the scaled min/max, the input, and the output.

Comparator Instructions — Acting on Analog Values

Once you have a scaled value, you can act on it with a comparator instruction. The four most common are:

  • EQU — Equal. Passes power if Source A = Source B.
  • NEQ — Not Equal.
  • GEQ — Greater Than or Equal. Passes power if A ≥ B.
  • LEQ — Less Than or Equal.

A comparator looks like a small box on the rung with two inputs (the two values to compare) and acts as a “smart contact” — passing or blocking power depending on the result. Worked program 8 below uses GEQ to start a cooling fan when temperature exceeds 60 °C.

Worked PLC Programs

Eight Ladder Programs — From “Hello, World” to a Real Motor Starter

Time to put theory into practice. We’ll start with the simplest possible rung and build up to the seal-in latch — the pattern you’ll find in literally every motor starter circuit in industry. Each program shows the inputs, outputs, ladder diagram, and a “what we learned” takeaway.

01

PLC Program · “Hello, ladder”

Single Input Drives a Single Output

XIC + OTE

The problem: a momentary push-button on the panel should turn a pilot lamp on while pressed and off when released. Simplest possible PLC program — useful for verifying your wiring before anything more complex.

Inputs & Outputs

INPUT

I:1/0 — push-button (Tag: Test_PB)

OUTPUT

O:2/0 — pilot lamp (Tag: Lamp)

Ladder Diagram

L1 L2 000 I:1/0 XIC O:2/0 OTE

One XIC, one OTE. O:2/0 = I:1/0 — output mirrors input.

How it works

When the button is pressed, input bit I:1/0 goes to 1. The XIC sees a 1 and passes power. The OTE sets O:2/0 to 1 and the lamp lights. Release the button, the bit goes back to 0, the OTE drops the output, and the lamp turns off. There is no memory — output follows input exactly.

What we learned: the basic “rung” — left rail to right rail, contacts on the left, coil on the right. Always check this works for any new I/O before writing more complex logic.
02

PLC Program · Permissive logic

Machine Won’t Start Without Both Conditions

AND

The problem: a small drilling press should run only when the operator presses the START push-button and the safety guard is closed. Releasing either condition stops the press immediately.

Inputs & Outputs

INPUTS

I:1/0 — start push-button

I:1/1 — guard-closed limit switch (1 = closed)

OUTPUT

O:2/0 — drill motor

Ladder Diagram

L1 L2 000 I:1/0 XIC · Start I:1/1 XIC · Guard O:2/0 OTE · Drill

Two XIC contacts in series — classic AND. O:2/0 = I:1/0 · I:1/1.

How it works

Power has to flow through both XIC contacts in succession. Both must be 1 for the rung to be true. The moment either condition drops, the OTE de-energises and the drill stops — no latching, the operator must keep holding the button. This is sometimes called a jog or inching control.

What we learned: in industrial control, “permissives” (conditions that must be true) become XIC contacts in series. Add as many as you need — limit switches, level sensors, key switches — they all chain together with no extra cost.
03

PLC Program · Stop logic

Adding a Normally-Closed Stop Button

XIC + XIO

The problem: extend the previous program with a STOP push-button. The drill should run only while START is held AND guard is closed AND STOP is not pressed. The STOP button is wired NC (normally-closed) for safety.

Because STOP is wired NC, its input bit is 1 when the button is at rest and 0 when pressed (or when the wire breaks). To “test for not pressed”, we use an XIC on the STOP input — XIC passes power when the bit is 1, which means STOP is at rest.

Inputs & Outputs

INPUTS

I:1/0 — start push-button (NO)

I:1/1 — guard limit switch

I:1/2 — stop push-button (NC, 1 = at rest)

OUTPUT

O:2/0 — drill motor

Ladder Diagram

L1 L2 000 I:1/2 XIC · Stop I:1/0 XIC · Start I:1/1 XIC · Guard O:2/0 OTE · Drill

All three contacts are XIC because STOP is wired NC — the bit is 1 when STOP is at rest.

How it works

STOP wired NC + tested with XIC = fail-safe. If the STOP button is pressed, its bit goes to 0 and the XIC blocks. If the wire to STOP breaks, the input goes to 0 and the XIC also blocks. Both fault modes shut the drill down — exactly what you want from an emergency stop. This is the standard pattern for every industrial stop circuit.

What we learned: NC field wiring + XIC instruction = fail-safe stop. The opposite (NO field wiring + XIO instruction) is a wiring trap waiting to happen — never use it on safety-related circuits.
04

PLC Program · The most important rung in industry

Seal-In Latch — Start/Stop Motor Control

SEAL-IN

The problem: a momentary START button should start the motor, and the motor should keep running after the button is released. Pressing the STOP button (or any safety condition going false) stops the motor and it stays off until START is pressed again. This is the seal-in or latch rung — the single most common pattern in industrial PLC programming.

The trick: we add a parallel branch around the START contact, containing a contact of the output itself. Once the output turns on, that parallel branch stays closed, “sealing in” the rung even after START releases.

Inputs & Outputs

INPUTS

I:1/0 — START push-button (NO, momentary)

I:1/1 — STOP push-button (NC, momentary)

OUTPUT

O:2/0 — motor contactor (Tag: Motor_Run)

Ladder Diagram

L1 L2 000 XIC I:1/0 Start O:2/0 seal-in XIC I:1/1 Stop (NC) O:2/0 Motor OTE

The classic seal-in. Note the parallel XIC of O:2/0 — the output’s own bit holds the rung true after START releases.

How it works — scan by scan

  1. At rest: START not pressed, STOP not pressed. I:1/0=0, I:1/1=1 (NC at rest), O:2/0=0. Both branches of the parallel section are open. Output stays 0.
  2. Press START (one scan): I:1/0 becomes 1. Top branch closes, power passes through it AND through the STOP XIC (still 1). OTE energises O:2/0=1.
  3. START still held: top branch still passes; bottom branch also now passes because O:2/0=1.
  4. Release START: top branch opens. But the bottom branch (XIC of O:2/0) still closes because the output is still 1. Power continues to flow. OTE keeps O:2/0=1. The motor runs.
  5. Press STOP: I:1/1 goes from 1 to 0. The STOP XIC blocks. Rung becomes false. OTE drops O:2/0 to 0. The bottom branch’s XIC opens too. Everything resets.
What we learned: the seal-in branch — a parallel XIC of the output bit itself — gives a momentary START button the same effect as a maintained switch. Memorise this rung. You’ll write it on every project for the rest of your career.
05

PLC Program · Set / Reset

Latch and Unlatch — the OTL / OTU Alternative

OTL + OTU

The problem: identical behaviour to program 04 — momentary START turns the motor on and keeps it on, momentary STOP turns it off — but using two separate rungs with OTL and OTU instead of a seal-in branch. This is closer to how some logic charts express the action: “set on START, reset on STOP”.

Inputs & Outputs

INPUTS

I:1/0 — START push-button (NO)

I:1/1 — STOP push-button (NC)

OUTPUT

O:2/0 — motor contactor

Ladder Diagram (Two Rungs)

L1 L2 000 I:1/0 XIC · Start L O:2/0 OTL 001 I:1/1 XIO · Stop U O:2/0 OTU

Rung 0 latches the motor on. Rung 1 unlatches it. Two rungs, no seal-in branch.

How it works

Rung 0: when START is pressed, OTL sets O:2/0=1 and leaves it set. Releasing START doesn’t matter — OTL is retentive. Rung 1: when STOP is pressed (the NC button’s bit goes from 1 to 0), the XIO becomes true and OTU resets O:2/0=0.

What we learned: OTL/OTU achieves the same effect as a seal-in but with two simpler rungs. Caveat: OTL is retentive across power cycles — if power is lost while latched, the motor will restart on power-up. The seal-in version (program 04) does not have this property because the output drops when CPU loses power. For motor control, the seal-in pattern is usually safer. OTL/OTU shines for state flags that genuinely should survive a power cycle.
06

PLC Program · Mutual exclusion

Forward / Reverse Motor with Electrical Interlock

INTERLOCK

The problem: a conveyor motor can run forward or in reverse, controlled by two push-buttons (FWD and REV) plus a STOP. The two outputs must never energise simultaneously — that would short-circuit the motor starter and blow fuses. We add an interlock: each direction’s rung is blocked by an XIO of the other direction.

Inputs & Outputs

INPUTS

I:1/0 — FWD push-button

I:1/1 — REV push-button

I:1/2 — STOP push-button (NC)

OUTPUTS

O:2/0 — forward contactor

O:2/1 — reverse contactor

Ladder Diagram (Two Rungs)

L1 L2 000 I:1/0 FWD O:2/0 seal I:1/2 STOP O:2/1 XIO · interlock O:2/0 OTE · FWD 001 [Rung 1: identical structure for REV — XIC I:1/1 + XIC O:2/1 (seal) → XIC I:1/2 (Stop) → XIO O:2/0 (FWD interlock) → OTE O:2/1 (REV)]

Each direction has its own seal-in plus an XIO of the opposite output to prevent both ever energising at once.

How it works

Rung 0 (FWD): the structure is the seal-in from program 04, with two extra contacts in series — the STOP XIC and an XIO of O:2/1 (the reverse contactor’s bit). If REV is already on, the XIO blocks and FWD cannot energise. Rung 1 (REV) mirrors this exactly, with the interlock pulling from O:2/0 instead. Pressing FWD when REV is running does nothing — you must press STOP first.

What we learned: use an XIO of an output bit in another rung to enforce mutual exclusion. This is electrical interlocking in software. For high-power motors, you would also hardwire mechanical interlocks between the contactors — software alone isn’t trusted for safety-critical exclusion. Belt-and-braces.
07

PLC Program · Code reuse

Internal Relay — Reusing Complex Permission Logic

B-BIT

The problem: a small process plant requires three conditions to be met before any motor is allowed to start: tank level OK, temperature OK, and operator key-switch in AUTO. We have three motors on the plant. Rather than repeat all three contacts in three rungs, we compute the permission once into an internal relay and reference it three times.

Inputs & Outputs

INPUTS

I:1/0 — tank level switch (1 = OK)

I:1/1 — temperature switch (1 = OK)

I:1/2 — auto/manual selector (1 = AUTO)

I:1/3 · I:1/4 · I:1/5 — start buttons for motors 1, 2, 3

OUTPUTS

B3:0/0 — Plant_Permit (internal)

O:2/0 · O:2/1 · O:2/2 — three motor contactors

Ladder Diagram (Four Rungs)

L1 L2 000 I:1/0 Level I:1/1 Temp I:1/2 Auto B3:0/0 Plant_Permit 001 B3:0/0 Permit I:1/3 Start_M1 O:2/0 Motor 1 002 [Rung 2: same structure for Motor 2 — XIC B3:0/0 + XIC I:1/4 → OTE O:2/1] 003 [Rung 3: same structure for Motor 3 — XIC B3:0/0 + XIC I:1/5 → OTE O:2/2]

Rung 0 computes the plant permit. Rungs 1–3 each reference it as one contact instead of repeating the three field conditions.

How it works

The permit logic — three conditions ANDed — is computed exactly once and stored in B3:0/0. Each motor rung now needs only two contacts: the plant permit and that motor’s start button. If the plant conditions change (say, you add a fourth permissive), you edit only rung 0; the three motor rungs don’t change.

What we learned: internal relays — B-bits in old terms, BOOL tags in new — are how you keep ladder programs maintainable. The rule: if you’d write the same group of contacts twice, hoist it into a B-bit. Future-you (and future maintainers) will thank you.
08

PLC Program · First analog rung

Analog Input — Cooling Fan with GEQ Comparator

ANALOG

The problem: a 4–20 mA temperature transmitter (range 0–100 °C) is wired to analog input I:3.0. A cooling fan must turn on automatically when the temperature reaches or exceeds 60 °C, and turn off when it drops below 55 °C (the 5 °C gap is a deadband that prevents the fan from chattering on/off near the threshold).

For this rung we’ll use a GEQ (Greater-or-Equal) comparator and a small piece of seal-in logic to give us the deadband. We’ll assume the analog reading has already been scaled to °C and stored in F8:0.

Inputs & Outputs

INPUTS

F8:0 — Tank_Temp (scaled, °C)

OUTPUT

O:2/0 — cooling fan

Ladder Diagram

L1 L2 000 GEQ Greater-or-Equal A: F8:0 (Temp) B: 60.0 O:2/0 seal GEQ Greater-or-Equal A: F8:0 (Temp) B: 55.0 O:2/0 Cooling Fan OTE

Two GEQ blocks with different thresholds + a seal-in branch — that’s a deadband (hysteresis) controller in three contacts.

How it works

  1. Below 60 °C, fan off: top branch’s GEQ (Temp ≥ 60) is false. Fan output is 0, so bottom branch’s seal XIC is open. Both branches blocked → fan stays off.
  2. Crosses 60 °C: top GEQ becomes true. Power flows through. OTE energises the fan. O:2/0=1.
  3. Drops to, say, 58 °C: top GEQ (≥ 60) becomes false again. But the bottom branch is now alive: its seal XIC is true (fan is on), and the GEQ (≥ 55) is still true. So the fan stays on.
  4. Drops below 55 °C: bottom-branch GEQ becomes false. Both branches now blocked. Fan switches off. Will not turn back on until temperature climbs above 60 °C again — that’s the deadband.
What we learned: comparator instructions (GEQ, LEQ, EQU, NEQ) act like smart contacts on the rung. Combine two of them with a seal-in branch and you get hysteresis — the on-threshold and off-threshold are different, so noisy analog signals don’t make the output chatter. This is the standard pattern for tank level pumps, AC compressors, heater bands — anything bang-bang controlled by an analog measurement.

Common Student Mistakes

Things to watch out for in this chapter:

  • Wiring stop buttons NO instead of NC. A NO stop button + XIO test means a broken wire keeps the motor running. Always wire stops NC and test with XIC.
  • Forgetting the seal-in branch. Without it, releasing a momentary START button immediately drops the output. The motor “won’t run” — but really it’s running for one scan, then stopping.
  • Confusing OTE with OTL. OTE follows the rung every scan. OTL is set-and-forget — it stays on after the rung goes false. Mixing them up gives baffling behaviour.
  • Two OTE coils on the same address. If two rungs both have OTE O:2/0, only the last one to execute wins — the earlier rung’s effect is overwritten. This is “double-coil syndrome” and most editors will warn you about it.
  • Forgetting to add a forward/reverse interlock. Software interlocks via XIO of the opposite output bit are essential — and so are mechanical/electrical interlocks at the contactor for high-power motors.
  • Thinking forces are temporary. Forces stay set across power cycles on most platforms. Always check the force list before walking away from a CPU. Better: never leave a force in place.
  • Skipping the deadband on analog comparators. A single GEQ at 60 °C will chatter the fan on and off as the reading wobbles around 60.0. Always use two thresholds with a seal-in (programs 4 and 8 combined).

Quick Recap

The ten things to take away from Chapter 5:

  • The CPU’s memory is split between the program file (your code, read-only at runtime) and the data file (every variable, read-and-written every scan).
  • The scan cycle is four steps — input scan, program execution, output update, housekeeping — repeated thousands of times per second.
  • The five IEC 61131-3 languages are LD, FBD, ST, IL, SFC. Ladder dominates because electricians and technicians can read it.
  • The bit-level instruction set is XIC, XIO, OTE, OTL, OTU — that’s 90% of every ladder program ever written.
  • Instruction addressing is either file/element/bit (e.g. I:1/0) or tag-based (e.g. Start_PB) — same memory, different syntax.
  • Series contacts = AND, parallel branches = OR. Internal relays (B-bits) let you compute a permission once and reuse it everywhere.
  • Stop buttons are wired NC and tested with XIC — fail-safe wins.
  • The seal-in latch rung — XIC of START in parallel with XIC of the output, both feeding through XIC of STOP into OTE — is the most important pattern in industrial control.
  • Run, Program, and Test modes do different things to the outputs. Switching between them mid-process can cause real damage; always plan ahead.
  • Analog inputs come from 4–20 mA loops, get scaled to engineering units, then drive comparator instructions (GEQ, LEQ, EQU, NEQ) on rungs. Add a deadband for stability.

Review & Self-Assessment

Chapter 5 Review Questions

Try answering each question on your own first. Tap to reveal the answer when you’re done.

Q1Explain the difference between the program file and the data file in a PLC’s memory.+
The program file holds your code — main routine, subroutines, configuration. It is read-only during execution. The data file holds every live variable: input bits, output bits, timer accumulators, internal flags. The CPU reads and writes the data file constantly, but only reads (executes) the program file. Think recipe book vs cutting board.
Q2List the four steps of the program scan cycle in order.+
(1) Input scan — read all physical inputs into the input image table. (2) Program execution — evaluate every rung against the snapshot, write results to the output image table. (3) Output update — copy the output image table to the physical output terminals. (4) Housekeeping — communications, diagnostics, then loop back to step 1.
Q3Why might a button pressed for 0.5 ms be missed by a PLC even if the scan time is 5 ms?+
Because the input is only sampled once per scan (during the input-scan step). If the button is pressed and released entirely within a single scan — between two consecutive input samples — the PLC never sees the change. To catch fast events, use a high-speed input module with built-in latching, or write logic that latches the input on first detection.
Q4Name the five IEC 61131-3 PLC programming languages and say which is most popular for general factory work.+
LD (Ladder Diagram), FBD (Function Block Diagram), ST (Structured Text), IL (Instruction List), and SFC (Sequential Function Chart). LD is the most popular for general factory work — its relay-schematic look matches what electricians and technicians already understand.
Q5Decode the address I:1/3.+
I = the input file. 1 = element 1 (typically slot 1 in an Allen-Bradley SLC chassis). /3 = bit 3 (the fourth bit, since they start at 0). So I:1/3 is “the fourth input on the module in slot 1.”
Q6What’s the difference between OTE and OTL? When would you use each?+
OTE follows the rung every scan — output is 1 only while the rung is true. OTL is retentive — once set by a true rung, the output stays 1 even after the rung goes false. You only clear it with a separate OTU instruction. Use OTE for everything where the output should track a condition (lamps, valves with seal-in branches). Use OTL/OTU when you genuinely need set/reset behaviour and want the state preserved through power cycles — for example, “fault latched” indicators that must persist until acknowledged.
Q7Why are stop buttons wired normally-closed and tested with an XIC instruction?+
For fail-safe operation. NC field wiring + XIC test means: pressing STOP drops the input bit to 0 and stops the motor — and a broken wire also drops the input to 0 and stops the motor. Both fault modes shut things down. The opposite combination (NO + XIO) would let a broken wire keep the motor running, which is dangerous.
Q8Sketch the seal-in (start/stop) ladder rung and explain why the seal-in branch is needed.+
The rung has a parallel branch on the left: top arm is XIC of the START button, bottom arm is XIC of the output coil itself. After the parallel section, the rung continues with XIC of STOP (NC), then OTE of the output. The seal-in branch is needed because START is a momentary push-button — once released, the top arm opens. Without the seal-in arm, the output would drop. With it, the output’s own bit holds the rung true and the motor keeps running until STOP breaks the chain.
Q9In a forward/reverse motor program, where do you put the interlock that prevents both directions running at once?+
Each direction’s rung gets an extra XIO of the opposite output bit in series with everything else. The forward rung includes XIO O:2/1 (REV). The reverse rung includes XIO O:2/0 (FWD). If REV is on, the FWD rung is blocked — and vice versa. For high-power motors you also wire mechanical or electrical interlocks at the contactor itself; never trust the PLC alone for safety-critical exclusion.
Q10What is an internal relay and when should you use one?+
An internal relay (B-bit, M-bit, or BOOL tag depending on the platform) is a memory bit that exists only inside the CPU — not connected to any field wire. Use one whenever you find yourself writing the same group of contacts in more than one rung. Compute the result once into the internal relay, then reference it with a single XIC contact wherever you need it. This makes programs shorter, easier to read, and far easier to modify when the conditions change.
Q11Why is the 4–20 mA current loop preferred over a 0–10 V signal for analog instruments?+
Three reasons. (1) Distance: current is the same all the way down the wire regardless of resistance, so accuracy doesn’t degrade over long runs. A voltage signal drops with distance. (2) Noise immunity: current loops are far less affected by electrical noise than voltage signals. (3) Wire-break detection: 4 mA at the low end (rather than 0) means any reading of 0 mA must be a broken wire — the controller can detect and alarm the fault automatically.
Q12Why is a deadband (hysteresis) usually added to an on/off analog control rung?+
Without a deadband, an analog reading sitting close to the threshold causes the output to chatter on and off as the reading wobbles by tenths of a degree. Chattering wears out contactors, motors, and relays. By using two different thresholds (turn-on at 60, turn-off at 55, with a seal-in branch) you create a 5-unit gap where neither switching action happens — the output stays in whichever state it was last in. This is hysteresis, and it’s how every thermostat in your house works too.

Need help with your first ladder program or a tricky seal-in rung?

Get one-on-one tutoring or project guidance from Dr Ahsan Rahman — Head of Electrical Engineering, with two decades of teaching ladder logic and hands-on PLC commissioning.

Request Consultation →