Very Helpful. I plan to use this technique for an integrator in an earlier version of Simulink which does not provide the reset input on the integrator.
Just a note: I noticed that in your example the counter can be made to reset at each 5 second interval (instead of 6) by setting the threshold value to 4 instead of 5. Also seems to be a cleaner waveform.
Great video. Here is how my application works. I'm using an optical sensor to measure the revolutions on a disk. For that I put a pin on the disk so that every rotation it makes is counted as 1 pass on the optical sensor. I want to implement a counter on simulink that starts counting up once the pin passes through my optical sensor and resets itself on the next pass so that it starts counting back again. I managed to convert the analog signal coming from the sensor to a series of pulses so that its value is either one or zero. Now I'm on the counter part but still trying to figure it out. As I said earlier the idea is that for the first value of 1, the counter begins the counting and then on the next value of 1 it resets back to zero and starts counting again. If you have some idea on how to implement this please let me know. Again, great video. Keep up the good work.
Great video sir! I have a Simulink model where I have PWM values starting off at around ~160. I want to create a counter to count up to the ~160 approx and then disable the counter and PWM values should take over. From this video, I want to replace the 5 with the varying PWM, so the counter will count up to the PWM value and the counter should disable. Any suggestions will be much appreciated.
Hi Ajay, There can be various approach for this. The one I can quickly think of is to use the Enable subsystem block. Have the output of the counter in a switch block (controller input , u). Set the threshold of your switch to ~160. Once this threshold is crossed then switch will turn on one side. You can give Boolean input true or constant input 1 to that port of switch. Connect this switch block output to the enable subsystem condition signal. Thus the enable subsystem will get enabled only once the counter crosses 160. If needed, you can reset the counter value at 160 again to 0 as shown in the video. Hope this information helps.
@@ProgrammerWorld Thanks for your reply. I ended up hard coding it on the microcontroller. Thank you very much for your suggestion and it's a great tutorial. Much appreciated!
Thank you for the informative video. Actually I have some questions regarding counter time, can this counter time can be feed to stepper motor as this counter time shows the steps as per the time . I will be very happy to have your mail so that I can ask you some questions in personal. Thank you once again
For counter time to be fed in the stepper motor, one can easily use the clock block instead. However, yes, this counter can also be used. For contacting us, please reach out to us through our below page: programmerworld.co/contact/ For reference, the complete details of this tutorial is shared in the below link also: programmerworld.co/simulink/design-a-simple-counter-and-reset-counter-in-simulink/ Cheers Programmer World programmerworld.co -
Hi, I have 0 or 1 trigger as a input.. so let's say 1 is shows up for 50 secs and I want to limit it to 10sec. The first step is limit the sum of 1 showing time to 10 sec but the other part is I want to limit it for next 300 sec so that the counter should not start counting and once 300sec over it starts again then 10sec cut the counter hold it for 300 sec and so on. Can you help?
You shouldn't need to change the solver parameter. Just set the sample time to one second in the delay block and set -1 as initial condition to start it at zero.
Yes, you are right. Setting the sample time to 1 second in the delay block is a much better solution to set the counter step size equal to simulation time. For reference, details shown in this video is also shared in the below link: programmerworld.co/simulink/design-a-simple-counter-and-reset-counter-in-simulink/ Cheers Programmer World programmerworld.co -
Thanks for the suggestion. I will try to come up with such a demo. However, in the meantime you can check my below page. It may help. programmerworld.co/simulink/how-to-create-random-pwm-pulse-width-modulation-signal-with-fixed-duty-cycle-in-simulink-matlab-r2023a-using-random-number-generator-block/
Not this one.. Question is ' model a system inwhich 1) on the timer when the voltage is 5v 2)on the timer for 10second and off for the next 5second and repeat
Hello :) Thanks for the video, it is very helpful. I have one question about it. If I use your example, is it possible to count the time it takes for the counter to rise from 0 to 5? In my case, the time for each step is not 1 second, it varies, but my threshold is always the same. So, everytime when my counter reaches the threshold to switch back to zero, I would like to know how much time it took. Thank you!
If your question pertains to real clock time for simulation while counter is counting, then I do not think it can be directly done in the Simulink. You can use certain performance measurement techniques in Matlab (like tic-toc command) to measure the real world clock time it takes to simulate the counter for 5 simulation seconds. However, if you want to measure the simulation time in your model then you can simply use a clock block for the same. Good Luck Programmer World programmerworld.co -
Instead of 0 in the other option of Switch block, just implement the decreasing counter logic. Below video can help in implementing the down counter logic: th-cam.com/video/9seGi3vMWAc/w-d-xo.html th-cam.com/video/aUQ3RNH_oQs/w-d-xo.html Cheers Programmer World programmerworld.co/ -
1st use case: After every 2 cycles you want to reset the counter and begin counting again from 0. This case is explained in the later part of this video (after 6:30 mins) where it is shown how to design reset counter. 2nd use case: The other use case could be that you only want to count for 2 steps irrespective of simulation time of Simulink. In that case you have to use the reset part of the model with Threshold value of switch block being compared with a clock block's output. You can compare that when the clock gives output of more than 2 (2 seconds of simulation time) then your switch block's True condition gives a fixed output of 0. And hence the counter's output will be set to 0 after 2 cycle time. I hope the above explanation helps to design your requirement.
You can watch my below video: th-cam.com/video/wo0eIZq93aU/w-d-xo.html This video shows how to generate pulse signals using Simulink blocks. It does not shows the implementation in Stateflow. You can take a hint from this video for implementation in Simulink. If it will be possible then I will create another tutorial showing the implementation in Stateflow.
Then put a clock block and rest of the implementation in an enable subsystem. Once clock reaches your required threshold of 2 cycles, just disable the complete subsystem.
It can easily be done using a Mask Parameter and taking the value of the parameter in the Add block in the feedback loop of counter. I will try to make a video to explain this.
Once the counter reaches to zero you can either reset it as shown in my video tutorial. If you want to stop it completely then you can use conditional subsystems like Enable subsystems or If condition subsystems. In this subsystem once the counter value reaches to Zero you can disable it. So, the value of counter will not be considered anymore.
Please check if the below video helps: (How to design variable step increasing and decreasing counter in Simulink?) th-cam.com/video/FXGoe91n9GE/w-d-xo.html Good Luck Programmer World programmerworld.co -
Hi Ankita, You can watch my other video titled "How to design a Decreasing Counter and reset counter in Simulink?". This video shows how you can decrease the count in each simulation step. You can watch it using below link: th-cam.com/video/9seGi3vMWAc/w-d-xo.html
How to make this statement in simulink : Varying the current from 1 to 25A, in steps of 0.4 ampere, every 40 s over a total simulation period of 2720 s.
In this tutorial, the counter shown takes all the default values such as step size, increment value, etc. For your values, the specifications in the counter will need to be modified. Say for example, step-size should be 40s, Total simulation time = 2720s and increment value in every step to be 0.4Amps. Once you these changes in the model, you will get the model for your requirement. For further reference you can watch my other videos on counter topic using the below links: th-cam.com/video/FXGoe91n9GE/w-d-xo.html th-cam.com/video/aUQ3RNH_oQs/w-d-xo.html Hope the above helps. Good Luck Programmer World programmerworld.co -
Sir can you please tell me how to make this for a system containing continuous states: Varying the current from 1 to 25A, in steps of 0.4A, every 40sec over a total simulation period of 2720sec. Please help sir
Very Helpful. I plan to use this technique for an integrator in an earlier version of Simulink which does not provide the reset input on the integrator.
Thank you
Very Nice Tutorial
Just a note: I noticed that in your example the counter can be made to reset at each 5 second interval (instead of 6) by setting the threshold value to 4 instead of 5. Also seems to be a cleaner waveform.
Thank you for very good explanation
This is so helpful!!
Great video. Here is how my application works. I'm using an optical sensor to measure the revolutions on a disk. For that I put a pin on the disk so that every rotation it makes is counted as 1 pass on the optical sensor. I want to implement a counter on simulink that starts counting up once the pin passes through my optical sensor and resets itself on the next pass so that it starts counting back again. I managed to convert the analog signal coming from the sensor to a series of pulses so that its value is either one or zero. Now I'm on the counter part but still trying to figure it out. As I said earlier the idea is that for the first value of 1, the counter begins the counting and then on the next value of 1 it resets back to zero and starts counting again. If you have some idea on how to implement this please let me know. Again, great video. Keep up the good work.
Thank you kind sir
Great video sir! I have a Simulink model where I have PWM values starting off at around ~160. I want to create a counter to count up to the ~160 approx and then disable the counter and PWM values should take over. From this video, I want to replace the 5 with the varying PWM, so the counter will count up to the PWM value and the counter should disable. Any suggestions will be much appreciated.
Hi Ajay,
There can be various approach for this. The one I can quickly think of is to use the Enable subsystem block. Have the output of the counter in a switch block (controller input , u). Set the threshold of your switch to ~160. Once this threshold is crossed then switch will turn on one side. You can give Boolean input true or constant input 1 to that port of switch. Connect this switch block output to the enable subsystem condition signal. Thus the enable subsystem will get enabled only once the counter crosses 160. If needed, you can reset the counter value at 160 again to 0 as shown in the video. Hope this information helps.
@@ProgrammerWorld Thanks for your reply. I ended up hard coding it on the microcontroller. Thank you very much for your suggestion and it's a great tutorial. Much appreciated!
Thank you for the informative video. Actually I have some questions regarding counter time, can this counter time can be feed to stepper motor as this counter time shows the steps as per the time . I will be very happy to have your mail so that I can ask you some questions in personal. Thank you once again
For counter time to be fed in the stepper motor, one can easily use the clock block instead. However, yes, this counter can also be used.
For contacting us, please reach out to us through our below page:
programmerworld.co/contact/
For reference, the complete details of this tutorial is shared in the below link also:
programmerworld.co/simulink/design-a-simple-counter-and-reset-counter-in-simulink/
Cheers
Programmer World
programmerworld.co
-
Hi,
I have 0 or 1 trigger as a input.. so let's say 1 is shows up for 50 secs and I want to limit it to 10sec. The first step is limit the sum of 1 showing time to 10 sec but the other part is I want to limit it for next 300 sec so that the counter should not start counting and once 300sec over it starts again then 10sec cut the counter hold it for 300 sec and so on. Can you help?
You shouldn't need to change the solver parameter. Just set the sample time to one second in the delay block and set -1 as initial condition to start it at zero.
Yes, you are right. Setting the sample time to 1 second in the delay block is a much better solution to set the counter step size equal to simulation time.
For reference, details shown in this video is also shared in the below link:
programmerworld.co/simulink/design-a-simple-counter-and-reset-counter-in-simulink/
Cheers
Programmer World
programmerworld.co
-
thankyou.
Can you please make a model of timer which is on for 10 sec and off for 5 sec
Thanks for the suggestion. I will try to come up with such a demo.
However, in the meantime you can check my below page. It may help.
programmerworld.co/simulink/how-to-create-random-pwm-pulse-width-modulation-signal-with-fixed-duty-cycle-in-simulink-matlab-r2023a-using-random-number-generator-block/
Below may help. Please check:
programmerworld.co/simulink/how-to-design-pwm-signal-with-different-on-and-off-period-in-matlab-simulink/
Not this one.. Question is ' model a system inwhich 1) on the timer when the voltage is 5v
2)on the timer for 10second and off for the next 5second and repeat
Hello :) Thanks for the video, it is very helpful. I have one question about it. If I use your example, is it possible to count the time it takes for the counter to rise from 0 to 5? In my case, the time for each step is not 1 second, it varies, but my threshold is always the same. So, everytime when my counter reaches the threshold to switch back to zero, I would like to know how much time it took. Thank you!
If your question pertains to real clock time for simulation while counter is counting, then I do not think it can be directly done in the Simulink. You can use certain performance measurement techniques in Matlab (like tic-toc command) to measure the real world clock time it takes to simulate the counter for 5 simulation seconds.
However, if you want to measure the simulation time in your model then you can simply use a clock block for the same.
Good Luck
Programmer World
programmerworld.co
-
How to implement up counter and down counter in same logic.it should make triangle
Instead of 0 in the other option of Switch block, just implement the decreasing counter logic. Below video can help in implementing the down counter logic:
th-cam.com/video/9seGi3vMWAc/w-d-xo.html
th-cam.com/video/aUQ3RNH_oQs/w-d-xo.html
Cheers
Programmer World
programmerworld.co/
-
Sir...if I want the counter only for 2 cycles then what will be the design
1st use case: After every 2 cycles you want to reset the counter and begin counting again from 0. This case is explained in the later part of this video (after 6:30 mins) where it is shown how to design reset counter.
2nd use case: The other use case could be that you only want to count for 2 steps irrespective of simulation time of Simulink. In that case you have to use the reset part of the model with Threshold value of switch block being compared with a clock block's output. You can compare that when the clock gives output of more than 2 (2 seconds of simulation time) then your switch block's True condition gives a fixed output of 0. And hence the counter's output will be set to 0 after 2 cycle time.
I hope the above explanation helps to design your requirement.
If 3 bit how to proceed!
how can we generate a pulse in every 10 seconds in stateflow model??
You can watch my below video:
th-cam.com/video/wo0eIZq93aU/w-d-xo.html
This video shows how to generate pulse signals using Simulink blocks. It does not shows the implementation in Stateflow. You can take a hint from this video for implementation in Simulink. If it will be possible then I will create another tutorial showing the implementation in Stateflow.
If We dont want continuous output...if we want it only for two cycles then??
Then put a clock block and rest of the implementation in an enable subsystem. Once clock reaches your required threshold of 2 cycles, just disable the complete subsystem.
I want many different different increment and decrement and another output signal s
It can easily be done using a Mask Parameter and taking the value of the parameter in the Add block in the feedback loop of counter. I will try to make a video to explain this.
You can refer to below video. This video shows how you can design a variable step counter in Simulink:
th-cam.com/video/FXGoe91n9GE/w-d-xo.html
How to stop the counter once it comes to zero??
Once the counter reaches to zero you can either reset it as shown in my video tutorial.
If you want to stop it completely then you can use conditional subsystems like Enable subsystems or If condition subsystems. In this subsystem once the counter value reaches to Zero you can disable it. So, the value of counter will not be considered anymore.
what if the reset threshold is variable?
Please check if the below video helps:
(How to design variable step increasing and decreasing counter in Simulink?)
th-cam.com/video/FXGoe91n9GE/w-d-xo.html
Good Luck
Programmer World
programmerworld.co
-
How to design Decrement counter?
Hi Ankita,
You can watch my other video titled "How to design a Decreasing Counter and reset counter in Simulink?". This video shows how you can decrease the count in each simulation step. You can watch it using below link:
th-cam.com/video/9seGi3vMWAc/w-d-xo.html
How to make this statement in simulink :
Varying the current from 1 to 25A, in steps of 0.4 ampere, every 40 s over a total simulation period of 2720 s.
In this tutorial, the counter shown takes all the default values such as step size, increment value, etc. For your values, the specifications in the counter will need to be modified. Say for example, step-size should be 40s, Total simulation time = 2720s and increment value in every step to be 0.4Amps. Once you these changes in the model, you will get the model for your requirement.
For further reference you can watch my other videos on counter topic using the below links:
th-cam.com/video/FXGoe91n9GE/w-d-xo.html
th-cam.com/video/aUQ3RNH_oQs/w-d-xo.html
Hope the above helps.
Good Luck
Programmer World
programmerworld.co
-
@@ProgrammerWorld thank you sir
Sir can you please tell me how to make this for a system containing continuous states:
Varying the current from 1 to 25A, in steps of 0.4A, every 40sec over a total simulation period of 2720sec.
Please help sir
reset back to giro