Skip to content
Introduction to Industrial Robotics

PLC Programming Basics

20 min read Training Guide

Read and write basic ladder logic, use timers and counters, explain the scan cycle, and go online to troubleshoot, force and clear faults safely.

Table of contents

A programmable logic controller (PLC) is a rugged industrial computer that reads inputs, runs a program and controls outputs. It replaced walls of relays, because you can change a program without rewiring a panel. For most electricians, a PLC program is less a coding task and more a very detailed drawing that you can watch while it runs. This guide teaches you to read that drawing and to find out why a machine stopped.

What You Will Be Able to Do

  • Explain the scan cycle, and why it matters.
  • Read ladder logic, including series, parallel and normally closed logic.
  • Use timers and counters.
  • Go online, monitor a program and use forcing carefully.
  • Recognize faults, and protect a program with backups.

Instruction names here follow Allen-Bradley style, since it is the most common in North America. Siemens, Mitsubishi and Omron have the same concepts, using different names and software.

Why PLCs Replaced Relays

  • A logic change is an edit, not a rewiring job.
  • One PLC replaces many relays, timers and counters.
  • It has fewer moving parts to wear out.
  • It reports what is happening, which a relay cannot do.

The Scan Cycle

A PLC runs its program in a repeating loop called a scan:

  1. Read inputs. It takes a snapshot of every input and stores it in memory, in the input image table.
  2. Run the program. It solves each rung from top to bottom, and left to right.
  3. Update outputs. It sends the results from the output image table to the real outputs.
  4. Housekeeping. It handles communication, diagnostics and other internal work.

A scan typically takes from about 1 millisecond to as much as 100 milliseconds, depending on the CPU and the program.

Two things follow from the scan:

  • The inputs are frozen while the program runs. A change in the middle of a scan waits until the next one.
  • A signal that is shorter than a scan can be missed entirely. Fast pulses from an encoder need a high-speed counter module, or an interrupt routine.

Addresses and Tags

Each input and output has a unique address. That is how the program knows which terminal it means. An older style address looks like I:1/0 for input slot 1, terminal 0. Newer controllers use tag names such as Conveyor_1_Run, which are linked to physical I/O. Your I/O list connects each address or tag to a wire and a device. Keep it close when you troubleshoot.

Ladder Logic

Ladder logic was designed to look like a relay drawing. Two vertical rails stand for power. The left rail is the hot side, and the right rail is the common side. Rungs run between them, and each rung is a logic statement. Conditions sit on the left, and the output sits on the right.

If there is a true path from left to right, the rung is true, and the output turns on.

Basic Instructions

Instruction Symbol True when
XIC, examine if closed --[ ]-- The bit is 1
XIO, examine if open --[/]-- The bit is 0
OTE, output energize --( )-- Sets the bit to match the rung
OTL, output latch --(L)-- Turns the bit on, and it stays on
OTU, output unlatch --(U)-- Turns a latched bit off

AND and OR

Contacts in series make an AND. Every one must be true.

 --[ Guard_Closed ]----[ Start_OK ]----( Run )--

Contacts in parallel make an OR. Any one branch can make the rung true.

 --+--[ Button_A ]--+----( Lamp )--
   |                |
   +--[ Button_B ]--+

The Normally Closed Trap

An XIC or XIO looks at the bit in memory, not at what the field device is doing. This trips up nearly everyone at first.

A normally closed stop button wired to an input is closed at rest, so the input bit is 1 while nobody is pressing it. Pressing the button makes the bit 0.

Stop button Input bit XIC result XIO result
Not pressed 1 True False
Pressed 0 False True

To let a machine run while the stop is not pressed, you use XIC on that input, even though the button itself is normally closed. An XIO there would be false all the time the button is at rest, and the machine would never run. Stops are wired normally closed so that a broken wire stops the machine.

Seal-In

Here is a start-stop rung, working just like a three-wire starter:

 --[ Stop_PB ]--[ OL_OK ]--+--[ Start_PB ]--+----( Motor )--
                           |                |
                           +--[ Motor ]-----+

Pressing Start turns on the Motor bit. On the next scan, the Motor branch holds the rung true, after Start is released. Pressing Stop or tripping the overload breaks the rung.

Latches

An OTL stays on, even after its rung goes false. It takes an OTU to turn it off. Because the latch is remembered through a power cycle, a latched output can come back on when power returns, so use latches with care.

Duplicate Outputs

Do not use the same OTE address on more than one rung. The program solves them in order, so the last rung wins, and the earlier rungs seem to do nothing. Combine the conditions into a single rung, or use internal bits.

Timers

A timer needs a preset (the target time) and an accumulator (the time so far). It sets status bits: EN (enabled), TT (timing) and DN (done). Time units vary by platform, and on many controllers a preset of 3000 means 3 seconds.

Timer What it does Example use
TON (on-delay) Times while the rung is true, and DN turns on at the preset Wait after a conveyor starts
TOF (off-delay) Times after the rung goes false, and DN turns off at the preset Run a fan after a motor stops
RTO (retentive) Like TON, but it keeps its time when the rung goes false Track motor run hours

A TON resets to zero the moment its rung goes false, even if it has almost finished. If you need to keep time across interruptions, you need an RTO, which takes a RES instruction to clear it.

Worked Example: A Delay

