📖 Scenario: You work in a factory where machines need to be controlled by a PLC (Programmable Logic Controller). You want to create a simple reusable code pattern to turn a motor on or off based on a start and stop button.
🎯 Goal: Build a reusable code pattern (function block) in PLC programming that controls a motor using start and stop buttons.
📋 What You'll Learn
Create a function block called MotorControl with inputs Start and Stop and an output MotorOn
Add a variable MotorOn inside the function block to hold the motor state
Use a simple logic to turn MotorOn true when Start is pressed and false when Stop is pressed
Instantiate the MotorControl function block in the main program
Print or display the motor state after pressing start and stop buttons
💡 Why This Matters
🌍 Real World
Factories use PLCs to control machines safely and reliably. Reusable code patterns like function blocks help programmers write clear and maintainable control logic.
💼 Career
PLC programming skills with reusable code patterns are essential for automation engineers and technicians working in manufacturing and industrial automation.
Progress0 / 4 steps
1
Create the MotorControl function block
Write a function block called MotorControl with inputs Start and Stop of type BOOL and an output MotorOn of type BOOL.
PLC Programming
Hint
Define the function block with inputs and outputs exactly as described.
2
Add motor control logic inside MotorControl
Inside the MotorControl function block, write code to set MotorOn to TRUE when Start is TRUE, and set MotorOn to FALSE when Stop is TRUE.
PLC Programming
Hint
Use an IF statement to check Start and Stop inputs and set MotorOn accordingly.
3
Instantiate MotorControl in main program
In the main program, declare a variable motor1 of type MotorControl. Then set motor1.Start to TRUE and motor1.Stop to FALSE to simulate pressing the start button.
PLC Programming
Hint
Declare motor1 as MotorControl and assign Start and Stop values.
4
Display the motor state
Add code in the main program to call the motor1 function block logic and then print the value of motor1.MotorOn to show if the motor is ON or OFF.
PLC Programming
Hint
Call the function block instance as motor1(); and then access motor1.MotorOn.
Practice
(1/5)
1. What is the main benefit of using FUNCTION_BLOCK in PLC programming?
easy
A. It creates a graphical interface for the PLC.
B. It makes the program run faster by using less memory.
C. It automatically fixes errors in the code.
D. It allows you to reuse code and simplify program management.
Solution
Step 1: Understand the purpose of FUNCTION_BLOCK
FUNCTION_BLOCK is designed to package code that can be reused multiple times in a PLC program.
Step 2: Identify the main benefit
Reusing code saves time and makes the program easier to manage and maintain.
Final Answer:
It allows you to reuse code and simplify program management. -> Option D
Quick Check:
Reusable code = Simplify management [OK]
Hint: FUNCTION_BLOCK means reusable code blocks [OK]
Common Mistakes:
Thinking FUNCTION_BLOCK speeds up execution
Believing it fixes errors automatically
Confusing it with user interface creation
2. Which of the following is the correct way to declare a FUNCTION_BLOCK named Counter in PLC programming?
easy
A. FUNCTION Counter
VAR_INPUT
Count: INT;
END_VAR
END_FUNCTION
B. FUNCTION_BLOCK Counter
VAR_INPUT
Count: INT;
END_VAR
END_FUNCTION_BLOCK
C. FUNCTION_BLOCK Counter()
VAR_INPUT
Count: INT;
END_VAR
END_FUNCTION_BLOCK
D. FUNCTION_BLOCK Counter
VAR_OUTPUT
Count: INT;
END_VAR
END_FUNCTION_BLOCK
Solution
Step 1: Identify correct FUNCTION_BLOCK syntax
FUNCTION_BLOCK declaration starts with 'FUNCTION_BLOCK Name' and ends with 'END_FUNCTION_BLOCK'. Inputs are declared inside VAR_INPUT.
Step 2: Check each option
FUNCTION_BLOCK Counter
VAR_INPUT
Count: INT;
END_VAR
END_FUNCTION_BLOCK correctly declares FUNCTION_BLOCK Counter with VAR_INPUT and ends properly. FUNCTION Counter
VAR_INPUT
Count: INT;
END_VAR
END_FUNCTION uses FUNCTION instead of FUNCTION_BLOCK. FUNCTION_BLOCK Counter()
VAR_INPUT
Count: INT;
END_VAR
END_FUNCTION_BLOCK adds parentheses which are not used. FUNCTION_BLOCK Counter
VAR_OUTPUT
Count: INT;
END_VAR
END_FUNCTION_BLOCK uses VAR_OUTPUT instead of VAR_INPUT for input variable.
Final Answer:
FUNCTION_BLOCK Counter
VAR_INPUT
Count: INT;
END_VAR
END_FUNCTION_BLOCK -> Option B
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.