PLC
A PLC is a small, rugged computer that controls nearly all automated machines: it reads inputs from buttons and sensors, makes decisions based on a program, and activates motors, valves, and indicator lights. In this chapter, we’ll explore its design, how it works, how it’s connected, and how to program it using ladder logic—from the simplest contacts all the way to sequences using Grafcet—with plenty of diagrams and worked examples. Finally, we’ll take a look at the PLC inside every CNC machine.
49.1 From Relay Logic to PLC
An automatic control system, whether small or large, always does the same thing: it monitors input signals (buttons, limit switches, sensors), combines them according to a certain logic, and controls outputs (motor contactors, solenoid valves, indicator lights). Until the late 1960s, this logic was implemented solely using relays, contactors, and electromechanical timers connected to each other by wires: this is known as hardwired logic. The start-stop circuit with self-holding that you saw in Chapter 44 is an example of hardwired logic.
Hardwired logic works well and is still used for small circuits and safety circuits, but it has one major drawback: the logic is embedded in the wires. To change the machine’s cycle, you have to shut it down, disconnect it, and reconnect the wires; a control panel with a hundred relays takes up an entire cabinet, it wears out (the contacts wear down), and finding a fault means tracing the wires one by one with a tester.
In 1968, the American automotive industry—which was changing its systems with every new model—asked manufacturers for an electronic device that would replace relay cabinets, could be reprogrammed quickly, and that electricians could understand without having to become computer scientists. This led to the creation of the PLC, or Programmable Logic Controller. With a PLC, wires run only from the buttons and sensors to the inputs and from the outputs to the actuators; all the logic is contained in a program stored in memory. This is programmable logic.
To make it easy for electricians to understand, the first PLC language was drawn just like relay drawings: two vertical power supply lines and, in between, horizontal rows with contacts and coils. This is the ladder diagram, which we’ll cover starting in section 49.6.
| Appearance | Hardwired Logic (Relays) | Programmable Logic Controller (PLC) |
|---|---|---|
| Where's the logic? | in the files and relays | in the program stored in memory |
| Change the cycle | rewire the control panel | change the program, even in just a few minutes |
| Timers and Counters | one device for everyone | hundreds, software only |
| Dimensions | grows with complexity | almost fixed; only the input and output modules increase |
| Usury | contacts that wear out with every maneuver | electronic logic with no moving parts |
| Diagnosis | using the tester, strand by strand | LEDs on the modules and the program's online status |
| Suitable for | Small circuits and safety features | everything else |
49.2 How a PLC Works
A PLC is a computer designed for the shop floor: it withstands vibrations, dust, electrical interference, and the temperatures found in an electrical panel; it mounts on a DIN rail and operates for years without being shut down. Its components are always the same, whether it’s a compact PLC (all in one enclosure) or a modular PLC (side-by-side modules that are selected one by one).
- Power supply: It converts 230 V AC from the mains to 24 V DC for the CPU and modules. It is often a separate unit that also powers sensors and actuators.
- CPU (central processing unit): the processor that executes the program, along with its operating system, clock, and error handling. It has a selector or a RUN/STOP command: in RUN mode, it executes the program; in STOP mode, it halts all outputs.
- Memory: The program memory is not erased when the PLC is turned off; the data memory contains variables, timers, and counters. Some data can be declared as retained: it is preserved even when the PLC is turned off (for example, the daily piece counter).
- Digital Input Modules (DI): These channels receive signals that can be either present or absent—usually 0 or 24 V DC—from buttons, limit switches, proximity sensors, and thermal relay contacts. Each channel has an LED that lights up when a signal is present.
- Digital Output Modules (DO): They turn loads on or off, such as indicator lights, contactor coils, and solenoid valves. The outputs are relay-based o transistor-based (paragraph 49.4).
- Analog Modules: Analog inputs (AI) measure continuously varying quantities, such as pressure or temperature, which are converted by the transducer into 0–10 V or 4–20 mA (Chapter 47); analog outputs (AO) provide a variable voltage or current, such as the speed reference for an inverter (Chapter 45).
- Communication: the interface for the programming PC, industrial Ethernet networks, and fieldbuses to communicate with operator panels, inverters, remote modules, and other PLCs.
The inputs are almost always optically isolated: inside the module, the signal turns on a small LED that illuminates a phototransistor. Only light passes between the field and the CPU’s electronics, so any noise or voltage spike on the machine’s wires does not reach the processor.
A machine has 22 input signals and 13 digital outputs. You want to leave a 20% margin for future modifications. The available modules have 16 channels. How many input and output modules are needed?
49.3 The Scan Cycle
The PLC does not run the program “just once”: it repeats it in a continuous loop—called a scan cycle—hundreds of times per second. Each loop always consists of the same phases.
- Reading the inputs: The CPU reads the status of all inputs and copies it into a memory area called the input process image. From this point on, throughout the entire cycle, the program uses this snapshot, not the actual terminals.
- Program Execution: Instructions are executed in order, network by network, from top to bottom, and within each network, from left to right. The results are written to the output image.
- Writing to the outputs: At the end of the cycle, the output image is copied to the modules, and only then do the relays and transistors change state.
- Diagnostics and Communication: The CPU performs a self-check, responds to the operator panel and the PC, and then restarts.
The time for one complete cycle is the cycle time tc. It depends on the CPU speed and the length of the program: for a typical machine, it ranges from 1 to a few tens of milliseconds. The CPU monitors this using a watchdog: if a cycle lasts longer than a set limit—for example, because the program has entered an infinite loop—the CPU goes into STOP mode and turns off the outputs.
The Practical Consequences of the Cycle
- A pulse that is too short may be missed: if an input turns on and off between two readings, the program will never detect it.
- The PLC responds slowly: a change that occurs immediately after a read operation takes almost one cycle to be read and another cycle to reach the output.
- The order of the rounds matters: a round uses the bits that have already been updated by previous rounds in the same cycle, while the bits from subsequent rounds are still the same as in the previous cycle.
- Never use the same spool in two nets: The output takes the value of thelast the network that writes it, and the first one becomes useless. It's one of the most common errors made by beginners.
A sensor counts 4-mm-long tabs as they pass over a conveyor belt at 0.8 m/s. The PLC has a cycle time of 10 ms. Is the count reliable? What length would be required to ensure a pulse lasting at least two cycles?
An input has a 3-ms filter; the cycle time is 12 ms. What is the maximum response time with a relay output (10 ms) and with a transistor output (0.5 ms)?
49.4 Connecting Inputs and Outputs
The 24-volt inputs
The most common input module in Europe operates at 24 V DC and has its common terminal connected to 0 V (terminal M): an input is set to 1 when +24 V is applied to its terminal. A button is therefore connected between +24 V and the input terminal.
- A NO (normally open) button outputs 0 when at rest and 1 when pressed: this is the case with run buttons.
- An NC (normally closed) button outputs a 1 when at rest and a 0 when pressed: it is used for stopping, as we will see in section 49.7. The NC contact of the thermal relay is connected in the same way (terminals 95–96).
- A three-wire PNP sensor has the brown (BN) wire connected to +24 V, the blue (BU) wire to 0 V, and the black (BK) wire to the input: when it detects an object, it connects the black wire to +24 V. For inputs with the common terminal at 0 V, PNP sensors are required; NPN sensors close toward 0 V and require modules with the common terminal at +24 V (Chapter 47).
Outputs: Relays and Transistors
The relay outputs contain a small relay for each channel: the contact is voltage-free, so it can control both AC (230 V AC) and DC loads up to about 2 A. However, they are slow (about 10 ms) and wear out: a contact lasts for a few hundred thousand operations at full load. The transistor outputs operate only on 24 V DC, typically up to 0.5 A per channel, but they are extremely fast and do not wear out. For AC loads, triac outputs are also available.
Typical loads include indicator lights, contactor coils, and solenoid valves. The PLC never directly controls a motor: it controls the contactor coil, and it is the contactor’s power contacts that supply current to the motor (Chapter 45). For a load that requires more current than the output can provide, an interface relay is inserted.
Contactor coils, relays, and solenoid valves are inductive loads: when the output opens, the coil tries to maintain the current and produces a surge of hundreds of volts that can damage the transistor and disrupt the entire control panel. In direct current circuits, a recirculation diode is connected in parallel with the coil, mounted “backward” (cathode toward the + terminal): during operation, it does not conduct; when the circuit is turned off, it provides a closed path for the current in the coil, allowing the energy to discharge without spikes. In AC systems, however, an RC circuit or a varistor is used instead. The diode slows the coil’s discharge slightly, usually without any adverse effects.
A group of 8 transistor outputs has a limit of 0.5 A per channel and 2 A in total. Four 4.8-W solenoid valves and two 1.2-W indicator lights, all rated at 24 V DC, are connected and can be turned on simultaneously.
A solenoid valve activates 6 times per minute, 16 hours a day. The output relay is rated for 300,000 operations with that load (approximate value). How long will it last?
49.5 Addresses, Variables, and Languages
The Addresses
Every input, output, or internal bit has a address. In the notation most commonly used in Europe, the letter indicates what it is, and the numbers indicate where it is located: I0.3 is the entrance (Input) of byte 0, bit 3; Q1.0 This is the output of byte 1, bit 0. A byte has 8 bits, numbered from 0 to 7 (Chapter 46): after I0.7 comes I1.0.
- I: digital inputs, in bits (
I0.0), bytes (IB0), or 16-bit words (IW0); analog inputs are read as words (IW64). - Q: Digital and analog outputs follow the same rules.
- M: Internal memory bits (
M0.0), which are not associated with any terminals and are used to store intermediate results, such as the auxiliary relays in a control panel. - T and C: timers and counters (
T1,C1), with their outputsT1.QandC1.Q.
Not all manufacturers write addresses this way. The IEC 61131-3 standard uses the percent sign: %IX0.0 for an input, %QX0.0 for an output, and %MX0.0 for a flag. Others number the inputs X0, X1… and the outputs Y0, Y1…, sometimes in base 8; still others use the module’s slot and channel. In the PMC of CNC machines (section 49.14), you’ll find the letters X (inputs from the machine), Y (outputs to the machine), F (signals from the CNC to the PMC), G (signals from the PMC to the CNC), and R (internal relays). The concept is always the same; only the notation changes.
The Table of Variables
No one remembers off the top of their head that I0.2 is the pump motor's thermal relay. That's why every program has a table of symbols (or variables): for each address, there is a symbolic name, the data type, and a comment. In the program, you write the name, and the software displays the address next to it.
| Type | What's Inside | Values |
|---|---|---|
| BOOL | a bit: input, output, flag | 0 or 1 (FALSE or TRUE) |
| BYTE, WORD | groups of 8 or 16 bits | 0 to 255, 0 to 65,535 |
| INT | 16-bit signed integer (raw analog values, counts) | from −32,768 to 32,767 |
| DINT | 32-bit signed integer | approximately ± 2.1 billion |
| REAL | numbers with a decimal point (pressures, temperatures) | about 7 significant digits |
| TIME | duration, for timers | for example, T#5s, T#500ms |
We need to control a pump motor using a start button, a NC stop button, a thermal relay, a run indicator light, and a thermal trip indicator light. Here is the table of variables that we will also use in Example 49.15.
| Symbol | Address | Type | Comment |
|---|---|---|---|
| MARCIA | I0.0 | BOOL | S1, NO run button |
| STOP | I0.1 | BOOL | S2, NC stop button (1 = not pressed) |
| THERMAL | I0.2 | BOOL | F1, thermal relay contacts 95-96 (1 = everything is OK) |
| KM1 | Q0.0 | BOOL | motor contactor coil |
| H_MARCIA | Q0.1 | BOOL | Green light, engine running |
| H_THERMAL | Q0.2 | BOOL | Yellow warning light, thermal protection activated |
The languages of the IEC 61131-3 standard
The international standard IEC 61131-3 defines PLC programming languages so that anyone who has learned one brand can easily adapt to others.
| Language | What's it like? | When to use |
|---|---|---|
| LD · ladder | diagram, contacts, and coils, like a relay diagram | bit logic, machines, maintenance: it's the most common in the workshop |
| FBD · Functional Blocks | diagram, logic gates, and blocks connected by lines | those with a background in electronics, control systems, and process systems |
| ST · Structured Text | text-based, similar to Pascal, with IF, CASE, and FOR | calculations, formulas, data management, algorithms |
| IL · List of Instructions | text-based, similar to assembler | old programs; the current standard considers them obsolete |
| SFC · Sequential Function Chart | graph, phases, and transitions (derived from Grafcet) | Step cycles: sequences of cylinders, automatic machines |
49.6 The Ladder Circuit: Contacts, Coils, Series and Parallel Connections
A ladder diagram has two vertical lines: the left one acts as the positive terminal, and the right one as the return. Between the two are the networks (in English rung, “rungs”): each one is a row with contacts on the left and one or more coils on the right. Imagine that a “logic current” flows from left to right: if it finds a closed path, it reaches the coil and activates it.
- NA Contact
--| |--: lets the signal pass when the bit above is set to 1. - NC Contact
--|/|--: let it pass when the bit is set 0. It is negation, the "NOT" of logic (Chapter 46). - Coil
--( )--: sets its bit to 1 if power is supplied to the network, and to 0 if it is not. The coil resets its bit every scan cycle. - Contacts in series they do theAND: It only works if they're all closed. Contacts in parallel they do theOR: It's enough if just one is closed.
| A (I0.1) | B (I0.2) | Series: A AND B | Parallel: A OR B |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 |
Some rules apply to almost all software: coils are always on the right, no contacts are placed after a coil, and each bit is written with a single coil in a single network. A contact, on the other hand, can be used as many times as you like: it’s not a real contact that wears out; it’s just the reading of a bit.
A machine's coolant pump must run when the selector switch is set to AUTO and (the spindle speed is high or the operator presses the MANUAL button), provided that the level in the tank is not too low. The low-level sensor outputs a 1 when the fluid is low.
49.7 The self-holding function and the NC stop button
The run button is pressed and released; however, the motor must remain on. As in the wired logic, a contact from the motor itself—the NO contact of KM1—is connected in parallel with the run button. As soon as KM1 turns on, this contact closes and “holds” the power supply even when the button is released: this is the self-holding mechanism. To turn it off, the power supply must be interrupted with a contact in series: the stop contact.
Here’s the point that confuses almost everyone. The stop button on the machine is always an NC button: in the rest position, it is closed and sends 24 V to the input, so in the rest position I0.1 = 1; when pressed, the circuit opens and I0.1 goes to 0. In the program, we want the power to be supplied when the stop button is not pressed—that is, when I0.1 = 1—which is why we use the NO contact shown in the diagram. The same applies to contacts 95-96 of the thermal relay.
| Situation | S2 Button | Bit I0.1 | NA Contact for I0.1 | Engine |
|---|---|---|---|---|
| Nobody touches anything | closed | 1 | closed: traffic can pass through | stay as it was |
| Press the stop button | open | 0 | open: the network goes down | stops |
| The S2 wire breaks | open circuit | 0 | open | It stops: definite malfunction |
| Press the start and stop buttons at the same time | open | 0 | open | suspended: the stop takes precedence |
Why does the stop button have to be NC? Because a broken wire, a corroded terminal, or a dirty contact will open the circuit—just as if someone had pressed the stop button: the machine stops, and the fault is detected immediately. If the stop button were NO, a broken wire would show no signs of a problem, and on the day you actually need to stop the machine, the button wouldn’t work. This is the principle of positive safety: the most likely failure must bring the machine to the safest state. Also note that the stop button is wired in series after the start button: if the start and stop buttons are pressed simultaneously, the stop button takes precedence (stop priority), which is always the right choice for a motor.
A beginner might think: “The stop button is NC, so I’ll put an NC contact in the program.” What happens with the circuit shown in Figure 49.12?
49.8 Set, Reset, and Edges
Set and Reset Coils
In addition to the normal coil, there are two coils that store data. The set coil --(S)-- sets the bit to 1 when the circuit is powered on and then keeps it at 1 even when the circuit is powered off. The reset coil --(R)-- resets it to 0. It is an SR flip-flop (Chapter 46) implemented with two coils. The same bit can be set and reset in different circuits; if both the set and reset signals arrive in the same cycle, the circuit written lower down takes precedence, because it is the last to write the bit. To avoid dependence on order, the standard also specifies SR (set wins) and RS (reset wins) blocks.
Rewrite the motor control using the set and reset coils, with the same inputs as shown in Figure 49.11. Which contacts are used for the stop and thermal protection?
Ascent face and descent face
Sometimes it’s not the fact that a signal is at 1 that matters, but the moment it becomes 1. The rising edge (P contact, R_TRIG block in the standard) outputs 1 for a single scan cycle when the signal changes from 0 to 1; the falling edge (contact N, F_TRIG block) does so when it changes from 1 to 0. To detect this, the PLC compares the current state with that of the previous scan cycle, which it stores in a memory bit. The edges are used for counting, to advance a sequence by one step with each press, and to start a cycle only once, even if the operator holds down the button.
A lamp should turn on when you press a button and turn off when you press it again. Why do we need the front panel? How do you spell it?
49.9 Timers and Counters
The TON, TOF, and TP Timers
The IEC 61131-3 standard defines three timers, which are represented as blocks in ladder logic. All of them have an input IN, a time that was once set PT (preset time), an exit Q and the time that has passed ET (elapsed time).
- TON, turn-on delay: Q goes to 1 when IN has remained at 1 for the PT duration. If IN returns to 0 first, the count is reset to zero. This is used for startup delays, sensor filters, and wait times.
- TOF, de-excitation delay: Q goes to 1 immediately when IN is set to 1, and returns to 0 only when IN has remained at 0 for the duration of PT. This is used for a fan that continues to run after being turned off, or a light that stays on for a short while.
- TP, pulse: On the rising edge of IN, Q generates a pulse exactly as long as PT, even if IN lasts longer or shorter. During the pulse, a new edge does not trigger it again.
The CTU and CTD meters
The CTU forward counter increments the current value CV by 1 on each rising edge of the CU input; the Q output goes to 1 when CV reaches the preset value PV; the R input resets it. The CTD down counter decrements CV on every CD edge, loads to the PV value with the LD input, and sets Q = 1 when CV reaches 0. The CTUD performs both functions simultaneously, for example, to count the number of parking spaces as a lot fills up and empties. The counter counts the edges, not the duration that the input remains at 1: a stationary workpiece in front of the photocell counts as one.
An engine's cooling fan must start when the engine starts and remain on for another 2 minutes after the engine has stopped. What timer is used, and what is the time setting?
49.10 Analog Values, Comparisons, and Calculations
An analog input converts a voltage or current into an integer using an analog-to-digital converter (Chapter 46). The number alone doesn’t mean anything: it must be converted back to the physical quantity, that is, scaled. The most commonly used signal in a workshop is 4–20 mA: 4 mA corresponds to the start of the scale, and 20 mA to the full scale. We start at 4 rather than 0 for a practical reason: if the wire breaks, the current drops to 0—well below 4 mA—and the PLC recognizes this not as zero pressure but as a fault (the so-called live zero). For the same reason, the current signal is not affected by the resistance of long wires, which, in contrast, causes a voltage signal to drop.
The relationship between current and magnitude is a straight line (Chapter 7): all you need to know are two points—the starting point and the full-scale point.
A 0–10 bar transducer with a 4–20 mA output. What pressure corresponds to 12 mA and 9.6 mA? What current will it output at 6 bar? And what if the PLC reads 3.2 mA?
The analog module of the same system converts 4 mA to 0 points and 20 mA to 27,648 points (a range used by many modules). The PLC reads 17,280 points. What is the pressure? What number corresponds to the 4-bar threshold?
Once the physical value is obtained, it is compared against certain thresholds. Comparators (=, ≠, >, ≥, <, ≤) take two numbers and produce a bit: in ladder logic, they are drawn as special contacts or as blocks with two inputs. Similarly, there are arithmetic blocks (ADD, SUB, MUL, DIV) and the MOVE block, which copies a value. For calculations, however, structured text is much more convenient and readable: the scaling and alarm in the example can be written in just two lines. In the ST language, decimals are written with a period, just like in any programming language.
PRESS_BAR := (CORRENTE_MA - 4.0) * 10.0 / 16.0; (* scalatura 4-20 mA in 0-10 bar *) GUASTO_SENS := CORRENTE_MA < 3.6; (* filo rotto o sensore guasto *)
A machine's pneumatic system must trigger an alarm if the pressure drops below 4.0 bar. To avoid false alarms during brief drops caused by a valve opening, the alarm must trigger only if the pressure remains low for more than 2 seconds. How do you write this?
49.11 Examples of ladder logic instructions
In this section, we’ll bring together the elements we’ve covered so far to solve the most common machine commands. For each one: what it’s supposed to do, the variables, and the solution we’ve drawn. It’s a good idea to try them out on your own before looking at the diagram.
Using the table in Example 49.6: the engine starts when set to "RUN" and stops when set to "STOP" or if the thermal cut-off is triggered; a green light indicates that the engine is running, and a yellow light indicates that the thermal cut-off has been triggered.
A motor must run forward (contactor KM_AV, Q0.0) or backward (KM_IN, Q0.1). The two contactors must never close at the same time: if two phases were swapped, they would short-circuit the line. Inputs: STOP NC (I0.0), FORWARD (I0.1), REVERSE (I0.2), THERMAL NC (I0.3).
Why repeat the interlock in the wiring? Because in the PLC, network 1 turns off KM_AV, and in the same cycle, network 2 can already turn on KM_IN: the two outputs change within a few milliseconds of each other, while a contactor takes tens of milliseconds to open and the electric arc persists for a little while longer. Furthermore, a faulty output transistor may remain closed on its own. The NC auxiliary contact of each contactor, wired in series with the coil of the other, makes the interlock independent of the program. On large motors, a brief pause with a TON is also added before the reversal.
A small press will lower only if the operator presses the two buttons P_SX (I0.0) and P_DX (I0.1) within 0.5 seconds of each other, and only as long as both are held down. If the operator releases one of them, to restart the press, they must release both buttons and press them again.
A traffic light at an internal crossing within a factory stays green for 20 seconds, yellow for 3 seconds, and red for 25 seconds, then repeats the cycle as long as the ON switch (I0.0) is closed. How long does a cycle last, and how do you write this using three TONs?
A conveyor belt starts with RUN (I0.0) and stops with NC stop (I0.1). A photoelectric sensor (I0.2) counts the workpieces that fall into a box: when the tenth workpiece is counted, the conveyor belt stops automatically and the H_LOTTO indicator light turns on. Pressing RUN again resets the count and fills the next box.
A pump (Q0.0) fills a tank from which water is drawn. Two sensors output a value of 1 when wet: LIV_MIN at the bottom (I0.1) and LIV_MAX at the top (I0.2). With the AUTO switch (I0.0) set, the pump should start when the water level drops below the minimum and stop at the maximum; it should not continuously turn on and off around a single level.
49.12 Sequences and Grafcet
Many machines operate in steps: one cylinder extends, then another, then the first retracts, and so on. You’ve already seen pneumatic sequences such as A+ B+ B− A− in Chapter 42, along with the issue of interlock signals and the cascade method. With a PLC, the problem is solved more simply: the machine’s current phase is stored in a single bit.
A double-acting cylinder is controlled by a monostable 5/2-way valve (coil Y1, Q0.0): when Y1 is energized, the cylinder extends; when de-energized, it retracts. Limit switches a0 (I0.1, cylinder retracted) and a1 (I0.2, cylinder extended). When START (I0.0) is pressed, the cylinder must extend and retract once, even if START remains pressed.
Grafcet
When there are more than two phases, a ladder diagram drawn “by intuition” becomes a tangled mess. In that case, one first draws the Grafcet (IEC 60848 standard), a diagram that describes the cycle in a way that the mechanical engineer, the electrician, and the programmer can all understand. In the IEC 61131-3 standard, this same method becomes the SFC language. There are three elements:
- phases (or stages), represented as numbered squares; the initial phase has a double square. A phase is either active or inactive;
- transitions, a solid dash indicating the connection between two phases, with the condition (known as receptivity) next to it that must be true in order to move on;
- Actions, rectangles to the right of the phases: what the machine does while the phase is active.
The rule of evolution is simple: a transition is completed when the preceding phase is active and its condition is true; at that point, the next phase becomes active and the previous one becomes inactive. The cycle proceeds in this way, one step at a time, and at any given moment, we know exactly where the machine is in the process.
Two double-acting cylinders with 5/2-way bistable valves (A: YA+ and YA−; B: YB+ and YB−) and limit switches a0, a1, b0, and b1. When START is pressed, with the cylinders retracted, the machine performs A+, B+, B−, A−, and then stops.
| Symbol | Address | Symbol | Address |
|---|---|---|---|
| START | I0.0 | YA+ | Q0.0 |
| a0 | I0.1 | YB+ | Q0.1 |
| a1 | I0.2 | YB− | Q0.2 |
| b0 | I0.3 | YA− | Q0.3 |
| b1 | I0.4 | X0 … X4 (phases) | M10.0 … M10.4 |
Translate the Grafcet from Example 49.22 into ladder logic using the phase-bit method. Each phase is a marker (X0 … X4).
49.13 Best Practices and Safety
A program is written by one person, but many people read it over the years: the night shift maintenance technician, the colleague who modifies it, and even you yourself in six months. A few good habits can make all the difference.
- Symbols and comments: Each variable has a clear name and a comment explaining what it means; each network has a title (“tape run,” “pressure alarm”).
- One coil per bit: each output is connected to a single point. If an output turns on for multiple reasons, those reasons are connected in parallel within the same network.
- Program divided into blocks: The standard refers to these program components as POUs (program organization units): programs, functions, and functional blocks. Having one block for each machine group (conveyor, press, alarms) makes the code more readable and reusable.
- Clear Alarms: A message on the panel is worth more than a hundred LEDs: “Air pressure below 4 bar” is immediately understandable.
- Copies and versions: Before making any changes, save the program with the date and note what has been changed.
Safety cannot rely solely on the PLC
A standard PLC is reliable, but it is not designed for safety functions: a failure of the CPU, a transistor output, or an error in the program could cause an output to remain on. This is why the emergency stop is hardwired: the red pushbutton, with positively driven NC contacts, controls a safety relay that directly cuts power to the outputs and contactors. The PLC reads the emergency status solely for informational purposes, to stop the cycle in an orderly manner and display the message. After an emergency, the machine must not restart on its own: a manual reset command is required.
When there are many safety functions (light curtains, locked doors, two-hand control, reduced speed), a safety PLC is used: it has two processors that perform the same calculations and monitor each other, dual inputs that compare two contacts from the same button, self-checking outputs, and a certification indicating the level of safety achieved. Standards, safety levels, and risk assessment are covered in Chapter 51.
49.14 The PLC in a CNC Machine and Diagnostics
Every numerically controlled machine tool has a PLC inside it. The CNC (Chapter 50) handles the program for workpieces and the movement of the axes; everything else—that is, the “machine logic”—is managed by a PLC integrated into the control system, written by the machine manufacturer. On Fanuc controls, it is called a PMC (programmable machine controller); on Siemens controls, it is a PLC integrated into the control system; in all cases, it is programmed in ladder logic or in languages compliant with the IEC 61131-3 standard.
- M codes: The CNC doesn't know what a pump is; when it reads "
M08," it sends the number 08 to the PLC via a strobe signal, and the PLC turns on the coolant pump; - spindle: compatibility with
M03,M04,M05; closed-jaw or collet control; orientation; - Tool change: Using the
M06command and the letter "T," the PLC controls the search in the tool magazine, the tool changer arm, and the sensors for unlocked and locked tools; - doors: door lock, authorization to operate with the door closed, reduced speeds when the door is open (in conjunction with the safety circuits);
- centralized lubrication, coolant, chip conveyor, lights, along with their timers and level alarms;
- operator panel: buttons, mode selectors, override potentiometers.
The CNC reads M08 (coolant on) or M09 (coolant off). The PLC receives the code already decoded into bits (DEC_M08, DEC_M09, DEC_M30) and the STROBE_M signal. How do you write the logic, including the FIN?
Simulation and Commissioning
You don't test a new program on the actual machine the first time. Programming software includes a simulator that runs the program on the PC: you force the inputs with the mouse and observe the outputs, checking even the most unusual sequences (two buttons pressed simultaneously, a sensor that doesn’t register, a power outage mid-cycle). Then, on the machine itself, commissioning follows a specific sequence.
- First, the safety circuits: emergency circuits, doors, safety relays—tested one by one;
- Next, check the wiring: press each button and activate each sensor while monitoring the LED and the bit in the software; test each output in an open-circuit condition, with the power disconnected or with the actuators free to move;
- Forcing outputs must be used with great caution, because it actually sets the machine in motion and bypasses the logic: whoever forces the output must be able to see the machine, and no one should be in the danger zone;
- Finally, the cycle—first step-by-step, then on automatic at reduced speed, and finally at full speed.
Diagnosis Using the Online Status
By connecting the PC to the running PLC, you can view the program in online mode: the contacts that are currently open and the energized coils are highlighted—usually in green—and the current values appear next to the timers and counters. To troubleshoot a fault, start with the output that isn’t activating and trace the circuit to the left until you reach the first contact that isn’t conducting: that’s the missing signal. Then check the machine to see why it’s missing.
The operator presses the RUN button, but the pump shown in Example 49.15 does not start. The online status displays the circuit shown in Figure 49.36. Where should the operator look for the fault?
| What Can You See? | What does that mean? | What do you do? |
|---|---|---|
| Green MARCH | The button is connected to the PLC | Nothing: The button and its wire are in place |
| Green STOP | I0.1 = 1, the stop button is not pressed, and the wire is intact | nothing |
| THERMAL (not green) | I0.2 = 0: Contact 95-96 is open or the file is broken | Check the I0.2 LED and the F1 thermal relay: if it has tripped, determine why the motor drew too much current, then reset it |
| H_THERMAL indicator light is on | Confirmation: Network 3 shows I0.2 = 0 | After the reset, the indicator light turns off and the engine restarts in DRIVE |
49.15 The Formulas in This Chapter
| What | Formula |
|---|---|
| Maximum response time | tr,max = tf + 2 · tc + tu |
| Duration of the pulse from a passing object | timp = L / v |
| Pulse Read with Confidence | timp > tc + tf; in other words, timp ≥ 2 · tc |
| Current Through a DC Load | I = P / U |
| Channels with a reserve | channels = signals · (1 + reserve), rounded up |
| Linear scaling | y = ymin + (x − xmin) · (ymax − ymin) / (xmax − xmin) |
| 4–20 mA Scale | p = pmin + (I − 4) · (pmax − pmin) / 16 |
| Current for a given value | I = 4 + (p − pmin) · 16 / (pmax − pmin) |
49.16 Exercises
For the programming exercises, the solution is written as a list of circuits; draw them in ladder diagram format on a sheet of paper, and if you can, test them using a simulator.
Solution
tr,max = 5 + 2 × 20 + 1 = 46 ms.
Solution
p = (14 − 4) · 6 / 16 = 10 · 0.375 = 3.75 bar. I = 4 + 4.5 · 16 / 6 = 4 + 12 = 16 mA.
Solution
T = −50 + (10 − 4) · 200 / 16 = −50 + 75 = 25 °C. I = 4 + (120 + 50) · 16 / 200 = 4 + 13.6 = 17.6 mA.
Solution
L = v · 2 · tc = 1.2 · 2 · 0.015 = 0.036 m = 36 mm.
Solution
Solenoid valve: 3 / 24 = 0.125 A; indicator light: 2.4 / 24 = 0.1 A. Total: 6 · 0.125 + 3 × 0.1 = 0.75 + 0.30 = 1.05 A < 2 A: it works, and each channel is below 0.5 A.
Solution
Operations per day: 10 · 60 · 8 = 4,800. Duration: 400,000 / 4,800 = 83.3 days. It’s better to use a transistor output or an interface relay that’s easy to replace.
Solution
Bits 0, 3, and 5 are set to 1: I0.0, I0.3, I0.5. In decimal, 32 + 8 + 1 = 41. Bit 5 of byte 2 is I2.5 (in the standard notation %IX2.5).
Solution
L = (A AND NOT B) OR C. A circuit: a branch with the NO contact of A in series with the NC contact of B, in parallel with a branch containing the NO contact of C; the coil L is on the right.
Solution
With the NC button: I0.1 goes to 0, the NO contact opens, and the motor stops; the fault is immediately apparent. With the NO button: the disconnected wire would leave I0.1 at 0 indefinitely; the NC contact would remain closed, and no one would notice until the stop button was pressed and the motor stopped.
Solution
Circuit 1: T2.Q’s NC contact → TON T1, PT 0.5 s. Circuit 2: T1.Q’s NO contact → TON T2, PT 0.5 s. Circuit 3: T1.Q’s NO contact → indicator coil. T1 counts for 0.5 s with the indicator light off; then T1.Q turns on the indicator light and starts T2; after another 0.5 s, T2.Q resets T1, the indicator light turns off, T2 resets, and the cycle restarts. Period: 1 s.
Solution
CV = 38 − 6 + 18 = 50 = PV: Q = 1, FULL on. After 3 outputs, CV = 47 < 50: FULL off.
Solution
States: 0 (idle), 1 (YA+), 2 (YA−), 3 (YB+), 4 (YB−). Transitions: 0 → 1 with ↑START · a0 · b0; 1 → 2 with a1; 2 → 3 with a0; 3 → 4 with b1; 4 → 0 with b0. Networks: X0 AND ↑START AND a0 AND b0 → S X1, R X0; X1 AND a1 → S X2, R X1; X2 AND a0 → S X3, R X2; X3 AND b1 → S X4, R X3; X4 AND b0 → S X0, R X4. Actions: X1 → YA+, X2 → YA−, X3 → YB+, X4 → YB−. Note: The transition 2 → 3 uses a0, which is also in the initial state, but there is no confusion because each network also considers its own phase bit.
Solution
Network 1: AUTO AND NOT T2.Q → TON T1, PT 15 min (900 s). Network 2: T1.Q → TON T2, PT 10 s. Network 3: T1.Q → LUB_PUMP. A cycle lasts 900 + 10 = 910 s; in 8 hours: 8 · 3600 / 910 = 31.6 → 31 complete lubrications.
Solution
After 0.5 s, T1.Q goes to 1; when P_DX arrives, the NC contact of T1.Q is open and the PRESS has no hold function: the press remains stationary. You must release both buttons (T1 resets) and press them again within 0.5 seconds of each other.
