IMDHMIControls LibraryShop main store

How-to

Program a CODESYS assembly line sequence in ladder logic

PLC programming Published 8 min read

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
  1. One state variable carries the sequence
  2. Declare the tags and the wiring behind them
  3. Build the rungs in this order
  4. What Stop does to the timers
  5. Where the Emergency Stop belongs
  6. One vehicle, by the numbers
  7. Test it before the conveyor is coupled
  8. What you have committed to
  9. Questions asked

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 #

  1. Run latch. xStart in parallel with bLineRunning, in series with xStopHealthy and xEstopOK, coils bLineRunning. 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.
  2. Transitions. One rung per arrow in the state table: an equality compare contact on the current iState, in series with bLineRunning and the condition from the last column, driving a MOVE of the next value into iState. The 0 to 10 rung needs only iState = 0 and the latch.
  3. Welding. iState = 20, bLineRunning and xAtWeld feed tonWeld with the preset PT := T#20s; qWeld coils 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 on tonWeld.Q.
  4. Painting. Same shape in state 40: xAtPaint qualifies tonPaint, PT := T#40s, tonPaint.Q moves the sequence to 50, and qPaint follows the state.
  5. Door assembly. iState = 60, bLineRunning, xAtDoors and a normally closed contact of tonDoors.Q feed tonDoors, 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.
  6. Door counter. iState = 60 in series with tonDoors.Q drives ctuDoors.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 of tonDoors.Q, still qualified by state 60.
  7. Counter reset. iState = 0 coils ctuDoors.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.
  8. Outputs. qConveyor coils from bLineRunning AND (iState = 10 OR iState = 30 OR iState = 50 OR iState = 70); qWeld, qPaint and qDoors each follow their own state and the latch.
  9. Emergency-stop clear. A rung conditioned on xEstopOK FALSE moves 0 into iState, 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: iState shows 10 and qConveyor is TRUE. Force xAtWeld: state 20, tonWeld.ET climbs to 20,000 ms, tonWeld.Q goes TRUE and the next scan moves iState to 30.
  • Repeat at paint, where tonPaint.ET must reach 40,000 ms, and at doors, where tonDoors restarts itself: Q pulses once per door and ctuDoors.CV steps 1, 2, 3, 4 with no skips or repeats.
  • On the fourth count ctuDoors.Q is TRUE, iState shows 70, and xAtExit returns the sequence to 0 with CV back to 0.
  • Press Stop mid-weld: qWeld drops and tonWeld.ET reads 0. Press Start: the sequence resumes in state 20 and ET must reach 20,000 ms again before the transition.
  • Open the safety auxiliary: iState falls 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.

Keep reading

  • How-to

    Build a siren pulse count sequence in S7-1200 ladder logic

    Use a positive-edge request to load a target of 3, 2, or 1 pulses, then run a four-state ladder sequence — idle, start, stop, count — that drives Q6.2 (START) and Q6.1 (STOP) from separate states so the two SIMOCODE Pro V commands never overlap. With a 2.0 s on-time and a 1.0 s gap, the three patterns finish in 11.0 s, 7.0 s, and 3.0 s.

    PLC programming

  • How-to

    Structure dock door conveyor interlocks in Studio 5000

    Run the conveyor on three explicit states — Ready, Running and Restart-required — with door-open, truck-present and curtain-clear as permissives. Truck departure mid-cycle stops the conveyor and latches a manual restart; the light curtain's protective stop stays in safety-rated equipment. Fix the door sensor before debouncing, then size the timer to the measured flicker — 600 ms against a 240 ms example.

    PLC programming

  • How-to

    How to verify AI-generated PLC code before you trust it

    A clean compile proves your AI-generated rung is syntactically valid for the project, not that it behaves correctly. Write a behavior specification first — states, startup, fault response, reset — then run normal, abnormal and restart tests in an isolated environment before any download to operating equipment. Generated logic earns trust the same way hand-written logic does: by passing a test written before the code was accepted.

    PLC programming