PLC programming
The IEC 61131-3 standard defines five languages. They do not compete with one another: each is convenient for a class of problem, and in a real project they usually coexist. What does not change is the method: first you understand the process, then you write the program.
01The five languages
| Language | Type | What it is good for |
|---|---|---|
| Ladder diagram (LD) | graphical | Combinational and control logic. Any electrician can read it. By far the most widely used. |
| Function block diagram (FBD) | graphical | Processes built from chained blocks: PID, filters, scaling, communication. |
| Instruction list (IL) | textual | Compact and very close to the machine. Being phased out, but still found on older equipment. |
| Structured text (ST) | textual | Calculations, loops, table handling and everything that becomes unreadable in ladder. |
| Grafcet or SFC | graphical | Sequences: steps, transitions and actions. The one that truly brings order to a machine that works in steps. |
Rule of thumb: if the problem is described as “when this and that happen, activate that,” it goes in ladder. If it is described as “first this, then that, and if it fails go back to the start,” it goes in Grafcet. If calculations are involved, it goes in structured text. Forcing everything into ladder produces two-hundred-rung programs that nobody can read.
02Ladder diagram
| Element | What it means |
|---|---|
| Normally open contact | Conducts when the bit is 1. Read as “if the signal is active.” |
| Normally closed contact | Conducts when the bit is 0. Read as “if the signal is not active.” |
| Coil | Writes the result of the rung to a bit: an output or a memory flag. |
| Set and Reset coils | Turn a bit on or off permanently until the other one acts. Useful, but easy to leave without their counterpart. |
| Edge detection | Gives a one-scan pulse when the signal changes. Essential for counting and for triggering actions only once. |
| Series and parallel | Contacts in series form a logical AND; in parallel, a logical OR. |
This is the fundamental circuit from which almost all the others derive. In ladder:
start stop_nc estop_ok motor
---| |----+------|/|------------| |--------------( )---
|
motor |
---| |----+
The motor contact in parallel with the start pushbutton is the seal-in (holding contact): once started, the rung sustains itself even if the pushbutton is released. The stop, in series, breaks that path.
The physical stop pushbutton is normally closed: at rest it delivers a 1 to the input. Therefore, in the program it is represented by a normally open contact, not a normally closed one. Using an NC contact in the program for a physical NC pushbutton inverts the logic: the machine never starts or, worse, starts when the stop is pressed.
The rule that avoids the mistake: the program is written according to what the input delivers, not according to what the button is called. And it is always verified in online monitoring, looking at the real state of each bit.
03Grafcet: thinking in sequences
- The initial step is activated at start-up.
- A transition fires if it is enabled—the preceding step is active—and its receptivity (transition condition) is true.
- When it fires, the next step is activated and the preceding one is deactivated, at the same time.
- Several transitions that can fire simultaneously all fire.
- If a step receives the order to be activated and deactivated at the same instant, it stays active.
- Linear sequence: one step after another.
- OR divergence: two possible paths, one is taken depending on the condition.
- AND divergence: two branches that run in parallel and synchronize at the end.
- Jump and loop: to repeat or skip steps.
It translates to ladder mechanically: one flag (memory bit) per step, with Set and Reset driven by the transitions. Many PLCs accept it directly as SFC.
Classic cycle A+ B+ A− B−: cylinder A extends, B extends, A retracts, B retracts.
| Step | Action | Transition to the next |
|---|---|---|
| 0 | Idle, everything retracted. | Cycle pushbutton and both retraction sensors active. |
| 1 | Extend A. | A-extended sensor. |
| 2 | Extend B. | B-extended sensor. |
| 3 | Retract A. | A-retracted sensor. |
| 4 | Retract B. | B-retracted sensor, and it returns to step 0. |
Written this way, the program cannot skip a step or stall halfway without it being noticed. Written directly in ladder, with loose flags and no structure, is where you get cycles that jam and nobody understands why.
04From the process to the program
| Step | What to do |
|---|---|
| 1 | Write in words what the machine does, including what happens on each fault and how it is reset. |
| 2 | List inputs and outputs with symbolic name, type and address: this is the I/O assignment table. |
| 3 | Draw the Grafcet or the flowchart of the sequence, with its conditions. |
| 4 | Write the program in blocks: safety, modes, sequence, outputs, signaling. |
| 5 | Simulate or test with no load, forcing inputs from the programming terminal. |
| 6 | Test with the machine, first in manual and step-by-step mode, then in automatic. |
| 7 | Document: commented program, assignment table, Grafcet and operating manual. |
Separate the program into blocks in a fixed order: safety and general conditions, mode selection (manual, automatic, step-by-step), sequence, output control and signaling. Each physical output is written in one place only, at the end, combining the sequence conditions with the safety ones.
That last rule—one output, one place—eliminates at the root the mistake of outputs written twice, which is the one that wastes the most time during commissioning.
05In the lab
Program the seal-in circuit with start, NC stop and emergency stop, and verify it online by watching the state of each bit. Then deliberately invert the stop contact in the program and analyze exactly what happens and why.
Program the direction reversal of a motor with an interlock between both directions and a waiting delay when reversing. Verify that no order of button presses manages to energize both contactors at the same time, and also add the mechanical and electrical interlock in the panel.
Draw the Grafcet of the A+ B+ A− B− sequence and translate it to ladder with one flag per step. Implement it on the pneumatic trainer. Then add an OR divergence for an alternative cycle and check how much simpler the change turns out to be with the step structure.
Hand another group the program with its assignment table and its Grafcet, and ask them to make a functional modification with no explanations. The time they take is the real measure of the quality of the documentation.
06Common mistakes
| Mistake | Consequence |
|---|---|
| NC contact in the program for a physical NC pushbutton | Inverted logic: the machine does not start, or starts when the stop is pressed. |
| Same output written in several rungs | Only the last one counts: the output seems not to respond. |
| Set without its matching Reset | Bits that stay active forever and states that cannot be left. |
| Counting without edge detection | The counter increments once per scan while the signal is active: absurd counts. |
| Programming the sequence with loose flags | Cycles that jam or skip steps, with no clear way to diagnose them. |
| Everything in ladder, including calculations | Unreadable rungs where structured text would have taken three lines. |
| No manual or step-by-step mode | Impossible to tune the machine or recover it after a fault. |
| Program without comments | Nobody can maintain it, and the program was the machine’s documentation. |
07Self-assessment
Name the five languages of IEC 61131-3.
Ladder diagram, function block diagram, instruction list, structured text and Grafcet or SFC.
When is Grafcet preferable to ladder?
When the problem is a sequence: first this, then that. Ladder is better for combinational and control logic.
What is a seal-in (holding contact) and how is it implemented?
A contact of the output itself in parallel with the start pushbutton, so that the rung sustains itself when the pushbutton is released. The stop goes in series and breaks that path.
A physical NC stop pushbutton: which contact does it get in the program?
A normally open one, because at rest the input is 1. The program is written according to what the input delivers, not according to what the button is called.
Why is edge detection needed for counting?
Because without it the counter increments on every scan while the signal is active. The edge gives a one-scan pulse for each change.
State the Grafcet evolution rule.
A transition fires if it is enabled—the preceding step is active—and its receptivity (transition condition) is true. When it fires, the next step is activated and the preceding one is deactivated simultaneously.
What is the difference between OR divergence and AND divergence?
In the OR, one of the paths is taken depending on the condition; in the AND, the branches run in parallel and synchronize at the end.
In what order is the program organized?
Safety and general conditions, mode selection, sequence, output control and signaling.
Why must each physical output be written in only one place?
Because the PLC executes in order and only the last write counts. Concentrating it at the end, combining sequence and safety, eliminates that error at the root.
What is step-by-step mode for?
For tuning the machine and for recovering it after a fault, advancing the sequence one step at a time under an operator’s supervision.