📖 Scenario: You are working on a factory automation project using a PLC (Programmable Logic Controller). The project controls a conveyor belt system that moves boxes. To keep the program easy to understand and fix, you will use modular programming by dividing the tasks into small parts.
🎯 Goal: Build a simple PLC program using modular programming. You will create separate modules for starting the conveyor, stopping the conveyor, and checking the conveyor status. This will show why modular programming helps keep your code clean and easy to manage.
📋 What You'll Learn
Create a main program that calls separate modules
Create a module to start the conveyor
Create a module to stop the conveyor
Create a module to check conveyor status
Print the conveyor status at the end
💡 Why This Matters
🌍 Real World
Factories use PLC programs to control machines like conveyors. Modular programming helps engineers quickly update or fix parts without changing the whole program.
💼 Career
Understanding modular programming is essential for PLC programmers to write clean, maintainable, and scalable automation code.
Progress0 / 4 steps
1
Create the main program structure
Create a main program called MainProgram that will later call other modules. For now, just define it as an empty program.
PLC Programming
Hint
Use the PROGRAM keyword to define the main program and END_PROGRAM to end it.
2
Create modules to start and stop the conveyor
Create two separate modules called StartConveyor and StopConveyor. Each module should be a program that sets a variable ConveyorRunning to TRUE or FALSE respectively.
PLC Programming
Hint
Use VAR to declare variables inside each program. Assign TRUE or FALSE to ConveyorRunning in each module.
3
Create a module to check conveyor status
Create a module called CheckConveyorStatus that reads the ConveyorRunning variable and sets a variable StatusMessage to 'Running' if ConveyorRunning is TRUE, or 'Stopped' if FALSE.
PLC Programming
Hint
Use an IF statement to check ConveyorRunning and assign StatusMessage accordingly.
4
Print the conveyor status in the main program
In the MainProgram, call the StartConveyor module, then call CheckConveyorStatus, and finally print the StatusMessage variable.
PLC Programming
Hint
Call the modules by their names followed by parentheses. Use a variable StatusMessage in MainProgram to hold the status.
Practice
(1/5)
1. Why is modular programming important in PLC programming?
easy
A. It breaks big tasks into smaller, manageable parts.
B. It makes the program run faster by itself.
C. It removes the need for testing the program.
D. It prevents the program from ever having errors.
Solution
Step 1: Understand the purpose of modular programming
Modular programming divides a large task into smaller parts to make it easier to handle.
Step 2: Compare options with this purpose
Only It breaks big tasks into smaller, manageable parts. correctly states this benefit. Other options are incorrect because modular programming does not automatically speed up programs, remove testing, or prevent all errors.
Final Answer:
It breaks big tasks into smaller, manageable parts. -> Option A
Quick Check:
Modular programming = breaking tasks into parts [OK]
Hint: Modular means breaking big tasks into small parts [OK]
Common Mistakes:
Thinking modular programming makes code error-free
Believing modular code runs faster automatically
Assuming modular code needs no testing
2. Which of the following is the correct way to define a module (function) in a PLC structured text program?
easy
A. FUNCTION MyModule : BOOL; BEGIN ... END_FUNCTION;
B. MODULE MyModule() { ... }
C. def MyModule(): ... end
D. function MyModule() { ... }
Solution
Step 1: Identify correct PLC structured text syntax
In PLC programming, modules or functions are defined using FUNCTION ... END_FUNCTION blocks.
Step 2: Check each option
FUNCTION MyModule : BOOL; BEGIN ... END_FUNCTION; uses correct syntax. The other options use syntax from other languages (like JavaScript or Python), which is invalid in PLC.
Final Answer:
FUNCTION MyModule : BOOL; BEGIN ... END_FUNCTION; -> Option A
Quick Check:
PLC functions use FUNCTION ... END_FUNCTION [OK]
Hint: PLC functions use FUNCTION ... END_FUNCTION syntax [OK]
Common Mistakes:
Using Python or JavaScript syntax in PLC code
Forgetting END_FUNCTION to close the module
Confusing MODULE keyword with FUNCTION
3. Given this PLC code snippet, what will be the output of Result after execution?
FUNCTION AddTwoNumbers : INT
VAR_INPUT
a : INT;
b : INT;
END_VAR
AddTwoNumbers := a + b;
END_FUNCTION
PROGRAM Main
VAR
Result : INT;
END_VAR
Result := AddTwoNumbers(3, 4);
END_PROGRAM
medium
A. 34
B. 7
C. 0
D. Error: Function call invalid
Solution
Step 1: Understand the function behavior
The function AddTwoNumbers takes two integers and returns their sum.
Step 2: Calculate the function call result
Calling AddTwoNumbers(3, 4) returns 3 + 4 = 7, which is assigned to Result.
Final Answer:
7 -> Option B
Quick Check:
3 + 4 = 7 [OK]
Hint: Add inputs inside function to get output [OK]
Common Mistakes:
Concatenating numbers as strings (getting 34)
Assuming default zero output
Thinking function call syntax is wrong
4. Identify the error in this modular PLC code snippet:
FUNCTION Multiply : INT
VAR_INPUT
x : INT;
y : INT;
END_VAR
Multiply := x * y
END_FUNCTION
medium
A. END_FUNCTION keyword is misspelled
B. Incorrect function name
C. VAR_INPUT block should be outside FUNCTION
D. Missing semicolon after assignment
Solution
Step 1: Check syntax line by line
The assignment line 'Multiply := x * y' is missing a semicolon at the end.
Step 2: Verify other parts
Function name is valid, VAR_INPUT is correctly inside FUNCTION, and END_FUNCTION is spelled correctly.
Final Answer:
Missing semicolon after assignment -> Option D
Quick Check:
PLC statements end with semicolon [OK]
Hint: Check for missing semicolons in assignments [OK]
Common Mistakes:
Ignoring missing semicolon errors
Thinking VAR_INPUT must be outside function
Assuming function name must be different
5. You have a large PLC program controlling a factory line. How does modular programming help when one part of the line needs fixing without stopping the whole system?
hard
A. Modular programming slows down the system to prevent errors.
B. Modular code automatically fixes errors without human help.
C. Modules let you isolate and fix only the faulty part easily.
D. Modules combine all code so you must fix everything at once.
Solution
Step 1: Understand modular programming benefits in maintenance
Modular programming breaks code into parts, so you can work on one part without affecting others.
Step 2: Apply this to fixing a factory line
If one module controls a part of the line, you can fix that module without stopping the entire system.
Final Answer:
Modules let you isolate and fix only the faulty part easily. -> Option C
Quick Check:
Modular code isolates fixes [OK]
Hint: Fix one module without stopping whole system [OK]
Common Mistakes:
Thinking modular code fixes errors automatically
Believing all code must be fixed at once
Assuming modular programming slows system down
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.