Getting a rolling count

Hello again,
Our team is currently trying to find a way to read a rate of increase on a variable over the course of half a second, in order to get a rate of how much it would theoretically rise per minute.
For example:
If our counter went from 0-1 in the course of half a second, the output would be 120 counts per minute.

We’re doing this to get the total rate of one of our pumps, and while we have figured out the counter portion, we’re running into a brick wall translating that into a rate. We are mainly using the ladder logic, but not opposed to using ST.

Thank you for any help in advance!


I’m not 100% certain if the HW supports the TIME-type conversions necessary so I would do this fully with integers myself, by counting how many code execution cycles it takes for the counter to increase from 0 to >=1 (for example 50 with 10ms cycles in your example above). Then as your desired flow rate time (per minute) is known and how many cycles that would take to execute can be calculated (6000 pcs of 10ms cycles), you can simply divide the full time with the required time: 6000 / 50 = 120.

The resolution of the calculation can be enhanced by moving the algorithm to faster time level and the integers could be changed to REAL-types at some point prior to the division to handle the very low flow rates.


1 Like

Structured text is a more natural language to solve this problem because of the math involved but that is largely personal preference.

A thing to think about when using a plc a cyclic routine is that there are going to be scans where there are not going to be pulses. This will make the pulses per minute be 0 for some scans or higher than expected for other scans. A IIR filter is probably the easiest to implement to fix this. Something like this should do:


The 0.8 and 0.2 values can be adjusted to have faster or slower averaging, but make sure that the sum of the two values =1.

I would just use the SysTimeGetUs(); function to measure the time between scans and also account for jitter. The jitter of the PR1 or PR2 seems to be about 1ms

There is also OSCAT Cycle_Time function block to get the timing information.

1 Like


On the bottom is a link to a Function Block for Codesys.

It is in a Library container, to use it:
1: open it and in Codesys.
2: Build it with no Errors (Check all Pool Objects).
3: Find the Button in Files, (Save Project and Install Into Library Repository).
4: Close the Library Project.
5: Open your Device project, and pull The Water Treatment Library in using the Library Manager.
6: Now you should be able to use it.

1 Like

Sorry everyone, been dealing with multiple projects.

Edwurt, I got the library container to work for the most part, the only issue I’m running into now is that the count continuosly increases. It’s as if the buffer is only working half the time but I have been going through it trying to refine it some more.

Reeves, this is my first time really diving into structured text and I’ll be honest, it’s fun to learn but it’s still a slow learning process haha


I think the problem you are having with that block is, it doesn’t have enough data or the data gets processed too quickly due to the fact that the old FB was cycling at 500ms. So if there is no pulse within 500 ms, but right after we get a pulse in, it will drop to 0 for the past cycle.

I have updated the FB to look at a full minute to get more data to look at. If you’re still interested, please download the FB located in the Orignal post to update your FB.

Of topic, but still inclusive. I switch from LD to ST and I am not looking back. ST is faster to enter using the keyboard instead of riding the mouse all day for LD. And to be clear, for me, it is just as awesome processing bit (on-off) functions as it is doing calculations. 1 thing to take seriously. go in full throttle, doing half LD, and half ST didn’t help me(Maybe it could work for you). But it is just a step. Especially with Codesys. they have some great help files right in the program. To continue, using the Library development path to solve complex issues is a big advantage over the conventional method,(Flat coding, LD, or ST). scalability is so much easier utilizing Library management. Library management made it easy to switch from Schneider(EcoStruxure Machine Expert) to Opto 22 grove EPIC processors.

2 of the best references on ST available are.
1: PLC Controles with Structured Text. by Tom Mejer Antonsen.
2: The Book of Codesys. by Gary L. Pratt.

Which you the best.