Main Content

Simulate, Model, and Generate Code for Timer-Based Ladder Logic

Model and simulate ladder logic for a simple timer-based motor controller. The ladder logic based controller uses an on-delay timer to delay the start of the motor and an off-delay timer to delay the stopping of the motor. After verifying that the controller and timers function for your requirements generate code for the controller.

Model Description

The model consists of a Motor Controller block that implements the ladder logic used to control the motor. The model contains a Human Machine Interface (HMI) block that enables you to interact with the model. Open the model:


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:

  • Inputs: You use the Start and Stop toggle switches to change the value of the respective inputs. When the toggle switch is in the On position, the value of the corresponding input is 1.

  • Motor: Indicates motor status. The green-colored Motor ON indicator means that the motor is running. The gray-colored Motor ON indicator means that the motor is stopped.

  • Graphs: Displays the status of Start, Stop, and Motor against time as the model simulation progresses.

Motor Controller

The Motor Controller block is a PLC Controller block. It contains a Ladder Program block that houses the ladder logic. To view the ladder logic for the controller, open the Motor Controller block, and then open the Ladder Diagram Program block.

This image shows the ladder logic implementation of the timer-based motor controller inside the Ladder Diagram Program block.

This ladder logic has a TON timer named T1 that is responsible for the delay when starting the motor. The ladder logic has a TOF timer named T2 that is responsible for the delay when stopping the motor. T1 has a preset of 5 seconds and T2 has a preset of 10 seconds.

When the Start input is toggled to 1, the MotorStart output in the first rung is activated which starts the timer T1 counting operation. The T1.DN bit is set when T1 finishes counting to 5 seconds. The third rung with timer T2 is then activated. Because T2 is a TOF timer, the T2.DN bit is set. The timer starts the counting operation only when this rung becomes false. Both the inputs to the lowermost rung are true and the Motor output is activated.

When the Stop input is toggled to 1, the MotorStart coil is deactivated and the T1.DN bit is reset. The timer T2 starts counting. Once T2 finishes counting to 10 seconds, the T2.DN bit is reset and the Motor output is deactivated.

Configure Timer Delay Values

You can configure the timer delay value for T1 and T2 by specifying their delay values inside the InitFcn callback property for the model. To access this setting, click Modeling > Model Settings > Model Properties > Callbacks >InitFcn.

To specify a preset delay, enter the value in milliseconds into the TimerName_InitialValue.PRE. For example, T1_InitialValue.PRE is set to 5000, which means a time delay of 5 seconds and T2_InitialValue.PRE is set to 10,000, which means a time delay of 10 seconds. You can simulate different start and stop delays by changing these timer presets to different values.

Simulate Model and Generate Code

Simulate the model and observe the graphs in the HMI block. Turn on the Start button in the HMI. The Motor ON light turns green after five seconds.

Turn on the Stop button. The Motor ON light turns gray after 10 seconds.

After verifying that the timers function for your requirements, generate code for the Motor Controller block

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

### Generated ladder files:

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

See Also