How-to
Program a CODESYS assembly line sequence in ladder logic
Short answer
Structure the line as one INT state variable with permissive-qualified station starts: TON timers of T#20s, T#40s and T#60s run the stations, Stop drops the run latch so everything pauses with the timers reset, and a CTU with a preset of 4 releases the vehicle on the fourth door. The Emergency Stop belongs in the safety circuit, with the PLC only monitoring it and clearing the sequence.
On this page
Structure the line as one INT state variable that every rung reads, qualify each station start with its own state and its position sensor, and time the stations with three IEC 61131-3 TON blocks: PT := T#20s for welding, PT := T#40s for painting and PT := T#60s for door assembly. A CTU counter with a preset of 4 releases the vehicle on the fourth installed door. Stop drops a run latch, so the line pauses with every timer reset; the Emergency Stop acts in the safety circuit, and this program only monitors it and returns the sequence to idle. Every element is standard Ladder Diagram in CODESYS.
One state variable carries the sequence #
A chain of sealed-in station rungs leaves several stations logically active at once and gives you no single place to pause the line or to test one transition. One INT tag with a defined value per step fixes both: exactly one state is active, the run latch is the one gate every transition passes through, and each arrow in the table below is one compare contact you can force and watch. IEC 61131-3 is the standard at work: it defines Ladder Diagram as one of its languages and specifies TON and CTU as standard function blocks, so the timer and counter behavior this program depends on is the standard's behavior, not a CODESYS extra.
iState |
Meaning | Active while | Advance when |
|---|---|---|---|
| 0 | Idle | All outputs off | bLineRunning latches on Start |
| 10 | Transfer to weld | Conveyor on | xAtWeld |
| 20 | Welding | Weld station on, tonWeld timing |
tonWeld.Q |
| 30 | Transfer to paint | Conveyor on | xAtPaint |
| 40 | Painting | Paint station on, tonPaint timing |
tonPaint.Q |
| 50 | Transfer to doors | Conveyor on | xAtDoors |
| 60 | Door assembly | Door station on, tonDoors timing |
ctuDoors.Q at the fourth door |
| 70 | Release | Conveyor on | xAtExit, back to 0 |
Declare the tags and the wiring behind them #
Declare every tag with a type and an initial value so a cold start lands in a known state. Wire the Stop button through a normally closed contact: xStopHealthy is then TRUE when the button is released and the wire is intact, so a broken conductor reads as Stop, never as run. The block names below are the standard types; the signal names are yours to declare.
| Tag | Type | Initial | Role |
|---|---|---|---|
xStart |
BOOL | FALSE | Start pushbutton, momentary |
xStopHealthy |
BOOL | TRUE | Stop circuit through an NC contact, TRUE when healthy |
xEstopOK |
BOOL | TRUE | Auxiliary contact of the safety circuit, TRUE when healthy |
xAtWeld, xAtPaint, xAtDoors, xAtExit |
BOOL | FALSE | Vehicle position sensors |
qConveyor, qWeld, qPaint, qDoors |
BOOL | FALSE | Output commands |
bLineRunning |
BOOL | FALSE | Run latch every transition passes through |
iState |
INT | 0 | Sequence state, per the table above |
tonWeld, tonPaint, tonDoors |
TON | — | On-delay timer instances |
ctuDoors |
CTU | — | Up-counter instance |
Build the rungs in this order #
- Run latch.
xStartin parallel withbLineRunning, in series withxStopHealthyandxEstopOK, coilsbLineRunning. This is the seal-in pattern of a sealed motor starter rung, and the latch is the master gate: nothing advances, runs or times unless it is TRUE. - Transitions. One rung per arrow in the state table: an equality compare contact on the current
iState, in series withbLineRunningand the condition from the last column, driving a MOVE of the next value intoiState. The 0 to 10 rung needs onlyiState = 0and the latch. - Welding.
iState = 20,bLineRunningandxAtWeldfeedtonWeldwith the preset PT := T#20s;qWeldcoils from the same series condition. ET counts in milliseconds, so Q appears when ET reaches 20,000 ms, and the 20 to 30 transition fires ontonWeld.Q. - Painting. Same shape in state 40:
xAtPaintqualifiestonPaint, PT := T#40s,tonPaint.Qmoves the sequence to 50, andqPaintfollows the state. - Door assembly.
iState = 60,bLineRunning,xAtDoorsand a normally closed contact oftonDoors.QfeedtonDoors, PT := T#60s. The Q contact unpowers the timer the scan after it finishes and resets it, so the timer runs once per door, four times per vehicle. - Door counter.
iState = 60in series withtonDoors.QdrivesctuDoors.CU, with the preset PV := 4. CTU adds 1 on each rising edge at CU and holds Q TRUE while CV is 4 or more, so a held condition contributes exactly one count, the same edge discipline as a siren ring count built from a timer and counter. If the fixture gives you a per-door installed sensor, feed its edge to CU instead oftonDoors.Q, still qualified by state 60. - Counter reset.
iState = 0coilsctuDoors.R. The reset input is level-acting, so the counter sits at CV = 0 for the whole idle state and cannot lose a count mid-cycle. - Outputs.
qConveyorcoils frombLineRunningAND (iState = 10ORiState = 30ORiState = 50ORiState = 70);qWeld,qPaintandqDoorseach follow their own state and the latch. - Emergency-stop clear. A rung conditioned on
xEstopOKFALSE moves 0 intoiState, so the sequence lands in idle when the safety circuit opens.
What Stop does to the timers #
Pressing Stop drives xStopHealthy FALSE, which unlatches bLineRunning: the conveyor stops, every station output drops, no transition rung is true, and iState holds its value. The timers do not hold theirs. A TON keeps ET only while IN is TRUE; the scan the rung drops, ET returns to 0 and Q to FALSE. That reset-on-false rule is standard IEC 61131-3, and the situations where it seems broken, such as why a CODESYS TON output stays on after its rung goes false, come from how the block is called, not from the timer holding time.
Commit to this pause behavior: Stop resets elapsed time, and each station re-runs its full delay when you press Start again. For dwell stations that is the conservative choice, because a repeated dwell wastes time but never skips an operation. If a station cannot tolerate being repeated, its completion condition must come from process feedback, not from elapsed time.
Where the Emergency Stop belongs #
The emergency stop is a safety function: IEC 60204-1 requires it to stop the machine as a category 0 or category 1 stop through the safety-related parts of the control system, independently of the programmable logic. The button's normally closed contacts open the safety relay, and the relay drops the contactors that feed hazardous motion. This ladder program receives an auxiliary contact of that circuit as xEstopOK; it unlatches the run latch and zeroes iState, which is housekeeping, not the safety function. After a safety reset, start from state 0, because vehicle positions following an emergency stop are not trusted.
One vehicle, by the numbers #
The three delays are specified values; the transfer time you measure on your line. Weld and paint dwell adds to 60 s, and the four door operations at PT := T#60s contribute 240 s, so station dwell per vehicle is 300 s = 5.0 min. Take an illustrative measured transfer of 15 s per conveyor move, across four moves (into weld, into paint, into doors, out to the exit): 4 × 15 s of transfer. One vehicle then needs 300 s + 4 × 15 s = 360 s = 6.0 min, and the line delivers 3600 s/h ÷ 360 s = 10 vehicles per hour. Each second of transfer time you shave moves the cycle by 4 s. If your fixture installs all four doors inside a single T#60s dwell, dwell drops to 120 s, the cycle to 180 s and the rate to 20 vehicles per hour at the same transfers.
Test it before the conveyor is coupled #
Run the first tests with the output circuits disconnected or the actuators otherwise made safe, and watch the tags, not the drawing.
- In state 0, press Start:
iStateshows 10 andqConveyoris TRUE. ForcexAtWeld: state 20,tonWeld.ETclimbs to 20,000 ms,tonWeld.Qgoes TRUE and the next scan movesiStateto 30. - Repeat at paint, where
tonPaint.ETmust reach 40,000 ms, and at doors, wheretonDoorsrestarts itself: Q pulses once per door andctuDoors.CVsteps 1, 2, 3, 4 with no skips or repeats. - On the fourth count
ctuDoors.Qis TRUE,iStateshows 70, andxAtExitreturns the sequence to 0 with CV back to 0. - Press Stop mid-weld:
qWelddrops andtonWeld.ETreads 0. Press Start: the sequence resumes in state 20 and ET must reach 20,000 ms again before the transition. - Open the safety auxiliary:
iStatefalls to 0 and every output command is FALSE.
What you have committed to #
- The sequence is one INT,
iState, with the states tabled above; a station runs only in its own state, with its sensor TRUE and the latch TRUE. - Timers run at T#20s, T#40s and T#60s per door, with elapsed time as the completion proxy; a station that must not be repeated completes on process feedback instead.
- Stop pauses with reset: outputs off, state held, timers at zero, resume on Start from the same state.
- The Emergency Stop stops the machine in the safety circuit; this program clears to idle and restarts from state 0.
- The counter is a CTU with a preset of 4, one edge per door, reset held through idle.
- Release is state 70 only: the conveyor runs with the latch TRUE until the exit sensor ends the cycle.
Questions asked
Why does the welding timer start over instead of resuming its elapsed time after Stop?
A TON has no hold input: the moment IN goes false, IEC 61131-3 resets ET to 0 and Q to FALSE, so gating the station rung with the run latch costs you the elapsed time. The station re-runs its full 20 s when you press Start again. If a station must resume mid-dwell, accumulate elapsed time in an adder rung clocked once per second and compare it against the preset instead of relying on the timer's own ET.
Should the Emergency Stop be a PLC input that switches outputs off in ladder?
No. The emergency stop must remove hazardous motion through the safety circuit, meaning the button, safety relay and contactors, with the PLC logic as a secondary, monitored path. Wire an auxiliary contact of that circuit to xEstopOK so the program can drop its commands and zero the state, but the safety function itself is designed and validated separately from this ladder program.
When exactly does the door counter reset?
Hold ctuDoors.R true for the whole idle state, iState = 0. Reset is level-acting, so the counter sits at CV = 0 until the next vehicle reaches state 60, and no count can be lost mid-cycle because idle is the only place reset is active. Do not drive reset from the counter output while still in the release state: the release condition would then depend on a signal you are actively clearing, instead of on the exit sensor.
What cycle time does this program give, and what moves it?
The specified delays give 300 s of station dwell per vehicle: 20 s + 40 s for weld and paint, plus 4 × 60 s for the doors. At an illustrative 15 s per conveyor move across four moves, that is 4 × 15 s of transfer, so 360 s per vehicle and 3600 ÷ 360 = 10 vehicles per hour. If your fixture installs all four doors inside one door-length dwell, dwell falls to 120 s and the same arithmetic gives 20 vehicles per hour. Replace the 15 s with your measured transfer time.
What happens to the sequence after a power cycle?
Declare iState with an initial value of 0 and leave the counter non-retained: a cold start lands in idle with CV = 0 and every output off, and the interrupted vehicle is walked through the remaining stations again. Make the state or the counter RETAIN only if you accept resuming a partially completed operation after power returns, and test that recovery path explicitly before relying on it.