Ladder Logic 307: Data Average and Archive

Today’s post is in response to Yusuf Shaikh, who asked a question in a Facebook forum on PLCs. He asked “How to calculate average of drive current tag for every 15 seconds in Micrologix 1400 PLC?

Of course there are various questions that need to be asked about an application like this, and it is very difficult to explain something like this in text on Facebook. He did state that the current was a floating point value, which means it has been scaled from an Integer from an analog input. For the purpose of demonstration I used F8:0.

15 Second Pulse Timer

I have explained this rung previously both on this blog and in my book. Sometimes you have to move this rung to after the calculation logic, but I didn’t have time to download this and test it. Suffice it to say, if you don’t get values in the next few rungs, move this to the end of the logic.

“Cascade” of moves

I have explained this logic also before, it’s also in Advanced PLC Hardware and Programming, which discusses different ways of moving data. Since there are only 4 values to average, this is the easiest way.

Add 4 values for averaging

This logic simply adds the four floating point values together for averaging. It can be combined with the next rung, but I didn’t have a way to easily copy such a big rung, so I separated them. And yes, you can continuously overwrite F8:5 in successive rungs.

Divide for the final average value

Divide by 4 and you have calculated a running average of current for a one minute period that updates every 15 seconds. So hopefully that is what Yusuf was looking for.

The next few rungs assumes that I also want to archive the data, perhaps so I can trend it. So I am going to save the last 100 averaged values. one value every minute, continuously.

Rung 4 is a new pulse occurring every minute. Again, you may have to move this rung to the end. Unfortunately in a Micrologix and in RSLogix500 itself, you can only use the FFL and FFU instructions on Integers. So I reverse scaled the Floating point number, multiplying it by 100 with an implied decimal point. This allows values to be displayed on a touchscreen as XXX.XX, but you can’t use this technique if current values are higher than 327 amps or so, since the highest value allowed in a signed integer is 32,767.

This is the FIFO Load and Unload Instruction, the core of archiving the data. Newest values enter the stack pushing all of the other values down, until full. So this would keep a running list of your averaged current values for 100 minutes. You could also archive these values, average them again, or display them. The FFU is needed because when the stack gets full, it won’t let you put any more on. When you unload, you remove the oldest value.

I mentioned on the Facebook forum that answering questions like this is time consuming, and of course people charge good money for programming. At the same time, I do have this blog, and I’m stuck at home… and I like to teach… so there it is.

I hope that answers your question Yusuf!


Electrical Engineer and business owner from the Nashville, Tennessee area. I also play music, Chess and Go.

Tagged with: , ,

2 Comments on “Ladder Logic 307: Data Average and Archive

    • You’re welcome Yusuf! I am always looking for ideas to post here, and things that come from people who have a real application are always best. If you or anyone else has any applications on Allen-Bradley or Siemens products let me know!

Leave a Reply

Your email address will not be published. Required fields are marked *