0
0
SimulinkHow-ToBeginner · 4 min read

How to Simulate PLC Logic in Simulink: Step-by-Step Guide

To simulate PLC logic in Simulink, use the Stateflow tool to model state machines and control logic visually. Combine Simulink blocks with Stateflow charts to replicate ladder logic and sequential control behavior for PLC systems.
📐

Syntax

Simulating PLC logic in Simulink involves these main parts:

  • Stateflow Chart: Visual tool to create state machines and flow logic similar to PLC ladder diagrams.
  • Simulink Blocks: Use blocks like Switch, Relational Operator, and Logical Operator to build control logic.
  • Inputs and Outputs: Define signals that represent sensors and actuators connected to the PLC.

Basic syntax to create a Stateflow chart in Simulink is:

open_system('model_name');
add_block('stateflow/Chart', 'model_name/Chart');

This adds a Stateflow chart block to your Simulink model where you can draw PLC logic states and transitions.

matlab
open_system('myPLCModel');
add_block('stateflow/Chart', 'myPLCModel/PLC_Logic');
Output
Simulink model 'myPLCModel' opens with a new Stateflow chart block named 'PLC_Logic'.
💻

Example

This example shows how to simulate a simple PLC logic that turns a motor ON when a start button is pressed and OFF when a stop button is pressed.

We use a Stateflow chart with two states: MotorOff and MotorOn. Transitions depend on input signals start and stop.

matlab
function simulatePLCLogic()
    % Create a new Simulink model
    model = 'simplePLC';
    new_system(model);
    open_system(model);

    % Add Stateflow chart
    chart = add_block('stateflow/Chart', [model '/PLC_Logic']);
    set_param(chart, 'Position', [100 100 300 300]);

    % Open Stateflow editor
    sfChart = sfroot.find('-isa', 'Stateflow.Chart', '-and', 'Path', [model '/PLC_Logic']);

    % Add states
    motorOff = Stateflow.State(sfChart);
    motorOff.Name = 'MotorOff';
    motorOff.Position = [50 50 90 60];

    motorOn = Stateflow.State(sfChart);
    motorOn.Name = 'MotorOn';
    motorOn.Position = [200 50 90 60];

    % Add transitions
    t1 = Stateflow.Transition(sfChart);
    t1.Source = motorOff;
    t1.Destination = motorOn;
    t1.LabelString = 'start == 1';

    t2 = Stateflow.Transition(sfChart);
    t2.Source = motorOn;
    t2.Destination = motorOff;
    t2.LabelString = 'stop == 1';

    % Set default state
    sfChart.Default = motorOff;

    % Add inputs
    inputStart = Stateflow.Data(sfChart);
    inputStart.Name = 'start';
    inputStart.Scope = 'Input';
    inputStart.Props.DataType = 'boolean';

    inputStop = Stateflow.Data(sfChart);
    inputStop.Name = 'stop';
    inputStop.Scope = 'Input';
    inputStop.Props.DataType = 'boolean';

    % Add output
    outputMotor = Stateflow.Data(sfChart);
    outputMotor.Name = 'motorRunning';
    outputMotor.Scope = 'Output';
    outputMotor.Props.DataType = 'boolean';

    % Define output in states
    motorOff.Entry = 'motorRunning = false;';
    motorOn.Entry = 'motorRunning = true;';

    % Save and run simulation
    save_system(model);
    sim(model, 10);
end
Output
Simulink model 'simplePLC' opens with a Stateflow chart simulating motor control logic; simulation runs for 10 seconds.
⚠️

Common Pitfalls

Common mistakes when simulating PLC logic in Simulink include:

  • Not defining input and output data types correctly in Stateflow, causing simulation errors.
  • Forgetting to set the default state in the Stateflow chart, which leads to undefined initial behavior.
  • Using continuous-time blocks instead of discrete-time blocks, which can mismatch PLC scan cycles.
  • Not connecting input signals properly from Simulink to Stateflow inputs.

Example of a common error and fix:

% Wrong: Missing default state
% Causes simulation error

% Right: Set default state
sfChart.Default = motorOff;
📊

Quick Reference

Tips for simulating PLC logic in Simulink:

  • Use Stateflow for clear state-based logic like ladder diagrams.
  • Define all inputs and outputs with correct data types (usually boolean).
  • Set the default state to avoid simulation startup errors.
  • Use discrete solvers to mimic PLC scan cycles.
  • Test logic with simple input signals before adding complexity.

Key Takeaways

Use Stateflow charts in Simulink to model PLC logic visually with states and transitions.
Always define inputs and outputs with correct data types and set a default state in Stateflow.
Combine Stateflow with Simulink blocks to simulate sensor inputs and actuator outputs.
Use discrete solvers to better represent PLC scan timing during simulation.
Test your PLC logic with simple input signals before building complex control systems.