As a trusted supplier of Allen - Bradley PLCs, I've witnessed firsthand the transformative power of user - defined functions (UDFs) in industrial automation. In this blog, I'll share in - depth insights on how to use user - defined functions in Allen - Bradley PLC programming, providing practical guidance and real - world examples.
Understanding User - Defined Functions in Allen - Bradley PLCs
User - defined functions in Allen - Bradley PLCs are a powerful tool that allows programmers to encapsulate a set of instructions into a single, reusable block. This not only simplifies the programming process but also enhances code modularity and maintainability.
Why Use User - Defined Functions?
- Code Reusability: Once a UDF is created, it can be called multiple times from different parts of the program. This reduces redundancy and saves development time. For example, if you have a set of calculations that are performed at various points in the program, you can create a UDF for those calculations and call it whenever needed.
- Modularity: UDFs break down a large, complex program into smaller, more manageable chunks. Each UDF can be developed, tested, and debugged independently, making the overall programming process more efficient.
- Easier Maintenance: When changes are required, you only need to modify the UDF in one place, and those changes will be reflected wherever the UDF is called. This reduces the risk of introducing errors when making updates.
Creating a User - Defined Function in Allen - Bradley PLC Programming
Step 1: Define the Function
The first step in creating a UDF is to define its name, input parameters, and output parameters. In Allen - Bradley PLC programming, you can use software like Rockwell Automation's Studio 5000 Logix Designer.
Let's say we want to create a UDF that calculates the sum of two numbers. In Studio 5000 Logix Designer, we would follow these steps:
- Open the project in Studio 5000.
- Navigate to the "Program Organization Unit" (POU) folder in the project tree.
- Right - click on the POU folder and select "Add New POU".
- Choose "Function" as the POU type.
- Give the function a meaningful name, such as "SumTwoNumbers".
- Define the input parameters. In our case, we need two input parameters of type integer, let's call them "Input1" and "Input2".
- Define the output parameter. We need an output parameter of type integer to hold the sum, let's call it "Output".
Step 2: Write the Function Logic
Once the function is defined, we can write the logic inside the function. For our "SumTwoNumbers" function, the logic is straightforward:
Output := Input1 + Input2;
This line of code simply adds the two input numbers and stores the result in the output parameter.
Step 3: Call the Function
After creating the UDF, we can call it from other parts of the program. To call the "SumTwoNumbers" function, we need to provide values for the input parameters and assign the output to a variable.
For example, if we have two variables, Number1 and Number2, and we want to calculate their sum using our UDF, we can write the following code:
SumResult := SumTwoNumbers(Number1, Number2);
Here, SumResult is a variable that will hold the result of the function call.
Using User - Defined Functions with Different Allen - Bradley PLC Models
Allen - Bradley offers a wide range of PLC models, each with its own capabilities and features. Let's take a look at how UDFs can be used with some popular models.
Allen Bradley 1769 - L24ER - QBFC1B Controller
The Allen Bradley 1769 - L24ER - QBFC1B Controller is a compact and powerful controller suitable for a variety of industrial applications. When using UDFs with this controller, the process is similar to what we described above. However, you need to ensure that the controller has enough memory to support the UDFs and the overall program.
The 1769 - L24ER - QBFC1B has a certain amount of program memory and data memory. If your UDFs are large or if you have many of them, you may need to optimize the code to fit within the memory limits.
Allen Bradley 1761 - L32BWA Programmable Controller
The Allen Bradley 1761 - L32BWA Programmable Controller is another popular model. It has its own programming environment and capabilities. When working with UDFs on this controller, pay attention to the data types and the addressing scheme.
For example, the 1761 - L32BWA may have different data types available compared to other models. You need to make sure that the input and output parameters of your UDFs are of the appropriate data types supported by the controller.
Passing Data to and from User - Defined Functions
When working with UDFs, it's important to understand how to pass data between the calling program and the function.
Pass - by - Value
In pass - by - value, a copy of the input parameter's value is passed to the function. Any changes made to the input parameter inside the function do not affect the original variable in the calling program. This is the default method of passing parameters in many programming languages, including Allen - Bradley PLC programming.
For example, if we have a variable X in the calling program and we pass it as an input parameter to a UDF, the UDF will receive a copy of the value of X. If the UDF modifies the input parameter, the value of X in the calling program remains unchanged.


Pass - by - Reference
In pass - by - reference, the address of the input parameter is passed to the function. This means that any changes made to the input parameter inside the function will affect the original variable in the calling program.
In Allen - Bradley PLC programming, you can use pass - by - reference when you need to modify the original variable. To do this, you need to use the appropriate data types and syntax in Studio 5000 Logix Designer.
Error Handling in User - Defined Functions
Error handling is an important aspect of UDF programming. You need to anticipate potential errors that can occur inside the UDF and handle them gracefully.
For example, if our "SumTwoNumbers" UDF is called with invalid input values (such as non - numeric values), we need to handle that error. We can add error handling code inside the UDF to check the input values and return an error code if necessary.
IF (NOT IsNumeric(Input1) OR NOT IsNumeric(Input2)) THEN
Output := -1; // Error code
ELSE
Output := Input1 + Input2;
END_IF;
Using User - Defined Functions with Input/Output Modules
Allen - Bradley PLCs often work with various input/output (I/O) modules, such as the Allen - Bradley 1794 - TB32. UDFs can be used to interact with these I/O modules.
For example, you can create a UDF that reads the status of an input module and performs a specific action based on that status. Let's say we have an input module connected to the PLC, and we want to check if a particular input is high. We can create a UDF for this task:
FUNCTION CheckInputStatus: BOOL
VAR_INPUT
InputAddress: DWORD; // Address of the input
END_VAR
CheckInputStatus := ReadInput(InputAddress);
END_FUNCTION
Conclusion
User - defined functions are a valuable asset in Allen - Bradley PLC programming. They offer numerous benefits, including code reusability, modularity, and easier maintenance. By following the steps outlined in this blog, you can effectively create and use UDFs in your Allen - Bradley PLC projects.
If you're in the market for Allen - Bradley PLCs or need further assistance with PLC programming, including the use of user - defined functions, we're here to help. Our team of experts has extensive experience in Allen - Bradley PLC systems and can provide you with the support and products you need. Contact us to start a discussion about your specific requirements and explore how we can meet your automation needs.
References
- Rockwell Automation. Studio 5000 Logix Designer User Manual.
- Allen - Bradley PLC Programming Guides.
