## Simulink Tutorial Series – 8

In this article, I am going to explain you how to design Reset counter(UP) and Reset counter(Down) in Simulink.

I will build the Simulink models for up down counter step-by-step.

Matlab/Simulink is the leading software for model based development in aerospace, automotive industry etc.

There are several other online resources, where you can get theoretical knowledge about Matlab/Simulink. In fact, Matlab has very good documentation for each of their products. But, there are not many resources to explain the theory along with good working examples. This is going to be a series of articles. This is the 8th article in this series.

In every article in this Simulink Tutorial Series, I will add real-life working examples and show how to build models for a particular problem.

## Problem Statements

Here is the Problem statements –

Design Reset counter(UP) and Reset counter(Down) in Simulink.

Design a Pause Counter in Simulink.

Note: This is also a popular interview question for Matlab/Simulink.

## Assumption

I am assuming, you have already gone through my previous articles –

In one of these articles, I have explained the delay block in Simulink Library. Understanding the delay block is crucial to implement the up down counter in Simulink.

## Design “Reset Counter(UP)” in Simulink

First of all, I need the following Simulink Library blocks to design reset counter(up) in Simulink and showcase a functionality:

2. Delay block
3. Switch block
4. Constant block
5. Display block
6. Scope block

If you have already gone through my previous articles, you already know how to find these library blocks in Simulink Library Browser and get them in a blank Simulink model.

In the previous article, I have shown the example of designing the Up counter in Simulink.

Here is the snapshot of the up counter model:

Now, I just need to modify the above model and add an additional switch block to reset the up counter after certain time. I am considering the reset threshold as 7. So whenever, the output of the model is 7 or above, the up counter output will reset back to 0.

You can also modify the reset_value parameter. In this example, I have chosen the reset_value as zero (0).

Now, I will save the model and execute it by hitting the green play button. You can double-click on the scope block to open the output graph in the scope window. Here is the scope window looks like:

As you can see above, the step size does not match with the sample time. I need to set the step-size as 1 in the configuration parameters window.

The solver type should be set to Fixed-Step and the Fixed-step-size should be set to as 1.

Let us now save the model and execute it again. As you can see below, now we are getting the right output – the initial output starts from zero, and the step size matches with the sample time. When the output reaches 7, it is getting reset back to 0. This is the expected behavior of the Reset Counter (UP).

## Design “Reset Counter(DOWN)” in Simulink

Now, let’s implement the Reset Counter (DOWN). I need the following Simulink Library blocks to design reset counter(down) in Simulink and showcase a functionality:

2. Delay block
3. Switch block
4. Constant block
5. Display block
6. Scope block

If you have already gone through my previous articles, you already know how to find these library blocks in Simulink Library Browser and get them in a blank Simulink model.

In the previous article, I have shown the example of designing the Down counter in Simulink.

Here is the snapshot of the up counter model:

Now, I need to modify the above model and add an additional switch block to reset the down counter after a certain time.

I am considering the reset threshold as 7. So, initially the down counter starts with the value as 7. It will be decremented by 1 in every cycle. When the down counter model output reaches to 0, it will reset to 7.

You can also modify the reset_value parameter. In this example, I have chosen the reset_value as zero (0).

Now, I will save the model and execute it by hitting the green play button. You can double-click on the scope block to open the output graph in the scope window. Here is the scope window looks like:

As you can see above, the step size does not match with the sample time. I need to set the step-size as 1 in the configuration parameters window.

The solver type should be set to Fixed-Step and the Fixed-step-size should be set to as 1.

Let us now save the model and execute it again. As you can see below, now we are getting the right output – the initial output starts from 7, and the step size matches with the sample time. When the output reaches 0, it is getting reset back to 7. This is the expected behavior of the Reset Counter (Down).

## Design Pause Counter in Simulink

So far, you have seen how to implement the Reset Counter (Up) and Reset Counter (Down) in Simulink.

Let us now implement the Pause Counter. If you do not know the behavior of pause counter, you can take a look at this image HERE.

I need the following Simulink Library blocks to design Pause Counter in Simulink and showcase a functionality:

2. Delay block
3. Switch block
4. Constant block
5. Display block
6. Scope block

If you have already gone through my previous articles, you already know how to find these library blocks in Simulink Library Browser and get them in a blank Simulink model.

I will just drag and drop all the required library blocks and join them up appropriately to implement the functionality of a pause counter.

Now, I will need to save the model and execute it by hitting the green play button.

You can double-click on the scope block to open the output graph in the scope window. Here is the scope window looks like:

Now, I will just edit the Model Configuration Parameters to set the solver type and step size.

You can find the Model Configuration Parameters option as shown below:

You can set the Solver type as Fixed-Step. The Fixed-step size (sample time) needs to be set to 1.

Let us now save the model and execute it again by hitting the green play button. Then double-click on the scope block.

Here is the output graph looks like in the scope graph window:

You can change the simulation time by just entering the desired time as shown below:

Here is the scope graph window when the simulation time is 20:

But, still as you can see above the initial step starts from 1 and not from zero. So, I need to tweak the model.

I am going to add a delay block in the else part of the switch. That way the first cycle value will get delayed by one cycle.

I have saved the model and executed again. Here is the scope graph:

As you can clearly see, the output start with zero instead of 1. This is the standard desired behavior of the pause counter.

However, you can other parameters such as step_value or the threshold to make it work differently, but the fundamental behavior will be the same.

So, we have successfully designed the Pause Counter in Simulink.

### Summary

Matlab/Simulink is a well known and very popular tool used for Model Based Software Development in the aerospace and automotive industry.

Today, in this article (Simulink Tutorial Series – 8), I have explained step-by-step, how to design Reset Counter(UP), Reset Counter(Down), and Pause Counter in the Simulink model using Simulink library blocks from scratch.

If you have any questions, please feel free to comment in the comment box below. 👇👇👇

I will keep sharing useful real-life Simulink model example here – Simulink Tutorial Series.

Happy learning!