Tuesday, April 25, 2023

Timer & Counter

Timer with Ladder diagram

A timer is used with a PLC to determine the time interval through logical operations. For instance, in a PLC-controlled traffic signal system, a timer restricts the signaling system for a specified time interval. There are different types of timers, such as on-delay, off-delay, and pulse timers, that are functional within a PLC. Physically, timers do not have a separate existence in a PLC but rather have an inbuilt memory area reserved for them in the CPU. In a ladder diagram, a timer is represented by a box with parameters and titles like T5, T10, and T15. However, different PLC manufacturers use various symbols and appearances to represent timers in a ladder diagram.

The following discussion describes timers in line with Siemens PLCs, specifically S7 300 and S7 400. The symbols and presentation types mentioned here are applicable only to Siemens PLCs. However, regardless of the representation or programming style used with the PLC, the basic working principle remains the same for all types of timers in all other PLCs. 

A timer has an enable signal denoted by the symbol S. The S symbol represents the set instruction, and the reset or stopping of a timer is indicated by the letter R. This means a timer starts when the S point receives a logic 1 signal and stops when the R point receives a logic 1 signal (as shown in the previous picture). The output of a timer is represented by the letter Q and can be used to activate a coil or output through various circuits using normally open (NO) and normally closed (NC) logic. The time value of a timer is typically expressed with the symbol TV, and a typical format like 'S5T#aH_bM_cS_dMS' is used to specify the time value of a timer (applicable to Siemens PLCs only). Here, H stands for an hour, M for a minute, S for a second, and MS for a millisecond. The letters a, b, c, and d are used to define the numeric value for the time interval (as shown in the previous picture, representing a time value of 15 seconds). The text 'S5T#' represents the S5 Timer. For example, to write the time value of 5 minutes and 3 seconds, it would be written as 'S5T#5M_3S' at the TV point. Other PLCs may use different representations for time values with their respective timer symbols. The following timers are typically used with most PLCs:

  • On-Delay Timer 
  • Retentive On-Delay Timer
  • Off-Delay Timer
  • Pulse Timer
  • Extended Pulse Timer

 

On-Delay Timer (S_ODT)

An on-delay timer is used when an output needs to turn on after a certain delay. The timer is enabled by an input signal at the S point and runs for the specified time interval set at the TV input. As long as the signal at the S point is logic 1, the output at the Q point will become logic 1 after the timer elapses the time interval. If the signal at the S point changes from logic 1 to logic 0 while the timer is running, the timer immediately stops, and the output status at Q becomes logic 0. The timer can be reset at any time if the reset point R is triggered or its status becomes logic 1. In a ladder diagram, the on-delay timer is represented as 'S_ODT' (for Siemens PLCs only). The following picture shows an on-delay timer symbol and its timing diagram. 

  

