zaro

How to create a timer in Simulink?

Published in Simulink Timing 4 mins read

To create a timer in Simulink, you typically set up a timer-driven task within a Subsystem block. This method leverages the concept of sample time to achieve periodic execution, effectively acting as a timer for the logic enclosed.

Understanding Timer-Driven Tasks in Simulink

A timer-driven task in Simulink ensures that a specific set of operations or calculations within a subsystem executes at precise, periodic intervals. This is fundamental for real-time applications and simulating embedded systems where tasks need to run based on a timer interrupt. The core mechanism involves setting a discrete sample time for an Inport block within a Subsystem, dictating how often the subsystem's contents are evaluated.

Step-by-Step Guide: Creating a Timer-Driven Task

Follow these steps to implement a timer using a Subsystem and its Inport block, based on the provided reference:

1. Initial Setup

  1. Create a new blank model: Begin by opening Simulink and creating a fresh, empty model. This provides a clean canvas for your design.
  2. Add a Subsystem block to the model: From the Simulink Library Browser, drag and drop a Subsystem block onto your new model. This block will encapsulate the logic that your timer drives.

2. Configuring the Timer (Subsystem)

  1. Open the subsystem block: Double-click on the Subsystem block you just added. This will open a new canvas, showing the default Inport and Outport blocks inside.

  2. Open the Block parameters dialog box of the Inport block, set the Sample Time to 0.1:

    • Inside the Subsystem, double-click on the Inport block (usually named In1).
    • In the Block Parameters dialog box, locate the Sample Time parameter.
    • Set its value to 0.1. This means the subsystem's contents will execute every 0.1 seconds (or 100 milliseconds), effectively defining your timer's period.
    • Click OK to apply the change.

    Example: If you want your timer to trigger every 500 milliseconds, you would set the Sample Time to 0.5.

3. Global Model Configuration

  1. In the Simulink editor, open the Configuration Parameters dialog box:
    • Navigate back to your top-level model window.
    • On the Modeling tab, click Model Settings (or press Ctrl+E). This opens the Configuration Parameters dialog box.
    • Under the Solver pane, ensure that a fixed-step solver (e.g., ode3, ode4, discrete) is selected, and the Fixed-step size is set appropriately. For systems with discrete sample times, it's often beneficial to set the fixed-step size to the smallest sample time in your model (or an integer multiple/divisor thereof) to ensure accurate execution of timed tasks.

Practical Considerations and Insights

  • Role of Sample Time: The Sample Time parameter in the Inport block is critical. It defines the discrete period at which the block's output (and thus the subsystem's execution) is updated. This is how Simulink simulates a periodic timer.
  • Fixed-Step Solvers: For accurate timer-driven execution, it's highly recommended to use a fixed-step solver. Variable-step solvers adjust their step size during simulation, which can lead to inconsistencies when precise timing is required for discrete events.
  • Inside the Subsystem: Once configured, any blocks or logic you place inside this subsystem will execute according to the defined sample time (e.g., every 0.1 seconds in the example). This is where you would place the "task" that needs to be performed periodically.
  • External Triggers: While the Inport block defines the timer, you might need to connect a signal to it from the outside if your subsystem requires an input. If it's purely an internal timer driving a task, the Inport block can simply serve to define the sample time.

By following these steps, you create a robust timer-driven mechanism in Simulink, ideal for managing periodic tasks and emulating real-time system behavior.

For more detailed information, refer to the MathWorks documentation on timer-driven tasks.