Docsity

PLC Subroutines Study notes, Study Guides, Projects, Research of Programming Languages

Toggle sidebar
bg1
PLC SUBROUTINE STUDY NOTES
PLC Subroutines
Design, Scan Cycle, and Troubleshooting Study Notes
Independent study material
Purpose. These notes explain how a PLC uses subroutines to divide control logic into smaller
functional blocks. The examples are generic and focus on program organization rather than a
specific assignment, trainer, or manufacturer-provided worksheet.
Topics covered
Main routine and subroutine responsibilities
PLC scan order and conditional subroutine calls
Generic input/output mapping
Testing and troubleshooting methods
Design mistakes that can cause unexpected outputs
Page 1
pf3
pf4
pf5

Partial preview of the text

Download PLC Subroutines Study notes and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!

PLC Subroutines

Design, Scan Cycle, and Troubleshooting Study Notes

Independent study material

Purpose. These notes explain how a PLC uses subroutines to divide control logic into smaller functional blocks. The examples are generic and focus on program organization rather than a specific assignment, trainer, or manufacturer-provided worksheet. Topics covered

 Main routine and subroutine responsibilities

 PLC scan order and conditional subroutine calls

 Generic input/output mapping

 Testing and troubleshooting methods

 Design mistakes that can cause unexpected outputs

1. Why PLC Programs Use Subroutines

A subroutine is a separate group of ladder instructions that performs one defined task. Instead

of placing every condition and output in a single long routine, the programmer can divide the

application into blocks such as startup control, alarm handling, manual operation, and

sequence control. The main routine then determines when each block should execute.

1.1 Benefits of a modular program

 Readability: each routine can be named and reviewed according to its purpose.

 Troubleshooting: a technician can monitor the routine connected to the failed function.

 Reuse: a proven logic pattern can be adapted for similar machines or stations.

 Expansion: new functions can be added without turning the main routine into one large

ladder.

 Testing: routines can be verified one function at a time before the complete system is run.

1.2 Main routine versus subroutine Program section Primary responsibility Good design practice Main routine Controls overall program flow and decides which functions are allowed to run. Keep it concise enough that the operating sequence can be understood quickly. Subroutine Handles one machine function, output group, mode, or sequence step. Use one clear purpose and avoid hidden dependencies on unrelated routines. Fault or alarm routine Checks abnormal conditions and records or responds to faults. Prioritize safe states and make reset conditions explicit. 1.3 Typical execution pattern

The following text example shows a main routine making conditional calls. The labels are

generic and are not copied from a specific controller project.

MAIN ROUTINE

Rung 0 [System_Enable] -------------------- (Call STARTUP routine) Rung 1 [Auto_Mode] [No_Fault] ------------ (Call AUTO routine) Rung 2 [Manual_Mode] ---------------------- (Call MANUAL routine) Rung 3 [Fault_Present] -------------------- (Call ALARM routine) Rung 4 ------------------------------------ (End main routine)

2.4 Example routine logic MAIN Rung 0 [System_Enable] -------------------- (Call INDICATION routine) INDICATION SUBROUTINE Rung 0 [Green_Command] [/Red_Command] ------ (Green_Light) Rung 1 [Red_Command] [/Green_Command] ------ (Red_Light) Rung 2 ------------------------------------ (Return)

The normally closed command contacts in this example create a simple interlock: both pilot

lights cannot be energized by simultaneous button commands. A real system would also

include the required safety and fault conditions.

2.5 Output ownership rules

 Assign one routine as the primary owner of each physical output whenever practical.

 Use internal status bits to communicate between routines instead of repeating the same

output coil.

 Define what happens when the call condition becomes false: clear, retain, or reset the

controlled state.

 Place safety-permissive conditions close to the output logic so they remain easy to verify.

2.6 Quick scan-order check

When an output behaves unexpectedly, list every routine that can affect it and write down the

order in which those routines execute. This simple trace often reveals a duplicate instruction or

a later rung that overwrites an earlier result.

3. Planning a Subroutine-Based Program 3.1 Define functions before addresses

Begin by listing the machine functions in plain language. Examples include “permit startup,”

“run conveyor,” “display status,” and “detect jam.” After the functions are clear, assign each

one to the main routine or to an appropriate subroutine. Address selection should follow the

physical wiring and the controller platform.

3.2 Suggested planning worksheet Function Routine Inputs needed Outputs affected Safe-state rule System permission MAIN Enable, stop, fault status Routine calls Disable calls when permission is false Status indication STATUS Mode and command bits Pilot lights Clear non-latched outputs when disabled Automatic sequence AUTO Start, sensors, timers Motors and valves Stop motion on fault or mode change Alarm handling ALARM Fault bits and acknowledgments Alarm lamp or message Keep active until the reset rule is satisfied 3.3 Naming and documentation

 Use tag names that describe purpose rather than wire color alone.

 Add a brief rung comment when the reason for a condition is not obvious.

 Keep routine names consistent, such as MAIN, AUTO, MANUAL, STATUS, and ALARM.

 Document whether an output is ordinary, latched, or controlled by more than one routine.

 Record the expected safe state for each field output.

3.4 Common design mistakes Mistake Why it causes trouble Writing to the same output from several routines The final state may depend on scan order, making the behavior difficult to predict and troubleshoot. Calling a routine in several unrelated places The program flow becomes hard to follow and the routine may execute more often than intended. Skipping reset logic Latched states or retained values may remain active after a mode change. Using unclear addresses without tags Wiring mistakes and wrong-terminal troubleshooting become more likely. Testing every function at once A failure becomes harder to isolate because several conditions change together.

5. Review Questions

13. Why is it useful to keep the main routine shorter than the complete application logic?

14. What happens to program execution after a called subroutine reaches its end?

15. Why can writing to one physical output from several routines create unpredictable

behavior?

16. What should be defined for every output when its controlling routine is not executed?

17. Which testing method makes an addressing or interlock error easiest to isolate?

Answer key

 A shorter main routine makes overall program flow and operating permissions easier to

understand.

 Execution returns to the instruction following the call in the routine that initiated it.

 The output can be solved more than once in one scan, so the final result may depend on

execution order.

 The program should define the intended safe or retained state and include the necessary

clear or reset logic.

 Change and verify one input or condition at a time while monitoring the related rung.

6. Summary

Subroutines help organize PLC control logic by assigning one clear function to each program

section. A reliable design uses a readable main routine, documented calls, consistent tag

names, controlled output ownership, and explicit safe-state behavior. The most effective

verification process checks addresses first, tests one condition at a time, and monitors

program flow from the call rung through the selected subroutine.