How to Use Library Files to Share Code in the TIA Portal Environment?

 How to Use Library Files to Share Code in the TIA Portal Environment? In the development of a large-scale project, there often exists repetitive code. For example, let's say we need to control 10 walking motors on a production line, and their control methods are identical. If we were to write separate control programs for each motor, it would result in a significant amount of redundant code.

To minimize the waste of time and manpower caused by repetitive code writing, the control method for the motors can be encapsulated into a function block. The main program can then call this function block multiple times to achieve control over multiple motors. This concept aligns with the modular programming design we discussed earlier.For more information, you can refer to this article: 

How to Achieve Modular Programming Design in STEP7 & TIA Portal?

Today, we'd like to introduce another method to improve programming efficiency: using library files to share program code and data in the TIA Portal environment.

Taking the example of motor control function blocks, previously written function blocks were used within the same project. But what if another project has similar control requirements? Would we have to rewrite the code, or is there a way to share code between different projects?

In my experience with actual project development, there are two methods for sharing code: one is through recompiling source code for sharing, and the other is through library files. This article focuses on the latter.

First, let's understand what libraries are.

In TIA Portal, there are two types of libraries: Project Libraries and Global Libraries.

Project Libraries can store code or data that needs to be reused within a project. They are part of the project and are opened, closed, and saved along with the project.

The code in Project Libraries can only be used within the same project. To share code between multiple projects, Global Libraries are used.

Global Libraries can be categorized into three types: System Libraries, Corporate Libraries, and User Libraries.

System Libraries are provided by Siemens and include global objects such as program codes, data types, and graphic files for HMI screens. Users cannot modify the contents of System Libraries. In TIA Portal V13, System Libraries appear as shown in the following image:


Corporate Libraries, as the name suggests, are global libraries designed for collaboration among multiple individuals, typically used in scenarios where multiple people within a company collaborate on project development. When one person updates the code or data in a Corporate Library, others are notified of the version update to ensure they have the latest version.

User Libraries are libraries created by the user themselves. They are not tied to any specific project and can be used across different projects to access and utilize the code and data stored within them.

The steps to create a User Library are as follows:

  1. Right-click in the Global Libraries and select "New Library," as shown in the image below: 

  2. Set the path for the newly created library:

  3. Click "Create." You will see that the library "FDCP_Lib" has been added to the Global Libraries, as shown in the image below:



To create a shared program block in the User Library, it's quite simple—all you need to do is drag and drop.

For example, let's say we've created a function block for motor control named "FC_Motor_Control" in our project. To add this function block to the User Library, simply left-click to select the function block and drag it into the "Master Copies" section of the User Library. Here's an illustration:


Almost all objects can be placed in the "Master Copies" section, including Function Blocks (FB), Function Charts (FC), user-defined data types, and even the entire PLC document can be dragged directly into the "Master Copies" section to create a library file.

In a new project, if you want to use programs and data from the User Library, you can simply drag and drop, making it very convenient.

That concludes our introduction to sharing programs and data between multiple projects using library files.