Catto / Topic Map · Industrial Electronics II Year 7
Industrial Electronics II · 120 h · Topic 4 of 8

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.

Ladder Grafcet Seal-in IEC 61131-3

01The five languages

LanguageTypeWhat it is good for
Ladder diagram (LD)graphicalCombinational and control logic. Any electrician can read it. By far the most widely used.
Function block diagram (FBD)graphicalProcesses built from chained blocks: PID, filters, scaling, communication.
Instruction list (IL)textualCompact and very close to the machine. Being phased out, but still found on older equipment.
Structured text (ST)textualCalculations, loops, table handling and everything that becomes unreadable in ladder.
Grafcet or SFCgraphicalSequences: steps, transitions and actions. The one that truly brings order to a machine that works in steps.
How to choose

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

startstopestop_okmotormotorIdlemotor stoppedNobody presses start and the motor is stopped: there is no complete path.Start pressedmotor runningThe path closes through the pushbutton and the coil energizes.Start releasedmotor runningThe motor’s own contact holds the rung: this is the seal-in.Stop pressedmotor stoppedThe stop contact opens the path and the coil drops out, even with the seal-in present.The motor’s own contact in parallel with start is the seal-in: it holds the rung once the pushbutton is released.Note: the stop pushbutton is NC on the panel, and for that reason it is represented in the program by an open contact.
Figure 1. A ladder rung in operation, animated. The imaginary “current” flows from left to right: if it finds a complete path of closed contacts, the coil energizes.
ElementWhat it means
Normally open contactConducts when the bit is 1. Read as “if the signal is active.”
Normally closed contactConducts when the bit is 0. Read as “if the signal is not active.”
CoilWrites the result of the rung to a bit: an output or a memory flag.
Set and Reset coilsTurn a bit on or off permanently until the other one acts. Useful, but easy to leave without their counterpart.
Edge detectionGives a one-scan pulse when the signal changes. Essential for counting and for triggering actions only once.
Series and parallelContacts in series form a logical AND; in parallel, a logical OR.
Start and stop with seal-in

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 NC contact trap

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

0idlecycle and both retracted1A+sensor A extended2B+sensor B extended3A-sensor A retracted4B-sensor B retractedStep 0 activeaction: idlemoves on to the next when:cycle and both retractedStep 1 activeaction: A+moves on to the next when:sensor A extendedStep 2 activeaction: B+moves on to the next when:sensor B extendedStep 3 activeaction: A-moves on to the next when:sensor A retractedStep 4 activeaction: B-moves on to the next when:sensor B retractedOnly one step active at a time.A transition fires when thepreceding step is active and itscondition is true.Written this way, the cycle cannot skip a step.
Figure 2. A Grafcet in operation, animated. Only one step is active at a time; the transition fires when its condition is true, and then the token advances to the next step.
Its five rules
  1. The initial step is activated at start-up.
  2. A transition fires if it is enabled—the preceding step is active—and its receptivity (transition condition) is true.
  3. When it fires, the next step is activated and the preceding one is deactivated, at the same time.
  4. Several transitions that can fire simultaneously all fire.
  5. If a step receives the order to be activated and deactivated at the same instant, it stays active.
Structures
  • 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.

Example · Two-cylinder sequence

Classic cycle A+ B+ A− B−: cylinder A extends, B extends, A retracts, B retracts.

StepActionTransition to the next
0Idle, everything retracted.Cycle pushbutton and both retraction sensors active.
1Extend A.A-extended sensor.
2Extend B.B-extended sensor.
3Retract A.A-retracted sensor.
4Retract 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

StepWhat to do
1Write in words what the machine does, including what happens on each fault and how it is reset.
2List inputs and outputs with symbolic name, type and address: this is the I/O assignment table.
3Draw the Grafcet or the flowchart of the sequence, with its conditions.
4Write the program in blocks: safety, modes, sequence, outputs, signaling.
5Simulate or test with no load, forcing inputs from the programming terminal.
6Test with the machine, first in manual and step-by-step mode, then in automatic.
7Document: commented program, assignment table, Grafcet and operating manual.
The structure that is always worth using

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

Lab 1 · Start and stop

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.

Lab 2 · Reversing with interlocks

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.

Lab 3 · Grafcet to ladder

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.

Lab 4 · Document and hand over

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

MistakeConsequence
NC contact in the program for a physical NC pushbuttonInverted logic: the machine does not start, or starts when the stop is pressed.
Same output written in several rungsOnly the last one counts: the output seems not to respond.
Set without its matching ResetBits that stay active forever and states that cannot be left.
Counting without edge detectionThe counter increments once per scan while the signal is active: absurd counts.
Programming the sequence with loose flagsCycles that jam or skip steps, with no clear way to diagnose them.
Everything in ladder, including calculationsUnreadable rungs where structured text would have taken three lines.
No manual or step-by-step modeImpossible to tune the machine or recover it after a fault.
Program without commentsNobody 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.

Development of the topic “PLC instructions and programming” of Industrial Electronics II (Year 7), based on the “Curriculum Proposal – Second Cycle of the Technical-Vocational Track, Secondary Education – Electronics,” Ministry of Education of the Province of Córdoba, DGETyFP. Back to the Topic Map · catto.ar