PLC Logic Programming Part 3
A counter is a PLC instruction that either increments (counts up) or decrements (counts down) an integer number value when prompted by the transition of a bit from 0 to 1 (“false” to “true”). Counter instructions come in three basic types: up counters, down counters, and up/down counters. Both “up” and “down” counter instructions have single inputs for triggering counts, whereas “up/down” counters have two trigger inputs: one to make the counter increment and one to make the counter decrement.
To illustrate the use of a counter instruction, we will analyze a PLC-based system designed to count objects as they pass down a conveyor belt:
We will now analyze a simple ladder diagram program designed to increment a counter instruction each time the light beam breaks:
This particular counter instruction (CTU) is an incrementing counter, which means it counts “up” with each off-to-on transition input to its “CU” input. The normally-closed virtual contact (IN sensor object) is typically held in the “open” state when the light beam is continuous, by virtue of the fact the sensor holds that discrete input channel energized while the beam is continuous. When the beam is broken by a passing object on the conveyor belt, the input channel de-energizes, causing the virtual contact IN sensor object to “close” and send virtual power to the “CU” input of the counter instruction. This increments the counter just as the leading edge of the object breaks the beam. The second input of the counter instruction box (“R”) is the reset input, receiving virtual power from the contact IN switch reset whenever the reset pushbutton is pressed. If this input is activated, the counter immediately resets its current value (CV) to zero.
Status indication is shown in this ladder diagram program, with the counter’s preset value (PV) of 25 and the counter’s current value (CV) of 0 shown highlighted in blue. The preset value is something programmed into the counter instruction before the system put into service, and it serves as a threshold for activating the counter’s output (Q), which in this case turns on the count indicator lamp (the OUT counts reached coil). According to the IEC 61131-3 programming standard, this counter output should activate whenever the current value is equal to or greater than the preset value (Q is active if CV ≥ PV).
This is the status of the same program after thirty objects have passed by the sensor on the conveyor belt. As you can see, the current value of the counter has increased to 30, exceeding the preset value and activating the discrete output:
If all we did not care about maintaining an accurate total count of objects past 25 – but merely wished the program to indicate when 25 objects had passed by – we could also use a down counter instruction preset to a value of 25, which turns on an output coil when the count reaches zero:
Here, a “load” input causes the counter’s current value to equal the preset value (25) when activated. With each sensor pulse received, the counter instruction decrements. When it reaches zero, the Q output activates.
A potential problem in either version of this object-counting system is that the PLC cannot discriminate between forward and reverse motion on the conveyor belt. If, for instance, the conveyor belt were ever reversed in direction, the sensor would continue to count objects that had already passed by before (in the forward direction) as those objects retreated on the belt. This would be a problem because the system would “think” more objects had passed along the belt (indicating greater production) than actually did.
One solution to this problem is to use an up/down counter, capable of both incrementing (counting up) and decrementing (counting down), and equip this counter with two light-beam sensors capable of determining direction of travel. If two light beams are oriented parallel to each other, closer than the width of the narrowest object passing along the conveyor belt, we will have enough information to determine direction of object travel:
This is called quadrature signal timing, because the two pulse waveforms are approximately 90o (one-quarter of a period) apart in phase. We can use these two phase-shifted signals to increment or decrement an up/down counter instruction, depending on which pulse leads and which pulse lags.
A ladder diagram PLC program designed to interpret the quadrature pulse signals is shown here, making use of negative-transition contacts as well as standard contacts:
The counter will increment (count up) when sensor B de-energizes only if sensor A is already in the de-energized state (i.e. light beam A breaks before B). The counter will decrement (count down) when sensor A de-energizes only if sensor B is already in the de-energized state (i.e. light beam B breaks before A).
Note that the up/down counter has both a “reset” (R) input and a “load” input (“LD”) to force the current value. Activating the reset input forces the counter’s current value (CV) to zero, just as we saw with the “up” counter instruction. Activating the load input forces the counter’s current value to the preset value (PV), just as we saw with the “down” counter instruction. In the case of an up/down counter, there are two Q outputs: a QU (output up) to indicate when the current value is equal to or greater than the preset value, and a QD (output down) to indicate when the current value is equal to or less than zero.
Note how the current value (CV) of each counter shown is associated with a tag name of its own, in this case parts counted. The integer number of a counter’s current value (CV) is a variable in the PLC’s memory just like boolean values such as IN sensor A and IN switch reset, and may be associated with a tag name or symbolic address just the same8. This allows other instructions in a PLC program to read (and sometimes write!) values from and to that memory location.
A timer is a PLC instruction measuring the amount of time elapsed following an event. Timer instructions come in two basic types: on-delay timers and off-delay timers. Both “on-delay” and “off-delay” timer instructions have single inputs triggering the timed function.
An “on-delay” timer activates an output only when the input has been active for a minimum amount of time. Take for instance this PLC program, designed to sound an audio alarm siren prior to starting a conveyor belt. To start the conveyor belt motor, the operator must press and hold the “Start” pushbutton for 10 seconds, during which time the siren sounds, warning people to clear away from the conveyor belt that is about to start. Only after this 10-second start delay does the motor actually start (and latch “on”):
Similar to an “up” counter, the on-delay timer’s elapsed time (ET) value increments once per second until the preset time (PT) is reached, at which time its output (Q) activates. In this program, the preset time value is 10 seconds, which means the Q output will not activate until the “Start” switch has been depressed for 10 seconds. The alarm siren output, which is not activated by the timer, energizes immediately when the “Start” pushbutton is pressed.An important detail regarding this particular timer’s operation is that it be non-retentive. This means the timer instruction should not retain its elapsed time value when the input is de-activated.
Instead, the elapsed time value should reset back to zero every time the input de-activates. This ensures the timer resets itself when the operator releases the “Start” pushbutton. A retentive on-delay timer, by contrast, maintains its elapsed time value even when the input is de-activated. This makes it useful for keeping “running total” times for some event.
Most PLCs provide retentive and non-retentive versions of on-delay timer instructions, such that the programmer may choose the proper form of on-delay timer for any particular application.
The IEC 61131-3 programming standard, however, addresses the issue of retentive versus nonretentive timers a bit differently. According to the IEC 61131-3 standard, a timer instruction may be specified with an additional enable input (EN) that causes the timer instruction to behave nonretentively when activated, and retentively when de-activated. The general concept of the enable (EN) input is that the instruction behaves “normally” so long as the enable input is active (in this case, non-retentive timing action is considered “normal” according to the IEC 61131-3 standard), but the instruction “freezes” all execution whenever the enable input de-activates. This “freezing” of operation has the effect of retaining the current time (CT) value even if the input signal de-activates.
For example, if we wished to add a retentive timer to our conveyor control system to record total run time for the conveyor motor, we could do so using an “enabled” IEC 61131-3 timer instruction like this:
When the motor’s contactor bit (OUT contactor) is active, the timer is enabled and allowed to time. However, when that bit de-activates (becomes “false”), the timer instruction as a whole is disabled, causing it to “freeze” and retain its current time (CT) value9. This allows the motor to be started and stopped, with the timer maintaining a tally of total motor run time.
If we wished to give the operator the ability to manually reset the total run time value to zero, we could hard-wire an additional switch to the PLC’s discrete input card and add “reset” contacts to the program like this:
Whenever the “Reset” switch is pressed, the timer is enabled (EN) but the timing input (IN) is disabled, forcing the timer to (non-retentively) reset its current time (CT) value to zero.
The other major type of PLC timer instruction is the off-delay timer. This timer instruction differs from the on-delay type in that the timing function begins as soon as the instruction is deactivated, not when it is activated. An application for an off-delay timer is a cooling fan motor control for a large industrial engine. In this system, the PLC starts an electric cooling fan as soon as the engine is detected as rotating, and keeps that fan running for two minutes following the engine’s shut-down to dissipate residual heat:
The following timing diagrams compare and contrast on-delay with off-delay timers:
While it is common to find on-delay PLC instructions offered in both retentive and non-retentive forms within the instruction sets of nearly every PLC manufacturer and model, it is almost unheard of to find retentive off-delay timer instructions. Typically, off-delay timers are non-retentive only10.
As we have seen with counter and timers, some PLC instructions generate digital values other than simple Boolean (on/off) signals. Counters have current value (CV) registers and timers have elapsed time (ET) registers, both of which are typically integer number values. Many other PLC instructions are designed to receive and manipulate non-Boolean values such as these to perform useful control functions.
The IEC 61131-3 standard specifies a variety of data comparison instructions for comparing two non-Boolean values, and generating Boolean outputs. The basic comparative operations of “less than” (<), “greater than” (>), “less than or equal to” (≤), “greater than or equal to” (≥), “equal to” (=), and “not equal to” (6=) may be found as a series of “box” instructions in the IEC standard:
The Q output for each instruction “box” activates whenever the evaluated comparison function is “true” and the enable input (EN) is active. If the enable input remains active but the comparison function is false, the Q output de-activates. If the enable input de-de-activates, the Q output retains its last state.
A practical application for a comparative function might be to compare the total run-time of two redundant electric motors11, the PLC determining which motor to turn on next based on which motor has run the least:
Many industrial processes require control actions to take place in certain, predefined sequences. Batch processes are perhaps the most striking example of this, where materials for making a batch must be loaded into the process vessels, parameters such as temperature and pressure controlled during the batch processing, and then discharge of the product monitored and controlled. Before the advent of reliable programmable logic devices, this form of sequenced control was usually managed by an electromechanical device known as a drum sequencer. This device worked on the principle of a rotating cylinder (drum) equipped with tabs to actuate switches as the drum rotated into certain positions. If the drum rotated at a constant speed (turned by a clock motor), those switches would actuate according to a timed schedule12.
The following photograph shows a drum sequencer with 30 switches. Numbered tabs on the circumference of the drum mark the drum’s rotary position in one of 24 increments. With this number of switches and tabs, the drum can control up to thirty discrete (on/off) devices over a series of twenty-four sequenced steps:
A typical application for a sequencer is to control a Clean In Place (CIP) system for a food processing vessel, where a process vessel must undergo a cleaning cycle to purge it of any biological matter between food processing cycles. The steps required to clean the vessel are well-defined and must always occur in the same sequence in order to ensure hygienic conditions. An example timing chart is shown here:
In a general sense, the operation of a drum sequencer is that of a state machine: the output of the system depends on the condition of the machine’s internal state (the drum position), not just the conditions of the input signals. Digital computers are very adept at implementing state functions, and so the general function of a drum sequencer should be (and is) easy to implement in a PLC. Other PLC functions we have seen (“latches” and timers in particular) are similar, in that the PLC’s output at any given time is a function of both its present input condition(s) and its past input condition(s). Sequencing functions expand upon this concept to define a much larger number of possible states (“positions” of a “drum”), some of which may even be timed.
Unfortunately, despite the utility of drum sequence functions and their ease of implementation in digital form, there seems to be very little standardization between PLC manufacturers regarding sequencing functions. Sadly, the IEC 61131-3 standard (at least at the time of this writing, in 2009) does not specifically define a sequencing function suitable for ladder diagram programming. PLC manufacturers are left to invent sequencing instructions of their own design.
1Called the data table, this map shows the addressing of memory areas reserved for programs entered by the user. Other areas of memory exist within the SLC 500 processor, but these other areas are inaccessible to the technician writing PLC programs.
2Some systems such as the Texas Instruments 505 series used “X” labels to indicate discrete input channels and “Y” labels to indicate discrete output channels (e.g. input X9 and output Y14). This same labeling convention is still used by Koyo in its DirectLogic and “Click” PLC models. Siemens continues a similar tradition of I/O addressing by using the letter “I” to indicate discrete inputs and the letter “Q” to indicate discrete outputs (e.g. input channel I0.5 and output Q4.1).
3This particular program and editor is for the Koyo “Click” series of micro-PLCs.
4If this were a legacy Allen-Bradley PLC system using absolute addressing, we would be forced to address the three sensor inputs as I:1/0, I:1/1, and I:1/2 (slot 1, channels 0 through 2), and the indicator lamp output as O:2/0 (slot 2, channel 0). If this were a newer Logix5000 Allen-Bradley PLC, the default tag names would be Local:1:I.Data.0, Local:1:I.Data.1, and Local:1:I.Data.2 for the three inputs, and Local:2:O.Data.0 for the output. However, in either system we have the ability to assign symbolic addresses so we have a way to reference the I/O channels without having to rely on these cumbersome labels. The programs showing in this book exclusively use tag names rather than absolute addresses, since this is the more modern programming convention.
5The particular input and output channels chosen for this example are completely arbitrary. There is no particular reason to choose input channels 6 and 7, or output channel 2, as I have shown in the wiring diagram. Any available I/O channels will serve the purpose quite adequately.
6While it is possible to wire the overload contact to one of the PLC’s discrete input channels and then program a virtual overload contact in series with the output coil to stop the motor in the event of a thermal overload, this strategy would rely on the PLC to perform a safety function which is probably better performed by hard-wired circuitry.
7Referred to as “Latch” and “Unlatch” coils by Allen-Bradley.
8This represents the IEC 61131-3 standard, where each variable within an instruction may be “connected” to its own arbitrary tag name. Other programming conventions may differ somewhat. The Allen-Bradley Logix5000 series of controllers is one of those that differs, following a convention reminiscent of structure element addressing in the C programming language: each counter is given a tag name, and variables in each counter are addressed as elements within that structure. For example, a Logix5000 counter instruction might be named parts count, with the accumulated count value (equivalent to the IEC’s “current value”) addressed as parts count.ACC (each element within the counter specified as a suffix to the counter’s tag name).
9The “enable out” (ENO) signal on the timer instruction serves to indicate the instruction’s status: it activates when the enable input (EN) activates and de-activates when either the enable input de-activates or the instruction generates an error condition (as determined by the PLC manufacturer’s internal programming). The ENO output signal serves no useful purpose in this particular program, but it is available if there were any need for other rungs of the program to be “aware” of the run-time timer’s status.
10The enable (EN) input signals specified in the IEC 61131-3 programming standard make retentive off-delay timers possible (by de-activating the enable input while maintaining the “IN” input in an inactive state), but bear in mind that most PLC implementations of timers do not have separate EN and IN inputs. This means (for most PLC timer instructions) the only input available to activate the timer is the “IN” input, in which case it is impossible to create a retentive off-delay timer (since such a timer’s elapsed time value would be immediately re-set to zero each time the input re-activates).
11Perhaps two pumps performing the same pumping function, one serving as a backup to the other.
12The operation of the drum is not unlike that of an old player piano, where a strip of paper punched with holes caused hammers in the piano to automatically strike their respective strings as the strip was moved along at a set speed, thus playing a pre-programmed song.
I will be posting updates to this article with regards to the following:
Structured Text (ST)
Instruction List (IL)
Function Block Diagram (FBD)
Sequential Function Chart (SFC)
- Learn Ladder Logic with a Free Version of RSLogix 500 and RSEmulator 500
- A Quick Tutorial on RSLogix Emulator 5000
- Editing Device Message Type and I/O Sizes
- Mapping Device Input and Output Data
- What are the primary elements used for flow measurement ?
- What is a Linear Variable Differential Transformer (LVDT)?
- Drives 101: Adjustable / Variable Frequency Drives
- Basic DC Electricity for Industrial Instrumentation
- SteelAsia - Various positions
- Predicting the Maximum Input and Output Times for CIP Connections
- Configuring an RSLogix 5000 Produced and Consumed Tag
- Determining System Bandwidth Requirements
- Adding a Local or Remote 1756-I/O Module to an I/O Configuration
- Configuring a Message
- DC Electromagnetism
- AC Electricity : Transmission Lines
- Introduction to Industrial Instrumentation
- Example: Boiler Water Level Control System
- Example: Wastewater Disinfection
- Example: chemical reactor temperature control
- Other Types of Instruments
- Instrumentation Documents
- Configuring a 1771-SDN Scanner Module
- Configuring a Scanner Module for Slave Mode
- Instrumentation Documents - Process and Instrument Diagrams
- Instrumentation Documents - Process Flow Diagrams
- Instrumentation Documents - Loop Diagrams
- Instrumentation Documents - SAMA Diagrams
- Instrumentation Documents - Instrument and Process Equipment Symbols
- Instrumentation Documents - Instrumentation Identification Tags
- Programmable Logic Controllers
- PLC Logic Programming Part 1
- PLC Logic Programming Part 2
- Analog Electronic Instrumentation
- How To Teach Yourself PLC Programming
- PLC Examples
- PLC Input/Output (I/O) Capabilities
- Monitoring and Modifying Data Views and Tag Values Using an EtherNet/IP Web Server Module
- Accessing a Data View
- Creating a Data View in an EtherNet/IP Web Server Module
- Creating and Editing a Scanlist
- Digital Data Communication Theory
- Analog Electronics Instrumentation - Current Loops
- Troubleshooting Current Loops
- Introduction - Digital Data Acquisition and Networks
- Creating and Assigning a Tag and an Array of Tags
- EIA/TIA-232, 422, and 485 Networks
- Creating an Offline Network Configuration
- Saving a Network Configuration
- Ethernet Networks
- Internet Protocol (IP)
- Transmission Control Protocol (TCP) and User Datagram Protocol (UDP)
- The HART Digital/Analog Hybrid Standard
- Accessing Diagnostic Information about an EtherNet/IP Network Using a Web Server Module
- Sending Email Using a Web Server Module
- Managing User Accounts and Access Levels for an EtherNet/IP Web Server Module
- DeviceNet and RSNetWorx Procedures Guide Glossary
- Commissioning Nodes
- Configuring a Scanner Module
- Editing Device Parameters
- Appendix A – Performing Basic Mouse Operations
- Instrumentation and Control - Sensors and Primary Transducers
- Recovering Duplicate Node Addresses Using the Faulted Address Recovery (FAR) Wizard
- Resolving a Device Mismatch
- Format an Explicit Message Using a ControlLogix Controller
- Format an Explicit Message Using an SLC 500 Processor
- Enabling the Shared Input Option for a Scanner Module
- Generating Configuration Reports
- Viewing and Managing Messages
- Browsing a Network and Verifying Devices
- Monitoring Device Parameters
- Defining a Diagnostic Session
- Diagnosing a Network Using the DeviceNet MD Tool
- Managing EDS Files
- Configuring a Driver to Go Online to A DeviceNet Network
- Going Online to a Network
- Uploading and Downloading a Device or Network Configuration
- Displaying Hardware
- Enabling the Automatic Device Recovery (ADR) Feature
- SPEEDTRONIC Mark VI Hardware Description
- SPEEDTRONIC Mark VI Control Software Philosophy
- SPEEDTRONIC Mark VI Protection Systems