Executing Initialization Function Block on PLC Startup in CodeSys

Question:

How can I run code stored in initialization Function Block (FB) when the PLC first starts up, similar to OB100 in Siemens PLC? I need to set the var_init variable to TRUE to execute the initialization FB, then set it to FALSE once done. In Siemens projects, OB100 is commonly used for this task. What is the best way to achieve this in CodeSys? Thank you for your help!

Top Replies

Contact and Coil on Preventing Multiple Engagements To prevent multiple engagements in a PLC program, it is crucial to ensure that the FB is activated during the first scan cycle after the PLC transitions from PROGRAM mode or after a power cycle into RUN mode. This "first-pass/-scan" Boolean option can be utilized to seal in the coil effectively.

mikas_m inquired about executing code stored in the initialization function block on the initial startup of the PLC, similar to OB100 in Siemens PLC systems. The goal is to set the var_init variable to TRUE to trigger the execution of the initialization function block, and then set it to false once completed. In CodeSys, the recommended approach for achieving this is to use a boolean variable like Oneshot_Occurred to ensure the code is executed only once during initialization. This simple yet effective method ensures that the initialization code runs smoothly and efficiently. It is important to note that this approach is implemented within the main loop and should not require redeclaration or redefinition of the variable. Further exploration into the characteristics of POU calling may provide additional insights for optimizing the code structure.

To improve your productivity, I recommend reviewing TASKS & EVENTS. Please refer to the attached screenshot for reference.

Thank you for your feedback! In the main program PLC_PRG, I utilized a function block (FB), while in the init function block (FB), I used structured text (ST). Take a look at the screenshot for reference. To ensure the init FB is executed only once, I had to use a one_shot variable both as an enable and an input. There might be a more efficient approach to achieve this. Additionally, I observed a discrepancy between the output of the ADD block and the value_int variable. I have configured the cyclic task to run every 2 seconds, incrementing the value_int variable from an initial value. When checking in online mode, I noticed a difference, which I believe is normal as the output of the ADD block is only assigned to the value_int variable at the end of the cycle.

This procedure seems overly complex. Instead of complicating things, why not simply assign a value in the initialization step of FB, like this: Code: out_var_INT := 12;? This will streamline the process. In the main program, you can then utilize the one_shot variable linked to the enable input. This simplified approach may improve efficiency and clarity in your programming.

In CodeSys, you can set up a similar function by using the "Init" block; think of this as the OB100. When the PLC starts, any code written inside the "Init" block will be the first to execute. You can easily place your FB inside this block and manipulate the var_init as you specified. Remember to set var_init to TRUE before the function block call and reset it to FALSE afterward. It’s a bit different than Siemens but accomplishes the same goal.

You could achieve this in CodeSys by using the "Start Up" POU (Program Organizational Unit). This functions similarly to the OB100 in Siemens. It automatically runs when the PLC starts up, making it an ideal place to put any initialization code. Be sure to set your var_init variable to TRUE for initialization, and then set it to FALSE once your initialization block has been executed. Just remember, the execution only happens once during startup. Hope this helps!

In CodeSys, you can achieve this by creating a new Program (PRG) and using it as your startup program. From the "Application" menu, select "Add Object," then "Program." In this newly created PRG, set your 'var_init' to TRUE at the beginning, call your initialization FB, then set 'var_init' to FALSE. Then, under "Project" -> "Options" -> "Build," you can select this program to run at startup. Remember to connect the variables correctly. This way, your system behaves similarly to the Siemens PLC running the OB100.

In CODESYS, you can emulate the OB100 execution at startup by creating an instance of your initialization Function Block within Program Organization Unit (POU) MAIN. Set 'var_init' to TRUE, execute your initialization FB, then immediately set 'var_init' to FALSE. This will ensure that your initialization code runs once during the PLC startup. MAIN is the first POU executed at PLC startup, much like OB100 function in a Siemens PLC. If your project has more complex needs, consider looking into the Cyclic and Startup Tasks under the "Task Configuration" as they offer more flexibility for managing execution.

More Replies β†’

Streamline Your Asset Management
See How Oxmaint Works!!

βœ…   Work Order Management

βœ…   Asset Tracking

βœ…   Preventive Maintenance

βœ…   Inspection Report

We have received your information. We will share Schedule Demo details on your Mail Id.

To add a comment, please sign in or register if you haven't already..   

Frequently Asked Questions (FAQ)

FAQ: FAQs:

Answer: 1. How can I execute initialization Function Block (FB) code on PLC startup in CodeSys? - In CodeSys, you can achieve this by setting a specific variable, like var_init, to TRUE initially to trigger the execution of the initialization FB, and then set it to FALSE once the initialization is complete.

FAQ: 2. Is there an equivalent of OB100 in Siemens PLC for executing initialization code in CodeSys?

Answer: - While OB100 is commonly used in Siemens PLC for startup tasks, in CodeSys, you can use a similar approach by utilizing a variable and the initialization Function Block to execute necessary code on PLC startup.

FAQ: 3. What is the recommended method to run initialization code on PLC startup in CodeSys?

Answer: - The best way to run initialization code on PLC startup in CodeSys is to set a variable to TRUE to trigger the execution of the initialization Function Block, and then set the variable to FALSE once the initialization process is finished.

FAQ: 4. Can you provide guidance on implementing startup tasks in CodeSys similar to OB100 in Siemens PLC?

Answer: - To replicate the functionality of OB100 in Siemens PLC in CodeSys, you can use a variable (e.g., var_init) to control the execution of initialization code within a Function Block at PLC startup. Set the variable to TRUE to initiate the initialization process and set it to FALSE when the initialization is complete.

Ready to Simplify Maintenance?

Join hundreds of satisfied customers who have transformed their maintenance processes.
Sign up today and start optimizing your workflow.

Request Demo  β†’