Main Content

Temperature Control Simulation and Code Generation Using Ladder Logic

This example shows how to model a temperature controller in Simulink® by using ladder logic. The example also showcases test case generation using Simulink Design Verifier™, C and ladder code generation, and ladder testbench generation.

The plcdemo_ladder_househeat_data.m file initializes data in the model workspace. To make changes, you can edit the model workspace directly or edit the file and re-load the model workspace. To view the model workspace, select MODELING > Model Explorer from the Simulink editor.

Step 1: Opening the Model

Open the plcdemo_ladder_househeat by using:

>> plcdemo_ladder_househeat_complete

Step 2: Model Initialization

When the model is opened, it loads the information about the house from the plcdemo_ladder_househeat_data.m file. The file does the following:

  • Defines the house geometry (size, number of windows)

  • Specifies the thermal properties of house materials

  • Calculates the thermal resistance of the house

  • Provides the heater characteristics (temperature of the hot air, ... flow-rate)

  • Defines the cost of electricity (0.09$/kWhr)

  • Specifies the initial room temperature (20 deg. Celsius = 68 deg. ... Fahrenheit)

Note: Time is given in units of hours. Certain quantities, like air flow-rate, are expressed per hour (not per second).

Step 3: Understanding Model Components

Set Point

Set Point is a constant block. It specifies the temperature that must be maintained indoors. It is 70 degrees Fahrenheit by default. Temperatures are given in Fahrenheit, but then are converted to Celsius to perform the calculations.


Range is a constant block. This specifies the range around the Set Point for the room temperature to fluctuate. It is 5 degrees Fahrenheit by default. Hence, the room temperature will fluctuate between $Set\_L$ and $Set\_H$ where

$$Set\_L = Set Point - Range$$

$$Set\_H = Set Point + Range$$

Temperature Controller

Temperature Controller is a subsystem that has a AOI Runner container. Inside this temperature controller AOI Runner is a Ladder Diagram Function Block. Double click this block and select Logic routine to view the ladder logic for the temperature controller.


House is a subsystem which models the outside environment, house and the heater dynamics. Refer to the Thermal Model of House example for more information on these.

Ladder Logic

Open the Temperature Controller > Ladder Diagram Function Block > Logic Routine to view the ladder diagram for the temperature controller

The first rung calculates the $Set\_L$ and $Set\_H$ values based on the Set Point and Range inputs. The GEQ activates the TEMP_H coil if the Room Temperature is greater than or equal to the SET_H. Similarly, The LEQ activates the TEMPL_L coil if the Room Temperature is less than or equal to the SET_L value.

The lowermost rung turns the heater ON if the TEMP_L output is active and turns the hearer OFF if the TEMP_H output is active.

Step 4: Run the Simulation

Click Run button to start simulation. Open HMI and use the following controls:

  • Temperature Controller Settings: Allows changing the Set Point and Range input values.

  • Ambient Temperature: Allows changing the average outside temperature and the range of its variation.

  • Room Temperature: Displays the heater status, and the inside and outside temperature graphs.

Step 5: Generate Simulink Design Verifier Test Cases

Preprocess the Simulink model for SLDV Test Case generation by executing the following command in the MATLAB Command Window:

>> plcladderoption (gcs, 'SLDV', 'on')

Open the Temperature Controller Subsystem and right click on the AOI Runner Block named Temperature Controller. Select Design Verifier > Generate test case for subsystem.

Step 6: Generate C code

Ensure that an ert.tlc is selected in the Code Generation tab of the Model Configuration Parameters. Preprocess the simulink model for C/C++ code generation by executing the following command in the MATLAB Command Window:

>> plcladderoption(gcs, 'FastSim', 'on');

Open the Temperature Controller Subsystem and right click on the AOI Runner Block named Temperature Controller. Select C/C++ Code > Build This Subsystem.

Step 7: Ladder code and testbench generation

Open ladder tank control testbench model:

>> plcdemo_ladder_househeat_tb

Select the Temperature Controller/ Temperature Controller Runner block and right-click PLC Coder->Generate Code for Subsystem to generate ladder code.

To generate testbench, select the Generate testbench for subsystem option in the PLC Configuration Parameters Dialog and generate code.