PLC Basics: Ladder Logic Common Functions
Introduction To Ladder
Ladder logic is a staple of PLC programming, it is more often than not the most used language in a PLC program. It's used because it's easy to read, easy to use and lends itself to logical processes, especially where digital logic (relay logic) is concerned.
In this article, we'll be looking at basic ladder code that are the building blocks for any size project
Latching signals are common place in automation, especially in factories and process plants. Take a look at the image above, this ladder rung is a classic "Hold On" latch where the coil (the furthest right) variable is used again to hold itself on.
When "ON" is set to TRUE, and "OFF" is set to FALSE, the "Latch" is set to TRUE.
This then "holds its self on" through the "Latch" contact and stays on until the "OFF" is set to TRUE as seen below
Making a logic branch is simple, think of it as an OR command. In the above image you can see that there is a "fork" in the logic path after "Signal_1". If "Override" is TRUE, the logic bypasses Signals 2,3,4,5 and sets the "Output" to TRUE.
This logic is not just limited to overrides either, imagine if "Output" was actually a fault indication. The above logic would now be:
IF Signals 1,2,3,4,5 are all true OR Signal 1 and Override are TRUE then Output = True.
This would give "Override" a higher priority over all other signals when it comes to driving the fault indication.
Set & Reset Latches
Personally, I don't like this approach because I feel a coil (output) should only ever be written to in one place so you can see what is happening clearly. This design can leave the door open to the latch staying on unnoticed if you have a lot going on.
In the example above, the Latch has already been set by "Signal_1" momentarily becoming TRUE. Notice the "S" inside the coil for "Latch", this is the SET command. Once set, "Latch" will not return to FALSE until the RESET instruction is given (seen on the last line of the logic).
When "Signal_3" becomes TRUE, the "Latch" will become false and therefore "Output" will also become FALSE.
!!! This is not always the case though !!!
What happens when "Signal_1" AND "Signal_3" are both TRUE?
The "Output" is TRUE, even though "Latch" is FALSE?
This is because of the PLC scan. The PLC scans top to bottom and in this case, the SET is TRUE on line 1, therefore on line 2 "Latch" is TRUE and allows "Output" to become TRUE. However on line 3, "Signal_3" is driving the RESET and setting "Latch" to FALSE.
The reason it is displayed incorrectly is because most PLCs only update their views at the beginning or end of the scan. This would be the same if you were monitoring "Latch" when connected to a PLC too, you wouldn't see it flicking between 0 and 1, it would most likely just sit at 0 even though it IS driving an output. This is why I don't like using this method.
Its not uncommon to want to run a PLC as a sequencer, especially for conveyor like systems. The above example shows a very basic sequencer. Imagine that this was controlling a conveyor belt.
- Step 0 - Wait for a bottle to appear in front of a sensor (Signal_1)
- Step 1 - Wait for a completed signal of a process filling the bottle (Signal_2)
- Step 2 - Wait for a signal to show the bottle was in a position to be picked up by an employee ready to pack it (Signal_3)
- Step 3 - Wait 10 seconds before restarting the process
This is a very crude example, but you get the idea.
Lines 1 and 3 have a "Run" coil assigned, these drive the "Output" signal to TRUE on the last line. As "Output" is the signal to run the conveyor system, this means that the bottles on the conveyor can only be moved on step 0 and step 2.
Some more experienced readers may notice "Run.0" and "Run.1". This is because "Run" is declared as a BYTE and not a BOOL, this simply allows me to use the variable "RUN" as a group of signals, like an array (Not all PLCs let you do this!)
Self Resetting Timer
The above image shows a Timer (TON) function that immediately resets itself, leaving the "Q" output TRUE for only 1 PLC scan.
When Timer.Q is TRUE, the "ADD" function is enabled and increments the "Count" value.
This logic has so many different uses it would be impossible to list them all, it's definitely one worth knowing!
The above examples are literally just that, examples, but when put together and applied to a solution will get you much further than you expect. These functions serve as basic building blocks for a variety of different functions.
Get experimenting! On that note, the above images were made with CoDeSys, a free PLC tool. Take a look at it, its very good for beginners to get to grips with things!