Reusable code patterns help you write parts of your PLC program once and use them many times. This saves time and makes your program easier to understand and fix.
When you have a task that repeats in different parts of your PLC program.
When you want to make your program shorter and clearer.
When you want to avoid mistakes by reusing tested code.
When you want to update a behavior in one place and have it change everywhere.
When you want to organize your program into smaller, manageable pieces.
This block controls a motor and can be used for many motors by changing inputs.
PLC Programming
FUNCTION_BLOCK MotorControl
VAR_INPUT
Run : BOOL;
Speed : INT;
END_VAR
VAR_OUTPUT
Status : BOOL;
END_VAR
(* Motor control logic here *)
END_FUNCTION_BLOCK
Sample Program
This program uses the SimpleCounter block twice. Counter1 counts when Start1 is TRUE, Counter2 counts when Start2 is TRUE.
A. Incorrect use of '=' instead of ':=' for assignment.
B. VAR_OUTPUT should be declared before VAR_INPUT.
C. Missing semicolon at the end of the assignment line.
D. FUNCTION_BLOCK name must be lowercase.
Solution
Step 1: Check assignment syntax in FUNCTION_BLOCK
In PLC programming, assignment uses ':=' not '='.
Step 2: Verify other syntax elements
Semicolon is present. VAR_INPUT before VAR_OUTPUT is correct. FUNCTION_BLOCK names are case-sensitive but uppercase is allowed.
Final Answer:
Incorrect use of '=' instead of ':=' for assignment. -> Option A
Quick Check:
Use ':=' for assignment [OK]
Hint: Use ':=' for assignments in PLC code [OK]
Common Mistakes:
Using '=' instead of ':='
Thinking semicolon is optional
Believing VAR_OUTPUT must come first
5. You want to create a reusable FUNCTION_BLOCK that counts how many times an input boolean Trigger changes from FALSE to TRUE. Which approach correctly implements this behavior?
hard
A. Reset the counter to zero every time Trigger is TRUE.
B. Increment the counter every time the FUNCTION_BLOCK runs regardless of Trigger state.
C. Use a VAR to store previous Trigger state and increment a counter only when Trigger changes from FALSE to TRUE.
D. Use VAR_INPUT to store the counter and increment it inside the block.
Solution
Step 1: Understand the counting condition
The counter should increase only when Trigger changes from FALSE to TRUE (rising edge).
Step 2: Implement edge detection logic
Store previous Trigger state in a VAR variable. Compare current and previous states. Increment counter only if previous was FALSE and current is TRUE.
Final Answer:
Use a VAR to store previous Trigger state and increment a counter only when Trigger changes from FALSE to TRUE. -> Option C
Quick Check:
Edge detection needs previous state storage [OK]
Hint: Store previous input to detect rising edge [OK]
Common Mistakes:
Incrementing counter every cycle
Resetting counter incorrectly
Using VAR_INPUT for internal state
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.