The number 'T5' represents an on-delay timer (indicated by the S_ODT symbol). Input I2.3 is used as the set or enable signal for the timer, and it will start when the input signal I2.3 becomes logic 1. The time value for the timer is set to 15 seconds (written as S5T # 15S at the TV point), and it activates the PLC output Q1.5. Input signal I2.4 is used to reset the timer. With this circuit, the T5 timer will immediately activate when the signal is received at input I2.3, but the status of Q will only become high after 15 seconds. It will also activate the Q1.5 output coil of the PLC or set its status to logic 1. The Q1.5 coil output will remain active as long as input I2.3 is ON or logic 1. To deactivate the timer output, either input I2.3 needs to become logic 0, or the reset input I2.4 needs to be set to logic 1. This timer can be used for functions such as delayed motor starting or delayed activation of an actuator linked to output Q1.5.


Retentive On-Delay Timer (S_ODTS)

The working principle of a retentive on-delay timer is very similar to an on-delay timer, with the only difference being that it does not turn off if the enable input goes off or its status becomes logic 0. This means a retentive on-delay timer starts with a positive edge signal at the setpoint and remains on regardless of the input signal status. The timer enables the output Q after the specified time interval elapses, and it remains on until the reset signal is sent. In a ladder diagram, the retentive on-delay timer is represented as 'S_ODTS' (for Siemens PLCs only). The following picture shows a retentive on-delay timer symbol and its timing diagram.

 

The enable sequence for a retentive on-delay timer is similar to an on-delay timer. The timer output Q will activate only after 15 seconds when it receives the enable signal I2.3 (using the discussed timing value), and the enable signal can be triggered by a positive pulse. This means if the input status of I2.3 becomes logic 0 within 15 seconds (in this case), the timer will remain enabled and the output Q will activate, or its status will become logic 1 after the time elapses. The timer can be reset by triggering input I2.4, and as a result, the output will also deactivate or its status will become logic 0. In this case, the timer output Q is used to enable the coil Q1.5.

 

Off-Delay Timer (S_OFFDT)

With an off-delay timer, the output point Q always remains logic 1 as long as the input status at the set point (S) remains logic 1. The off-delay timer starts with a negative edge signal at the set point S and is reset when a logic 1 signal is received while the timer is running. When the negative edge signal is received at the S input, the timer disables its output, and the status of the Q point becomes logic 0 after the specified time interval. In a ladder diagram, the off-delay timer is represented as 'S_OFFDT' (for Siemens PLCs only). The following pictures show an off-delay timer symbol and its timing diagram.

According to the timing diagram, the output Q will become logic 1 immediately after receiving the enable signal at the input I2.3 and will become logic 0 after the time interval is completed (which is 15 seconds in this case). The timing diagram also shows that the timing value will not be considered as long as the input status at the set point is logic 1, and the timer will start immediately upon receiving the negative pulse at the set point input I2.3. The timer can be reset and the output status set to logic 0 by setting the input status of I2.4 to logic 1. The timer output Q is used to enable the coil Q1.5, which can be further utilized in various logical operations.

 

     

Pulse Timer (S_PULSE)

A pulse timer is used in ladder diagrams to generate an output of fixed duration. The timer output immediately turns ON when the enabling signal is present at the set point of the pulse timer and turns OFF after a specified time. In a ladder diagram, a pulse timer is represented by the symbol 'S_PULSE' (for Siemens PLCs). The following images depict a pulse timer symbol and its timing diagram. 

In this diagram, input I2.3 is used to enable the pulse timer at set point S. When the input status becomes logic 1, the output Q immediately turns ON, and it turns OFF after the specified time (in this case, 15 seconds), while the input status at set point S remains logic 1. When the input status of I2.3 becomes logic 0, the output Q also turns OFF or becomes logic 0. The output Q can be reset at any point by enabling the reset input I2.4. The timer output Q is connected to energize coil Q1.5, which can be used in various logical operations.

 

Extended Pulse Timer (S_PEXT)

The extended pulse timer has a functionality similar to the pulse timer, with the difference that if the timer's enabling signal goes OFF, the output will not immediately stop but will remain active for the specified time. In a ladder diagram, the extended pulse timer is represented by the symbol 'S_PEXT' (for Siemens PLCs). The following images show the symbol and timing diagram of an extended pulse timer.

In this diagram, input I2.3 is used to enable the extended pulse timer at set point S. When the input status becomes logic 1, the output Q immediately turns ON, and it turns OFF after the specified time (in this case, 15 seconds). When the input status of I2.3 becomes logic 0, the output Q will not turn OFF immediately and will remain logic 1 until the specified time interval is completed. The output Q can be reset at any point by enabling the reset input I2.4. The timer output Q is connected to energize coil Q1.5, which can be used in various logical operations.


Counter with Ladder Diagram 


A counter is used in a PLC system to count or calculate objects. It increments or decrements the count value based on the input signals received. When the input signals to the counter are logic 1, the count value is incremented or decremented as an integer value. An "Up counter" increases the count, while a "Down counter" decreases the count. For example, in a bottling plant, a sensor signals the bottles passing over a conveyor, and the PLC counter estimates the number of bottles by counting the signal frequency.

Counters are typically used in two ways. In the first case, when a specified number is reached, the output signal of the counter becomes logic 1. For instance, after counting every ten bottles in the previous example, an output signal can be generated to indicate the start of packaging. In the second case, the output signal remains ON until a certain number of signals are received by the counter. Once the specified number is reached, the counter turns its output signal OFF.

Counters, like timers, are built into PLC systems and used in programs as needed. In a ladder diagram, a counter is represented by a box with associated components and is addressed as C1, C5, C10, etc. Different PLC manufacturers may use different symbols to represent counters, but the basic working principle remains the same. In this document, the symbols used align with SIEMENS S7300 and S7400 PLCs. There are two common types of counters: "Count-Up" and "Count-Down" Sometimes, both types are integrated into a single assembly and function as a "Count Up/Down" Counter. The following three types of counters are discussed below. 

Count-Up Counter

In a ladder diagram, a count-up counter is represented as 'S_CU' (for Siemens PLCs). The following image shows the symbol and timing diagram of a count-up counter.

 

In this diagram, the count-up counter is labeled as C5. Input I2.3 is connected to the CU point, which counts the up-count signals. Each pulse (transition from logic 0 to logic 1) received at input I2.3 adds one to the count continuously (as shown in the timing diagram). Input I2.4 is connected to the set point S, which activates the counter and sets it with its preset value. In this example, the preset value is set to 6 (MW6). When the count reaches 6, the output Q becomes high (logic 1). The output of the counter is used to activate the PLC coil Q1.5. Input I2.5 is used for resetting the counter at any point. When the input status of I2.5 is logic 1, all the counting values are reset to zero, and the output of the counter turns OFF (logic 0). The counter symbol is accompanied by a count-up timing diagram with a preset value of six for better understanding.

 

Count-Down Counter

The operating principle of a count-down counter is similar to that of a count-up counter. In a ladder diagram, it is represented as 'S_CD' (for Siemens PLCs). The following image shows the symbol and timing diagram of a count-down counter.

 

In this diagram, input I2.3 is connected to the count-down (CD) point, and each pulse received at this point (logic 0 to logic 1 transition) is considered a count-down signal. Inputs I2.4 and I2.5 are used as set and reset inputs, respectively. The coil output Q1.5 is connected to the output signal of the counter, and the preset value (PV) is set to 6. Each incoming pulse at the input point I2.3 subtracts from the preset value until it reaches zero. When the preset value becomes zero, the counter output Q is activated, and the PLC output coil Q1.5 turns ON (logic 1). The reset input I2.5 can turn the output coil Q1.5 OFF (logic 0). The count-down timing diagram with a preset value of six represents the counter symbol.

  

Count Up / Down Counter

Sometimes, both count-up and count-down counters are integrated into a single assembly and function as an up/down counter. In a ladder diagram, it is represented as 'S_CUD' (for Siemens PLCs). The following image shows the symbol and timing diagram of an up/down counter. 

In this up/down counter, input I2.2 is connected as the count-up (CU) signal, and input I2.3 is considered the count-down (CD) signal. The pulses received at the CU point are added, while the pulses received at the CD point are subtracted. The output Q activates based on the preset value (four in this case) and accumulates the count-up and count-down signals. It also activates the output coil Q1.5 of the PLC. Inputs I2.4 and I2.5 are used for set and reset functions, respectively. The count up/down timing diagram with a preset value of four represents the counter symbol.

Monday, April 10, 2023

PLC Program

Introduction 

As discussed earlier, a control program is essential for the proper functioning of a PLC. Different logical instructions and sequential operations are written in a way that is meaningful for PLC operation. Typically, a PLC program is written by users on a separate computer and transferred to the PLC memory for storage. PLC programs can be composed in different languages, with the most common ones being Ladder Logic or LAD, Statement List or STL, and Function Block Diagram or FBD. Among these languages, Ladder Logic is the most popular and widely used by PLC manufacturers. The following example illustrates a simple electrical circuit and its equivalent PLC programming using LAD, FBD, and STL languages.


Ladder Logic or LAD

Ladder Logic or LAD is the most popular programming language used for programmable logic controllers (PLCs). The Ladder program is similar to an electrical wiring diagram. Here's a simple example of LAD diagram programming:

The left-side diagram presents an electric circuit with switches S1, S2, and S3, which are normally open (NO), and Lamp L1, which will glow when either both S1 and S2 switches are activated or only S3 is activated. If the corresponding circuit is written in Ladder logic, it would look like the picture shown on the right side. In this ladder logic diagram, I0.1, I0.2, and I0.3 are normally open (NO) inputs, and Q0.0 represents the output, which can light a lamp or run a motor. According to the program, if I0.1 and I0.2 are activated (Logic 1), Q0.0 will be activated (Logic 1), and the output status will also become Logic 1. Similarly, if only I0.3 is activated, the output status of Q0.0 will become Logic 1.

In a Ladder diagram, the left-side vertical line represents an energized conductor, the right-side vertical line represents an output element or return path, and the ladder logic diagram is always read from left to right and top to bottom. A complete Ladder diagram is divided into separate branches called Rungs or Networks, and each rung consists of several input instructions. Each rung has only one logical output, and multiple rungs make up a complete PLC program.

Statement list or STL

Statement List or STL is another popular PLC programming language. In an STL program, the operations to be performed are written on the left side, while the operands or items to be operated on are usually addressed on the right side. The following example illustrates the previous electrical circuit in Statement List or STL format: 

In this example, the operation 'A' represents the 'AND' logic, and 'O' represents the 'OR' logic operation. Inputs I0.2, I0.3, and output Q0.0 are considered operands. According to the program, if I0.1 and I0.2 are in the AND logic, the output status of Q0.0 will be Logic 1 when both inputs are activated. If only input I0.3 is activated (as it is in OR logic), the output status of Q0.0 will also become Logic 1. Various types of operations and operands are used in a PLC program written in Statement List or STL, and a series of instructions make up a complete PLC program. The use of STL language is limited to PLC programming.

Function Block Diagram or FBD

Some PLC programmers also use the Function Block Diagram or FBD programming language. In this language, operations or functions are represented inside separate blocks, while operands are written outside those blocks. Separate blocks are used for different operations. If the previous electrical circuit were expressed in a function block diagram, it would look like the following:

In this example, the inputs I0.1 and I0.2 are placed in the AND operation block. The result is then added to input I0.3 in an OR operation block, and the output Q0.0 represents the result of the OR operation. A complete PLC program is written using several such block functions. However, the application of Function Block Diagram or FBD programming with PLCs is also limited.

How PLC programmed

A "Communication Port" on the CPU allows it to connect with an external programming device. There are two types of programming devices: a small hand-held unit for direct programming of the PLC and an all-purpose computer with specific software for PLC programming. The programming software is specific to the PLC manufacturer. For example, SIEMENS uses software like STEP 7-Micro and WIN32, which can only be used for SIEMENS PLCs and not for other companies like MITSUBISHI, OMRON, or FANUC. Similarly, a hand-held programming unit designed for one brand of PLC cannot be used to program a PLC from another brand. Using a computer is generally more suitable for PLC programming compared to a hand-held unit.

The most commonly used languages for PLC programming are Ladder Diagram or LAD, Statement List or STL, and Function Block Diagram or FBD. Most companies primarily employ Ladder Diagram programming for PLC programming. A PLC program is composed in a step-by-step format with different blocks using PLC programming software. A lengthy PLC program consists of several blocks, making it easier to understand and identify errors. After composing the program, it is checked for errors within the software. Once the entire program is reviewed and confirmed to be correct, it is transferred to the PLC unit from the computer using a cable. The PLC is then set to RUN mode, and its functions are tested accordingly. If there are any errors in the program while running the PLC, they can be rectified using an external computer. The corrected program is then transferred to the PLC, overwriting the old program. Small or medium-sized PLCs usually do not have a display unit, so the program cannot be observed directly. However, with advanced PLCs embedded with a CNC controller or connected to a robotic arm where a separate display unit is available, the PLC program can be edited through a keyboard.

 

Functions of Sensors and Actuators with the PLC program

Input elements or sensors are connected to the PLC through an Input Module, while output devices or actuators are interfaced through an output module. The output status changes based on the statements of the PLC program and logical instructions in the CPU, as well as the status of the sensors or input devices. This means that input and output devices have a close and intimate relationship with the PLC program. The following two diagrams illustrate the connection of sensors and actuators with a PLC, along with a simple PLC program to light a lamp. 

Different elements and their symbols used in PLC programming

PLC manufacturing companies use various components (internal and external) and a variety of symbols for writing PLC programs. Although the functional elements remain the same in almost all types of PLCs, the symbols used may vary. Here are some common and widely used elements and symbols in PLC programming:

Input - Status information (Active / Deactive) collected from sensors connected to the PLC is referred to as an input. It is commonly used in logical circuits to activate multiple outputs, timers, and counters. In a PLC program, inputs are represented or addressed as 'I' or 'X' followed by a numerical number (e.g., I1.5 or X4.7). SIEMENS PLCs use the letter 'I' to address inputs, while FANUC PLCs use 'X'.


Output - The signal information conveyed from the PLC to activate different actuators or output elements is referred to as an output. Sometimes, timers or counters are controlled using logical operations to trigger these outputs. Outputs in a PLC program are addressed with 'Q' or 'Y' followed by a numerical number (e.g., Q1.5 or Y4.7). SIEMENS PLCs use the letter 'Q' and 'Y' to address outputs, while FANUC PLCs use similar symbols.

Internal Relay - An internal relay in a PLC acts as a virtual relay and behaves like a physical relay in conjunction with other PLC elements such as inputs and outputs. There can be different types of internal relays present in a PLC, but they have no physical existence. Internal relays are stored in the memory of the PLC and are given different names by manufacturers. SIEMENS refers to them as "Flag," MITSUBISHI calls them "Auxiliary Relay," and TOSHIBA identifies them as "Internal Relay." Internal relays can be either retentive or non-retentive. Retentive internal relays retain their last logical state (Logic 0 or Logic 1) even after the PLC is powered off. Non-retentive internal relays switch to a Logic 0 state immediately after the power supply is turned off. In SIEMENS PLCs, these internal relays are identified by the letter 'M'.

 

Set Coil - Similar to an output, a set coil is triggered by different logical operations involving inputs, outputs, and internal relays. When the logical functions collectively result in a Logic 1 status, the set coil remains in a Logic 1 state regardless of the state of other logical operations. The status of the set coil stays at Logic 1 even if the collective nature of the logical functions becomes Logic 0. Typically, an output, timer, counter, or internal relay is turned on using a set coil, and it is denoted by the symbol 'S'.


Reset Coil - Similar to the set coil, a reset coil is triggered by logical operations involving inputs or internal relays. When the logical functions collectively result in a Logic 1 status, the reset coil changes its state from Logic 1 to Logic 0. If the reset coil already has a Logic 0 state, it does not change its state even if the result of the logical function becomes Logic 1. In a PLC, the reset coil is used to reset timers, counters, or any internal relays, and it is denoted by the symbol 'R'.

 

Timer - Timers are used in PLCs to measure time intervals. They have no physical presence but can be easily implemented through programming. Different types of timers are employed in PLCs, such as ON Delay and OFF Delay timers. They are represented by the letter 'T' followed by a numeric number (e.g., T5 or T10). Typically, an input, internal relay, or output is used to activate or deactivate a timer. Various timers and their operations are explained in detail in a separate chapter.

Counter - Counters are used in PLCs to count objects and usually have two signals: a UP count and a DOWN count. Similar to timers, counters do not physically exist inside the PLC but are built-in and operated based on the program requirements. Different PLC manufacturers represent counters differently in their programming styles, even within the same company's different models of PLCs. Counters are represented by the letter 'C' followed by a numeric number (e.g., C5 or C10) within a PLC program. Various types of counters and their operations are covered in a separate chapter.

Fundamentals of Ladder Programming

The ladder diagram is a graphical programming language that uses symbolic notation to express logical operations with Programmable Logic Controllers (PLCs). It consists of a series of rails and rungs containing different logic symbols that form decision-making expressions. There may be slight differences in the representation and usage of logical symbols among different PLC manufacturers. In this document, all symbols and logical expressions are aligned with SIEMENS PLCs, although other companies such as ALLEN-BRADLEY, MITSUBISHI, FANUC, DELTA, and OMRON have similar ladder programming methods. A ladder diagram comprises seven elemental components: Rail, Rung, Input, Output, Logic Expressions, Address notation, and Comments. The following picture illustrates the different elements in a ladder diagram.

 

How to write a Ladder logic program


To write a ladder logic program, you need to understand various symbols and logical expressions such as AND, OR, and NOT, which are used in the ladder diagram. The logical representation in ladder diagrams often involves Normally Open (NO) or Normally Closed (NC) contacts and series/parallel connections. In a ladder diagram, there are two rails, one on the left-hand side and another on the right-hand side, with Contacts, Coils, Logic expressions, and Internal variables arranged in between. Each component is interconnected and linked to the rails, forming a line of code called a Rung. Multiple rungs are written one after another, creating a ladder diagram. The following picture shows the standard format of a ladder diagram. 


The two principal symbol functions in ladder programming are Contacts and Coils. Contacts represent input elements, while Coils represent outputs in a ladder diagram. Additionally, there are "Box symbols" used in ladder programming to define different types of timers and counters.

 

Elements of a LADDER Programming

The following picture represents the individual elements used in PLC ladder diagrams. 

 

Contact - A contact serves as an input, similar to a switch, that can be activated or deactivated externally. Other elements such as outputs and internal relays can also function as contacts with logical operations. Contacts are typically categorized as Normally Open (NO) or Normally Closed (NC). The image below depicts both types of contacts.


With a Normally Open (NO) contact, the status is Logic 0 when there is no power flow through it. Upon activation, the status changes to Logic 1, allowing power to flow. Conversely, with a Normally Closed (NC) contact, the status is Logic 1 when there is power flow through it. When deactivated, the status changes to Logic 0, interrupting the power flow. Each contact is identified by an address written above the symbol. 

Coil - A Coil can be considered as a virtual relay that activates based on logical operations involving Contacts. Coils are commonly used to energize solenoids, relays, and motor starters. When the output status of a Coil is Logic 1, it triggers or energizes the connected element. Conversely, when the Coil status is Logic 0, it deactivates the element. Output Coils can also function as Normally Open (NO) or Normally Closed (NC) Contacts in a PLC program. A Coil is identified by an address written above its symbol. The following picture shows the symbol of a Coil used in a ladder diagram. 

Box - In ladder programming, a box symbol represents various logical functions such as timers, counters, comparisons, and math instructions. Several box symbols are used to represent complex functions, including timers and counters. The following diagrams show the symbolic representation of a typical Timer and Counter, which are frequently used in PLC ladder diagrams. 

Advanced PLC instructions are sometimes used to handle Non-Boolean integers and perform control functions. Data comparison instructions are employed to compare two Non-Boolean values and generate a Boolean output. Mathematical instructions and logical symbols are also used in advanced ladder logic programs to perform various mathematical operations. The following pictures illustrate some common comparisons and mathematical instructions used in advanced PLC programming. 


Using Contact and Coil with Ladder programming 

In the electrical circuit shown below, a push switch is connected to turn on the light. In the equivalent ladder diagram, the switch is replaced by a contact, and the coil represents the light. Since the switch is currently in the deactivated state, the light is not glowing.

 


When the push switch is pressed, the light will turn on. Similarly, when the contact is activated and its status becomes Logic 1, the coil will energize, resulting in the output status also becoming Logic 1. The following picture illustrates the activated state of the switch. In the respective ladder diagram, the contact and coil symbols will be enclosed in a grey box, indicating Logic 1 status for both the contact and coil.



Application of different symbols


Ladder programming logically applies multiple Inputs or Contacts to trigger an output Coil. In the example shown below, Contact-1 and Contact-2 are used to activate Coil-1. The status of Coil-1 depends on the states of Contact-1 and Contact-2. When Contact-1 and Contact-2 are both Logic 1, Coil-1 will also have a Logic 1 status.

 

A complete ladder diagram can have multiple Coils or outputs and various sets of instructions to activate them, along with several logical functions for different Coils. Each Coil and its associated logical functions form a ladder rung, and a complete ladder diagram consists of multiple ladder rungs. The following picture shows a ladder rung and different symbols used with their addresses. 


Addressing of symbols 


Addresses are usually indicated on top of symbols to identify them in a ladder diagram. For example, if the address written on a symbol is I1.2, it means that the following Contact is assigned the input address I1.2. Supplying a 24V DC voltage to input module terminal I1.2 will make the Contact status Logic 1, and the Contact symbol will appear in a light-colored box. Similarly, if the Coil symbol is addressed as Q2.3 and its status is Logic 1, a 24V DC voltage will be available at the output module terminal, and the Coil symbol will also appear in a light-colored box. The following picture illustrates the representation of different address symbols in a ladder diagram. 


Latching circuit with Ladder diagram

In some cases, it is necessary to maintain output in a PLC program based on certain circuit conditions. This can be achieved using a Latching circuit in a ladder diagram. For example, when a push switch serves as the starter for an electric motor, the output should remain on to keep the motor running until another push switch is used to stop it. A Latching circuit is employed in the PLC to fulfill this requirement. It requires at least one input to set the latch and another input to reset it, while the output holds the latched state. The following diagram illustrates a Latching circuit in a ladder diagram.

 

 

In addition to the previous ladder diagram, let's assume that the Coil output is used to energize a motor, and "Input-A" and "Input-B" are two push-button switches used to start and stop the motor, respectively. Input-A is a Normally Open (NO) switch, and Input-B is a Normally Closed (NC) switch. When Input-A is activated or its status becomes Logic 1, the output status of the Coil will also become Logic 1. As Input-B is an NC contact, its state will remain Logic 1 as long as it is deactivated. By applying an AND logic operation to Input-A and Input-B, the output status becomes Logic 1 when Input-A is pressed. The Output is also used as a Contact signal, in parallel with Input-A and applying an OR logic operation. When Input-A is released or turned off, its status becomes Logic 0, but the output status of the Coil remains Logic 1. The Output, combined with Input-A using OR logic, acts as a replacement for Input-A. As a result, the output status remains Logic 1, keeping the motor continuously running. This circuit is known as a Latching Circuit in a ladder diagram. 

To unlatch or deactivate the output status of the Coil, pressing Input-B will make the output status of the Coil Logic 0. When Input-B is activated and its status becomes Logic 0 (since it is an NC contact), the Output Coil status will also become Logic 0, resulting in the unlatching or deactivation of the output. The motor connected to the output or Coil will stop immediately. To reactivate the output, Input-A can be pressed again. The following diagrams illustrate three different situations of a Latching circuit.

 

In some cases, SET and RESET instructions can be used to latch and unlatch a Coil output. However, the SET and RESET functions cannot be written in the same rung of a ladder diagram. In the example shown below, if Input I1.2 momentarily becomes Logic 1, the SET symbol changes its state to Logic 1 and remains that way even if the status of Input becomes Logic 0. The Output address Q2.3, defined with the Set symbol, also changes its state accordingly. When Input I1.3 changes its state to Logic 1, it acts as a reset and changes the status of the Output address Q2.3 to Logic 0. The following picture illustrates the Set and Reset functions in a ladder diagram. 


Multiple outputs with Ladder diagram

Usually, a single Coil is used as an output in each rung of a ladder diagram. However, there are cases where multiple Coils or multiple outputs can appear in a single rung. The following ladder diagram represents two Coils (Output A and Output B) being energized by a single Contact (Input A). 

Ladder logic programming rules

There are some fundamental rules that apply to ladder logic programming across different PLC manufacturers, regardless of the specific programming commands they use: 

1. The signal flow in a ladder diagram always proceeds from the left rail to the right rail.


2. More than one output or Coil can be programmed in a single rung.


3. The output Coil can be used as an input signal or a Contact.


  

4. The output Coil, Timer, or Counter should not be directly connected to the left rail. There must be at least one input Contact at the start of each rung.


5. No Contact can be used on the line after the output Coil is turned on.

 

 


6. The input Contact cannot be taken as an output.




Wednesday, April 5, 2023

G Code & M Code

What is a G code

G codes are a programming language used in CNC machining to define various movements of axes and other functions of a CNC machine. They play a crucial role in understanding coordinate systems and measurements. G codes are categorized into different groups, allowing only one G code from a specific group to be used in a single program line. However, multiple G codes from different groups can be used in a single program block. Some G codes are defined as modal codes, meaning they remain active in a program until another G code within the same group is activated. While most G codes are common across all CNC controllers, there may be slight variations between milling and turning operations. The following are common and essential G codes frequently used with CNC milling and turning machines:

 

G00 – Rapid move - This code is used for rapid movement of the axes to move from one point to another in free space without cutting the material. It does not require a feed rate and usually occurs at the maximum velocity of the machine axes.

G01 – Linear feed movement - This code directs the axes to move in a straight path or linear interpolation with a programmed feed. It involves interpolating all the axes in motion to reach the endpoint at the same time.

G02 - Circular movement Clockwise - This code instructs the axes to move in a circular path or circular interpolation in a clockwise direction with a programmed feed. The circular path is defined by its start and endpoints, radius, or center point.

G03 – Circular movement Anti-clockwise - This code moves the axes in a circular path or circular interpolation in an anti-clockwise direction with a programmed feed. The circular path is defined by its start and endpoints, radius, or center point.

G04 – Dwell time - A dwell time is a pause in program execution. The duration of the dwell is specified by the F value in seconds. No axis movements occur during the dwell time, and other functions such as spindles and coolant remain on.

G17 / G18 / G19 - Plane Selection – These codes are used for selecting a plane during circular interpolation of axis movements. G17 is used for the XY plane, G18 for the ZX plane, and G19 for the YZ plane. The default plane selection for milling operations is G17.

G20 / G21 - Unit Selection – These codes define the measurement unit for axis movements during programming. G20 is used for inch units, and G21 is used for the metric system.

G28 - Zero Return - This function brings one or more axes back to the home position from the last cutting position, usually through an intermediate point.

G40 – Cutter compensation cancel – This code cancels the cutter compensation mode.

G41 / G42 - Cutter composition left / right – These codes enable cutter compensation mode. G41 allows compensation in the right direction, while G42 allows compensation in the left direction.

G43 / G44 - Tool length offset - During axis positioning, the actual movement of the axis is adjusted based on the tool length offset value. G43 is used for a positive tool offset, and G44 is used for a negative tool offset.

G49 - Tool length offset cancel – This code cancels the tool length offset value.

G53 - Machine Coordinate system – While machine positioning is typically done using a user-created coordinate system, it can sometimes be beneficial to program with the machine coordinate system. The G53 code in a program defines the machine coordinate system.

G54 to G59 – Fixture offset – These codes are used to select different fixture offsets in a program. It is possible to use multiple fixture offsets within the same program.

G73 to G89 – Canned cycles – These codes are reserved for canned cycles, which streamline program writing. For example, G81 is used for drilling and G84 is used for tapping.

G90 - Absolute position mode – With absolute position mode, the machine moves to the commanded position based on the user coordinate system.

G91 - Incremental position mode – With incremental position mode, the commanded position and movement are based on the distance and direction from the current location.

G94 - Feed per minute - This code specifies the feed rate of an axis movement in a program in units such as mm/min.

G95 - Feed per revolution - This code is used in tapping cycles in milling operations. The axis feed rate is programmed based on the spindle RPM.

G96 - Constant surface speed ​​– During turning operations, the machining or surface area of the workpiece continually changes. By applying this code, the machine maintains a constant surface speed based on the cutting diameter and specified surface speed in surface units per minute.


G97 - Constant RPM - The G97 code is used for spindle rotation at a constant RPM in milling operations. It cancels the constant surface speed in turning processes.

G98 - Return to an initial point – With drilling and boring canned cycle operations, it is necessary to return the cutting tool to the initial point before a rapid move to the next position. The G98 code is used for this operation.

G99 - Return to R point – With canned cycle operations, the G99 code is used to bring the cutting tool back to the R point, eliminating extra axis movement and reducing cycle time.

 


What is M code 

M codes are used in CNC (Computer Numerical Control) programming to activate or deactivate various miscellaneous functions of a CNC machine. They are used to perform different actions such as tool changes, coolant activation, and palette changes. The specific M code functions can vary depending on the type of machine and CNC controller being used. Here is a list of some commonly used M codes that are typically standard for all types of CNC machines.

M00 - Mandatory program stop - The M00 code is used to pause a running program at any point. When this code is encountered, all axes and spindle motion will come to a stop. To resume the operation, the cycle start button needs to be pressed again.

M01 - Optional program stop - The M01 code is similar to M00. It allows for an optional stop in the program. To enable this feature, the optional stop switch on the operator panel should be kept in the ON condition.

M02 - End of program – The M02 command marks the end of program execution. After encountering this code, all programming operations will cease, and the program will not rewind. If the cycle start button is pressed again, the programming operations will continue with the following program block.

M03 – Spindle forward or clockwise – The M03 code activates the spindle rotation in a clockwise or forward direction at a specified speed. The M03 command is typically accompanied by the 'S' word, which specifies the desired speed. For example, M03 S100 signifies the rotation of the spindle in a clockwise direction at 100 RPM.

M04 - Spindle reverse or counter-clockwise – The M04 code activates the spindle rotation in a counter-clockwise or reverse direction at a specified speed. Similar to M03, the M04 command is also accompanied by the 'S' word to represent the desired speed. For example, M04 S100 denotes the rotation of the spindle in a counter-clockwise direction at 100 RPM.

M05 - Spindle stop –  The M05 code stops the rotation of the spindle, whether it is rotating clockwise or counterclockwise.

M06 - Tool Change – The M06 code indicates a tool change on the machine, with the tool number specified by T. For instance, M06 T05 represents the change to tool number five.

M07 – Through coolant on – The M07 code activates the through-tool coolant output, allowing coolant to flow through the tool during machining.

M08 – Flood coolant on – The M08 code activates the flood coolant output, which provides a continuous flow of coolant during machining.

M09 – Coolant off – The M09 code deactivates all coolant outputs, stopping the flow of coolant.

M10 – Spindle chuck clamp – Spindle Chuck Clamp: The M10 command is used to clamp the spindle chuck on turning machines.

M11 - Spindle chuck Un-clamp – The M11 command unclamps the spindle chuck on turning machines.

M19 - Spindle orientation - This code enables the spindle to stop or orientate itself at a specific position. Spindle orientation is essential for tool-changing operations and can also be used in specific machining cycles such as G76 and G87.

M30 – Program end and rewind – The M30 command marks the end of the current program execution and rewinds it to the beginning. By pressing the cycle start button again, the program will restart from the first program block.

M60 - Palette Change – In some CNC machining centers, the M60 code is used to change the pallet and the workpieces attached to it.

M98 - Subprogram call - The M98 command is used to call separate subprograms, which helps reduce program length and complexity. The subprogram number is specified along with the M98 command.

M99 – Return to the main program – The M99 code is used to return to the main program from a subprogram.

Note: The above list provides a general overview of some commonly used M codes, but it's important to consult the specific machine documentation and CNC controller manual for accurate and detailed information about M codes supported by your machine.

Popular Posts