Main Content

Model, Simulate, and Generate Code for a Ladder Logic-Based Temperature Controller

Model and simulate a ladder logic-based temperature controller of a home. The ladder logic controls the heater based on the thermostat settings and the outside ambient temperature. After verifying that the controller functions for your requirements, generate code for your controller.

Model Description

The model consists of a House block that models the thermal characteristics of the house and the heating system. The Temperature Controller block implements the ladder logic that controls the heater. The Human Machine Interface (HMI) block enables you to interact with the model.

Open the model:


Initialize Model

This model calculates heating costs for a generic house. Opening the model loads the information about the house from the plcdemo_ladder_househeat_data.m file. The file:

  • 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°C = 68° F)

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

Model Components

Set Point

Set Point specifies the temperature that must be maintained indoors. It is 70° Fahrenheit by default. Temperatures are given in °Fahrenheit. The model converts the temperature to °Celsius.


Range is a constant block. This specifies the range around the set point for the room temperature to fluctuate. It is 5° Fahrenheit by default. The room temperature varies between Set_L and Set_H where:



Model the Environment

The model uses a heat sink with infinite heat capacity and time varying temperature Tout to simulate the environment. The constant block Avg. Outdoor Temp specifies the average air temperature outdoors. The Daily Temp Variation Sine Wave block generates daily outdoor temperature fluctuations. You can vary these parameters to see how they affect the heating costs.

HMI Block

The HMI block consists of inputs and outputs that you use to interact with the model simulation. Open the HMI block by double-clicking the block. This image shows the components of the HMI block.

This HMI block contains:

  • Temperature Controller Settings: Use the Set Point and Range to specify the inputs to the temperature controller. The values are specified in ° Fahrenheit.

  • Ambient Temperature: Use the Outside Temperature to set Avg.Outdoor Temp. Use the Range slider to set the amplitude of the Daily Temp Variation Sine Wave block. The values are specified in ° Fahrenheit.

  • Room Temperature: The graph and dial indicate the room temperature in ° Fahrenheit. The red-colored Heater indicator means that the heater is on. The gray-colored Heater indicator means that the heater is off.

Temperature Controller

The Temperature Controller block is an Add-On Instruction (AOI) runner block. The AOI runner block contains a ladder logic implementation of the heater controls. To view the ladder logic open:

  • The Temperature Controller block,

  • The Temperature Controller Runner block

  • The Function Block

Then, select Logic Routine.

This image shows the ladder logic-based temperature controller inside the Logic Routine block.

The first rung calculates SET_H and SET_L. On the second rung, when the room temperature is greater than or equal to SET_H, OTE1 is on. On the third rung when the room temperature is lesser than or equal to SET_L, OTE2 is on. When OTE2 is on and OTE1 is off, the heater is on.

Simulate Model and Generate Code

Simulate the model and observe the graphs in the HMI block. Vary the Set Point, Range, Avg.Outdoor Temp to see how the heater turns on and off. In this image, the set point is 70° Fahrenheit, the range is set to 5° Fahrenheit, Ambient temperature is set to 50° Fahrenheit, and the amplitude of the disturbance is set to 15° Fahrenheit.

After verifying that the controller functions for your requirements, generate code for the Temperature Controller block.

plcgeneratecode('plcdemo_ladder_househeat_complete/Temperature Controller/Temperature Controller Runner')
### Generating PLC code for 'plcdemo_ladder_househeat_complete/Temperature Controller/Temperature Controller Runner'.
### Using model settings from 'plcdemo_ladder_househeat_complete' for PLC code generation parameters.
### Begin code generation for IDE studio5000.
### Emit PLC code to file.
### PLC ladder code generation successful for 'plcdemo_ladder_househeat_complete/Temperature Controller/Temperature Controller Runner'.

### Generated ladder files:

Alternatively generate code by selecting the Temperature Controller block, Temperature Controller Runner block, and then select APPS > PLC Coder. On the PLC Code tab, click Generate PLC Code.

To generate a test bench, select the Temperature Controller Runner block. On the PLC Code tab, click Settings > PLC Code Generation > Generate testbench for subsystem. Click Generate PLC Code.

Generate Simulink Design Verifier™ Test Cases

Execute this command:


Open the Temperature Controller block, right-click AOI Runner block, and select Design Verifier > Generate test case for subsystem.

See Also