Main Content

Modeling and Simulation of Ladder Diagrams in Simulink

The ladder modeling feature of Simulink® PLC Coder™ enables you to create Ladder Diagrams in the Simulink environment as a model. After creating the Ladder Diagram, you can simulate and generate code for the Ladder Diagram models from within the Simulink environment.

  1. To create a Ladder Diagram, open the Simulink PLC Coder Ladder library. At the MATLAB® command line, enter:


    The Ladder library opens containing all the blocks required for building the Ladder Diagram in Simulink.

  2. Create a blank Simulink model. You can drag appropriate blocks from the library to build your ladder logic model in Simulink. For each block, you can double-click the block to see the block parameters and use the help menu to view its description. For more information on the Ladder instructions that are implemented by these blocks, refer to the LOGIX 5000 Controllers General Instructions Reference Manual.

  3. The Simulink PLC Coder Ladder library contains top level ladder logic block such as PLCControllerSuite, PLC Controller, Task, Ladder Diagram Program, Ladder diagram Subroutine, Ladder Diagram Function Block (AOI), and AOI Runner. All these blocks are organization blocks (Ladder Diagram containers) that cannot be on Ladder Diagram rungs. Apart from these organizational blocks, other blocks from the library cannot be top level ladder logic block for simulation.

    • PLCControllerSuite can hold controller tags that are visible for all ladder logic blocks in this controller, and also can contain Task block.

    • PLCController allows you to build ladder logic directly. All the tags in the controller level ladder diagram are controller tags (global variables or I/O symbols)

    • Task is used to contain Ladder Diagram programs that are using the same sample time and priority.


      Code generation for empty Task blocks is not supported. If a Task block is empty, the software does not issue warnings or errors during code generation, but the generated code produces errors in Rockwell IDEs.

    • Ladder Diagram Program enables you to build ladder logic directly. Program-level Ladder Diagram can have program scope variables and also can access controller tags if defined.

    • Ladder Diagram Subroutine enables you to create and define a named ladder routine. You can edit the logic implemented by the subroutine by clicking the Routine Logic button under the block parameters menu of this block.

    • Ladder Diagram Function Block (AOI) enables you to create the Ladder Diagram function block. You can edit the parameters and specifications of this block by using the various options available under the block parameters menu of this block.

    • AOI Runner is special program block that can contain only one Ladder Diagram Function Block (AOI: add-on instruction) designed for AOI testing (test bench generation and verification).

  4. Drag a PLCControllerSuite block into the blank model you created in the previous step. You can double-click each organizational unit to traverse to the lower level ladder logic semantics and build your Ladder Diagram. The empty ladder logic semantics is shown.

  5. Use the XIC and Motor blocks from the library to construct a simple ladder diagram. Use Add Rungs or Add 1 Rung buttons in the ladder logic semantic to add a new rung. All added blocks must be on the rung. Use the Junction block to merge rung branches.

  6. Double-click each new block added to the rung and specify the tags. In Ladder Diagrams, tags (variables) are used for representing all inputs, outputs, and internal memory. The tag can be a variable name or an expression like:

    • Variable Name: Start, Stop, Switch

    • Bit Access: MyInt.0, MyInt.31

    • Array Element: A[1], B[2,3], C[idx], D[i, j]. Use of braces for indexing is not allowed in a tag expression. For example, A(2) is illegal.

    • Structure: A.B, C.D, E.F.G

    • Mixture: A[1].B[i,j].C[3].D

    • Expressions: A[3].B > C.D; A[3]+B[4].C

  7. The tags can have attributes such as Data Type, Initial Value, and size. To change the attributes of the tag, open the Program Variables table within the Ladder Diagram Program block. You can delete the unused variables in the variable table by selecting the Delete option. You must select Apply for the changes to take effect. Go to controller level block, and double-click the Controller Tags table to specify the global variable and I/O symbol attributes.

  8. To add rung comments to your model in Simulink, create a connected annotation (see Motor Control Logic in image) to the rung terminal block. For more information on annotation connectors, see Associate Annotations with Blocks and Areas.

  9. Update the ladder logic model to reflect changes. You have now created a simple ladder model in Simulink.

Model an AOI Prescan Routine

  1. Double-click the Function Block (AOI) inside the parent program unit.

  2. Select the Allow Prescan Routine check box and click Apply button.

  3. Click the Prescan Routine button.

  4. Add the logic to Prescan Routine Ladder Diagram.


If Function Block (AOI) is at the top level of a ladder diagram model and not inside a parent program unit, the Allow Prescan Routine option is not enabled.

Ladder Model Simulation

To perform Ladder Diagram simulation in Simulink, you must connect appropriate input and output blocks to the ladder model.

  1. Use the plcladderoption function to enable Animation. At the MATLAB command line, enter:


  2. Connect input and output ports to the PLCControllerSuite block to provide inputs for simulation and read the outputs. You must modify the attributes of the switch and motor tags. To change the attributes of the tag, open the Program Variables table within the Ladder Diagram Program block and set them to the values shown.

  3. Go to controller level block and double-click the Controller Tags table to specify the global variable and I/O symbol attributes.

  4. The software now adds input and output ports to the PLCControllerSuite block. You can use Simulink blocks to add inputs to the ladder model. For example, you can use the Constant block to add Boolean inputs to mimic switch behavior.

  5. Traverse to the Ladder Diagram Program block of the ladder model and Step Forward through the simulation. The software uses the inputs provided, runs a behavioral simulation, and animates the ladder rungs and blocks based on the execution state.

  6. You can continue stepping forward or run a continuous simulation to the end.

See Also

| | | | | |

Related Topics