A warning horn must sound for 4 seconds before a press moves.

  1. A TON with a 4,000 ms preset runs when Cycle_Start is true.
  2. The horn output uses the timer's TT bit, so it sounds during timing.
  3. The press output uses the timer's DN bit, so it moves only after the delay.
  4. If Cycle_Start goes false at 2 seconds, the timer resets to 0 and the press does not move.

Counters

A CTU counts up by 1 each time its rung goes from false to true. Holding the rung true does not add more counts. When the accumulated value reaches the preset, the DN bit turns on. A CTD counts down in the same way.

Counters do not reset themselves. Use a RES instruction, or the count stays. A counter also needs a clean signal, because bouncing contacts can make it double-count.

Compare and Math

Compare instructions test values, and they make a rung true or false.

Instruction Meaning
EQU Equal
NEQ Not equal
GRT, GEQ Greater than, greater or equal
LES, LEQ Less than, less or equal

Math instructions such as ADD, SUB, MUL and DIV store a result. MOV copies a value. SCL or a similar instruction scales raw analog values to engineering units, such as turning a count into PSI.

Keeping a Program Readable

  • Use clear tag names, such as Pump_2_Running.
  • Add a comment to each rung that explains why it is there.
  • Split the program by machine function, using subroutines.
  • Keep rungs simple. Use an internal bit to break up a long rung.

Online and Offline

Offline means you are working on a copy of the program on a laptop, with no connection to the PLC. You can edit as much as you like, and nothing moves until you download it.

Online means your laptop is connected to the running PLC. You can watch logic in real time. True contacts are highlighted, and that makes online monitoring your best troubleshooting tool. Edits made online take effect in the running machine, so they need a plan and an approval.

Cross-reference tools list every rung where a bit or tag is used, so you can follow it through the program. If an output is off, find its rung, and see which contact is not highlighted. Then trace that contact.

Warning: A download can stop the processor. An online edit can move a machine. Check with operations and follow your plant's change process before you do either.

Forcing

A force makes an input or output take a state that you choose. It overrides the field device and the program. You might use it to check that a solenoid works, or to simulate a sensor during start-up.

Warning: A forced output can start a machine with no warning. Before you force anything, find out exactly what is connected, and make sure no one is in the machine's path. Have someone watching at the machine.

  • Forces often survive a power cycle.
  • A leftover force can cause problems that seem impossible.
  • Most CPUs show a "forces active" light or indicator. Check it whenever you troubleshoot.
  • Remove all forces when you finish.

Faults

If the PLC finds a serious problem, it faults. A major fault usually stops the program and turns outputs off. The CPU's fault code names the cause, so read it before you clear it.

Fault What it means What to do
I/O or module fault A module is not responding Check seating, power and network
Watchdog timeout The scan took too long Look for program loops or recent edits
Memory fault The program is corrupt Restore from backup
Math or program fault Bad value or bad instruction Find the rung in the fault log

Clearing a fault may restart a machine, so make sure it is safe first.

Batteries and Backups

Many PLCs keep the program and data in memory that depends on a battery while power is off. If the battery dies, the program may be lost at the next power outage. Watch for a low-battery light, and replace the battery on schedule. Many newer controllers also store the program in flash memory or on a memory card. Check how yours works.

Always keep a backup. Before you change anything, save the current program with the date. If your change causes trouble, you can go back to something that worked.

Finding a Fault in Logic

If an output will not turn on:

  1. Go online, and look at the rung. Is it true?
  2. If it is false, find the contact that is not true, and trace it.
  3. If it is true, check for a force.
  4. Check the output module's LED.
  5. Measure voltage at the output terminal, then follow the field wiring.

If an input does not seem to work:

  1. Check the device itself.
  2. Check the module LED.
  3. Check the input bit online.
  4. Check for a force on that input.

Keep these documents at hand: the program, the I/O list, the electrical drawings and the sequence of operation.

Common Mistakes

Mistake What it costs
Using XIO on an NC stop input A machine that will not run
Two OTEs for one output An output that seems to ignore its rung
Leaving forces on Faults that no one can explain
Clearing a fault without reading it The same fault, and maybe a damaged machine
Editing online with no backup No way to get back to a working program
Using a TON for run hours A count that resets on each stop

Check Your Understanding

1. A TON has a 10-second preset. Its rung stays true for 6 seconds and then drops out. How much time does the accumulator hold now?

Zero. A TON resets when its rung goes false.

2. A guard switch is wired normally open, and it closes when the guard is shut. Which instruction lets the machine run only with the guard shut?

XIC, because the bit is 1 when the switch is closed.

3. A motor output is on one rung near the top and on another rung near the bottom. The top rung is true, but the motor is off. Why?

The lower rung is false and it overwrites the first, because the last rung scanned wins.

4. Parts pass a sensor faster than the scan time, and the count is low. What is a fix?

Use a high-speed counter or an interrupt, because a normal input can miss events between scans.

5. A rung is true online, but the output LED is off. What is your next check?

Look for a force on that output, then the module and its fuse.

Key Takeaways

  • A PLC reads inputs, runs logic top to bottom, updates outputs and repeats.
  • XIC and XIO examine bits, not the field device, so normally closed wiring needs thought.
  • Series contacts make AND, and parallel contacts make OR.
  • A TON resets when its rung goes false. Counters need a reset.
  • Do not use the same output on two rungs, because the last one wins.
  • Before forcing, know what is downstream and clear people away. Always keep a backup.